[R] lapply

Weiwei Shi helprhelp at gmail.com
Fri Jul 1 21:22:35 CEST 2005


Hi, all:
I have a program here but it runs slow and I am wondering if there is
some place I can change to make it run faster.

Two lists, scd and c1, like this:
> scd[1:2]
[[1]]
[1] "54"  "241"

[[2]]
[1] "52" "53"
...
> c1[1:2]
[[1]]
 [1] "13"  "30"  "92"  "93"  "13"  "94"  "30"  "95"  "96"  "97"  "98"  "99"
[13] "8"   "19"  "31"  "100" "101" "29"

[[2]]
[1] "13" "55"

> length(scd)
[1] 2542
> length(c1)
[1] 31859

My target is 
for each in scd, I need to know how many times it (as the whole) occur in c1.

My code is
N <- length(scd) # num of word_comb
M <- length(c1) # num of class1
g1 <- lapply(1:N, function(i) lapply(1:M, function(j) all(scd[[i]]
%in% c1[[j]])))
a <- lapply(1:N, function(i) sum(g1[[i]]==T))

My questions:
1. g1's calc is very slow
2. how to do the following using apply:
tab <- array(as.integer(0), dim=c(2,2,N)
for (i in 1:N){
    tab[2,1,i] <- a[[i]]
}
tab[2,2,]=M-tab[2,1,]

Thanks,
-- 
Weiwei Shi, Ph.D

"Did you always know?"
"No, I did not. But I believed..."
---Matrix III




More information about the R-help mailing list