[R] use apply function with which

Charles C. Berry cberry at tajo.ucsd.edu
Mon Aug 27 22:48:23 CEST 2007


On Mon, 27 Aug 2007, schuurmans at geo.uu.nl wrote:

> 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'))

 	You have a syntax error in the previous line - '))'


> 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')

........^................^^^...

You use X twice in this expression. If you delete 'X=X,' and revise 
which.search to

  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 ] ) ) ]

to get rid of the $ operator which is deprecated for atomic vectors,

(and fix the above syntax error) then this expression agrees with 'result'

If you know that the matches are unique (only one row in Y will match any 
row of X), then

 	match( paste( X$c1, X$c2 ) , paste( Y$V1, Y$V2 ))

will be fast.

If nrow(Y) is small,

 	which(
 		outer(Y$V1, as.character(X$c1), "==" ) &
 			outer(Y$V2, as.character(X$c2), "==" ),
 	      arr.ind = TRUE )

will also be quick.


Otherwise something like

 	unlist( lapply( paste( X$c1, X$c2 ), match, paste( Y$V1, Y$V2 )) )

may be a good bet.


Please learn to use the space key to format your code in a more readable 
fashion!


HTH,

Chuck

>
> ###
>> 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"
>
> ______________________________________________
> 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
> and provide commented, minimal, self-contained, reproducible code.
>

Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901



More information about the R-help mailing list