r - Create data.frame conditional on another df without for loop -
i'm trying create data.frame takes different values depending on value of reference data.frame. know how "for loop", have been advised avoid loops in r... , actual data have ~500,000 rows x ~200 columns.
a <- as.data.frame(matrix(rbinom(10,1,0.5),5,2,dimnames=list(c(1:5),c("a","b")))) b <- data.frame(v1=c(2,10,12,5,11,3,4,14,2,13),v2=c("a","b","b","a","b","a","a","b","a","b")) c <- as.data.frame(matrix(0,5,2)) (i in 1:5){ for(j in 1:2){ if(a[i,j]==1){ c[i,j] <- mean(b$v1[b$v2==colnames(a)[j]]) } else { c[i,j]= mean(b$v1) }}} c
i create data.frame "c" based on value in each cell, , corresponding column name, of data.frame "a". there way this? indexing? using data.table? maybe apply functions? , appreciated!
(a == 0) * mean(b$v1) + t(t(a) * c(tapply(b$v1, b$v2, mean)))
run in pieces understand what's happening. also, note assumes ordered names in a
(and 0's , 1's entries in it, per op).
an alternative bunch of t
's above using mapply
(this assumes a
data.frame
or data.table
, not matrix
, while above doesn't care):
(a == 0) * mean(b$v1) + mapply(`*`, a, tapply(b$v1, b$v2, mean))
Comments
Post a Comment