Dear fellows,
I've been working this problem for a day and still can't see where the
problem is. I really appreciate if anyone can help me out.
My data is like:
V1 V2 v3
1 a 1.3
1 a 1.5
1 b 2.0
1 a 2.3
1 a 3.4
1 c 5.5
1 d 6.0
1 a 7.0
2 f 1.5
2 g 1.6
2 f 3.4
2 f 4.0
2 g 4.6
2 c 5.0
2 a 5.3
What I want to do is to get the max value of V3 for each unique V2 in each
V1. For example, when V1=1, unique V2 is (a, b, c, d), and corresponding max
value of V3 is (7.0, 2.0, 5.5, 6.0). And V1=2, unique V2 is (f, g, c, a) and
max value of V3 is (4.0, 4.6, 5.0, 5.3). Then combinne these max values.
My code is like:
z<-NULL
x<-NULL;y<-NULL
for (j in 1:2)
{
x<-unique(V2[V1==j])
for (i in 1:length(x))
{
y[i]<-max(V3[V2==x[i] & V1==j])
}
z<-c(z,y)
}
length(z)
My problem is the length of z is much bigger than the length of I'm supposed
to get, like above data, I should only get 8 max values. But the code will
return me a number bigger than 8. I'm thinking there might be some
overlapping problem. Can someone help?
Thank you for your time.
Best,
--
View this message in context:
http://r.789695.n4.nabble.com/Need-help-for-loop-code-thanks-tp2967036p2967036.html
Sent from the R help mailing list archive at Nabble.com.
Is this what you're looking for?> dat = read.table(textConnection('V1 V2 v3+ 1 a 1.3 + 1 a 1.5 + 1 b 2.0 + 1 a 2.3 + 1 a 3.4 + 1 c 5.5 + 1 d 6.0 + 1 a 7.0 + 2 f 1.5 + 2 g 1.6 + 2 f 3.4 + 2 f 4.0 + 2 g 4.6 + 2 c 5.0 + 2 a 5.3'),header=TRUE)> aggregate(dat$v3,dat[c('V1','V2')],max)V1 V2 x 1 1 a 7.0 2 2 a 5.3 3 1 b 2.0 4 1 c 5.5 5 2 c 5.0 6 1 d 6.0 7 2 f 4.0 8 2 g 4.6 - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Thu, 7 Oct 2010, wangguojie2006 wrote:> > Dear fellows, > > I've been working this problem for a day and still can't see where the > problem is. I really appreciate if anyone can help me out. > > My data is like: > > V1 V2 v3 > 1 a 1.3 > 1 a 1.5 > 1 b 2.0 > 1 a 2.3 > 1 a 3.4 > 1 c 5.5 > 1 d 6.0 > 1 a 7.0 > 2 f 1.5 > 2 g 1.6 > 2 f 3.4 > 2 f 4.0 > 2 g 4.6 > 2 c 5.0 > 2 a 5.3 > > What I want to do is to get the max value of V3 for each unique V2 in each > V1. For example, when V1=1, unique V2 is (a, b, c, d), and corresponding max > value of V3 is (7.0, 2.0, 5.5, 6.0). And V1=2, unique V2 is (f, g, c, a) and > max value of V3 is (4.0, 4.6, 5.0, 5.3). Then combinne these max values. > > My code is like: > > z<-NULL > x<-NULL;y<-NULL > for (j in 1:2) > { > x<-unique(V2[V1==j]) > for (i in 1:length(x)) > { > y[i]<-max(V3[V2==x[i] & V1==j]) > } > z<-c(z,y) > } > length(z) > > My problem is the length of z is much bigger than the length of I'm supposed > to get, like above data, I should only get 8 max values. But the code will > return me a number bigger than 8. I'm thinking there might be some > overlapping problem. Can someone help? > > Thank you for your time. > > Best, > > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Need-help-for-loop-code-thanks-tp2967036p2967036.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
you can use sqldf:> dat = read.table(textConnection('V1 V2 v3+ + 1 a 1.3 + 1 a 1.5 + 1 b 2.0 + 1 a 2.3 + 1 a 3.4 + 1 c 5.5 + 1 d 6.0 + 1 a 7.0 + 2 f 1.5 + 2 g 1.6 + 2 f 3.4 + 2 f 4.0 + 2 g 4.6 + 2 c 5.0 + + 2 a 5.3'),header=TRUE)> closeAllConnections() > require(sqldf) > sqldf("select V1, V2, max(V3) from dat group by V1, V2")V1 V2 max(V3) 1 1 a 7.0 2 1 b 2.0 3 1 c 5.5 4 1 d 6.0 5 2 a 5.3 6 2 c 5.0 7 2 f 4.0 8 2 g 4.6>On Thu, Oct 7, 2010 at 11:39 AM, wangguojie2006 <wangguojie2006 at gmail.com> wrote:> > Dear fellows, > > I've been working this problem for a day and still can't see where the > problem is. I really appreciate if anyone can help me out. > > My data is like: > > V1 ? ?V2 ? ? v3 > 1 ? ? ?a ? ? ? 1.3 > 1 ? ? ?a ? ? ? 1.5 > 1 ? ? ?b ? ? ? 2.0 > 1 ? ? ?a ? ? ? 2.3 > 1 ? ? ?a ? ? ? 3.4 > 1 ? ? ?c ? ? ? 5.5 > 1 ? ? ?d ? ? ? 6.0 > 1 ? ? ?a ? ? ? 7.0 > 2 ? ? ?f ? ? ? ?1.5 > 2 ? ? ?g ? ? ? ?1.6 > 2 ? ? ?f ? ? ? ? 3.4 > 2 ? ? ?f ? ? ? ? 4.0 > 2 ? ? ?g ? ? ? ?4.6 > 2 ? ? ?c ? ? ? ?5.0 > 2 ? ? ?a ? ? ? ?5.3 > > What I want to do is to get the max value of V3 for each unique V2 in each > V1. For example, when V1=1, unique V2 is (a, b, c, d), and corresponding max > value of V3 is (7.0, 2.0, 5.5, 6.0). And V1=2, unique V2 is (f, g, c, a) and > max value of V3 is (4.0, 4.6, 5.0, 5.3). Then combinne these max values. > > My code is like: > > z<-NULL > x<-NULL;y<-NULL > for (j in 1:2) > { > ?x<-unique(V2[V1==j]) > ? for (i in 1:length(x)) > ? ?{ > ? ? y[i]<-max(V3[V2==x[i] & V1==j]) > ? ?} > ?z<-c(z,y) > } > length(z) > > My problem is the length of z is much bigger than the length of I'm supposed > to get, like above data, I should only get 8 max values. But the code will > return me a number bigger than 8. I'm thinking there might be some > overlapping problem. Can someone help? > > Thank you for your time. > > Best, > > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Need-help-for-loop-code-thanks-tp2967036p2967036.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?