[R] Need help for loop code, thanks
wangguojie2006
wangguojie2006 at gmail.com
Thu Oct 7 17:39:33 CEST 2010
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.
More information about the R-help
mailing list