[R] use apply function with which
schuurmans at geo.uu.nl
schuurmans at geo.uu.nl
Mon Aug 27 21:13:07 CEST 2007
Dear R-users,
For a data frame (say in this example X) I want to look up the
corresponding value in a 'look-up data frame' (in this example Y). The
for-loop works but is very time-consuming because 'X' in reality is very
big.
Therefore I would like to have a solution with apply. However, I do not
succeed. Any suggestions?
Thanks in advance,
Hanneke
c1=c('a','a','b')
c2=c('j','k','k')
V1=c('a','a','a','a','b','b','b','b'))
V2=c('i','j','k','l','i','j','k','l')
V3=c(4,3,2,1,8,5,2,-1)
X=NULL
X$c1=c1
X$c2=c2
X=as.data.frame(X)
Y=NULL
Y$V1=V1
Y$V2=V2
Y$V3=V3
Y=as.data.frame(Y)
result=NULL
for (i in 1:dim(X)[1])
{
result=rbind(result, Y$V3[which(Y$V1==as.character(X[i,]$c1) &
Y$V2==as.character(X[i,]$c2))])
}
#######
which.search=function(X,Y,c1,c2,V1,V2,V3)
Y$V3[which(Y$V1==as.character(X$c1) & Y$V2==as.character(X$c2))]
apply(X,1,which.search,X=X,Y=Y,c1='c1',c2='c2',V1='V1',V2='V2',V3='V3')
###
> sessionInfo()
R version 2.5.1 (2007-06-27)
i386-pc-mingw32
locale:
LC_COLLATE=Dutch_Netherlands.1252;LC_CTYPE=Dutch_Netherlands.1252;LC_MONETARY=Dutch_Netherlands.1252;LC_NUMERIC=C;LC_TIME=Dutch_Netherlands.1252
attached base packages:
[1] "stats" "graphics" "grDevices" "utils" "datasets" "methods"
"base"
More information about the R-help
mailing list