[R] dataframes of unequal size
Renuka Sane
renukas at gmail.com
Mon Jul 18 11:38:05 CEST 2005
I have two dataframes C and C1. Each has three columns viz. state, psu
and weight. The dataframes are of unequal size i.e. C1 could be
2/25/50 rows and C has 42000 rows. C1 is the master table i.e.
C1$state, C1$psu and C1$weight are never the same. ThisA. P., Urban, 0
is not so for C.
For example
C
state, psu,weight
A. P., Urban, 0
Mah., Rural, 0
W.B., Rural,0
Ass., Rural,0
M. P., Urban,0
A. P., Urban, 0
...
C1
state, psu, weight
A. P., Urban, 1.3
A. P., Rural, 1.2
M. P., Urban, 0.8
......
For every row of C, I want to check if C$state==C1$state and
C$psu==C1$psu. If it is, I want C$weight <- C1$weight, else C$weight
should be zero.
I am doing the following
for( i in 1:length(C$weight)) {
C$w[C$state[i]==C1$state & C$psu[i]==C1$psu] <- C1$w[C$state[i] ==
C1$state & C$psu[i] == C1$psu]
}
This gives me the correct replacements for the number of rows in C1
and then just repeats the same weights for the remaning rows in C.
Can someone point out the error in what I am doing or show the correct
way of doing this?
Thanks,
Renuka
More information about the R-help
mailing list