[R] lapply

From: Weiwei Shi <helprhelp_at_gmail.com>
Date: Sat 02 Jul 2005 - 05:22:35 EST


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

______________________________________________
R-help@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
Received on Sat Jul 02 05:26:15 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:33:11 EST