[R] dataframes of unequal size
Liaw, Andy
andy_liaw at merck.com
Tue Jul 19 14:50:18 CEST 2005
Seems like no one had responded to this one yet, so I'll take a stab:
## Generate some bogus data:
set.seed(45)
dat <- cbind(expand.grid(LETTERS[1:2], 1:3), round(runif(6), 2))
names(dat) <- c("state", "psu", "weight")
dat2 <- data.frame(state=sample(c("A", "B"), 100, replace=TRUE),
psu=sample(3, 100, replace=TRUE),
weight=rep(0, 100))
## The actual work:
split(dat2$weight, interaction(dat2$state, dat2$psu)) <-
split(dat$weight, interaction(dat$state, dat$psu))
This, I think, will only work correctly if all state/psu combinations in
your "C" are also present in "C1". If not, you can just augment "C1" to
include them.
HTH,
Andy
> From: Renuka Sane
>
> 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
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>
>
>
More information about the R-help
mailing list