Skip to content

apply one list to another one

3 messages · Hui Du, Rui Barradas, David Winsemius

#
Hello,


Hui Du wrote
If it's a list and you want to avoid a loop, think about lapply or similar.
(Tip: you don't want sapply.)


?lapply
lapply(names(a), function(i) subset(a[[i]], select = b[[i]]))

It's your code, but "lapplied" to all elements of your objects.
Hope this helps,

Rui Barradas


--
View this message in context: http://r.789695.n4.nabble.com/apply-one-list-to-another-one-tp4607386p4607453.html
Sent from the R help mailing list archive at Nabble.com.
#
On May 3, 2012, at 7:39 PM, Hui Du wrote:

            
I'm assuming the is a typo and you wanted
  a[["1,2"]] become subset(a[["1.2"]], select = c("B", "C"))
using lapply is a loop construct but maybe it won't feel that way:

 > lapply( names(a), function(x) subset(a[[x]],  select=b[[x]]) )
[[1]]
               A           B
1  -0.660594340 -0.44402746
2   0.926022768  0.50906870
3  -0.357588460 -0.10657540
4   0.226316328  1.96415323
5   0.143330486 -0.89655890
6  -0.307193955 -1.10708473
7  -0.904927213  2.01289010
8  -1.922708953  0.74302970
9   1.008833376  0.90442422
10  1.095004836  1.23202474
11 -0.568905764  0.20946675
12  0.621113819  0.25753637
13  0.001997838  0.41691655
14 -0.206525640  0.94883919
15 -0.665738673  1.29442907
16 -1.225075753  0.49222447
17  0.224294111 -0.51241903
18 -0.170286569  0.09245769
19  0.396839258  0.23253796
20  0.645889199  0.52546631

[[2]]
              B           C
1  -0.44402746 -0.05013952
2   0.50906870  0.32247682
3  -0.10657540 -0.54740895
4   1.96415323  0.63823531
5  -0.89655890  0.54520142
6  -1.10708473 -0.14625567
7   2.01289010 -0.97021834
8   0.74302970  0.49252268
9   0.90442422  0.30610560
10  1.23202474 -0.52001742
11  0.20946675 -0.96469545
12  0.25753637  0.22606727
13  0.41691655  0.48728976
14  0.94883919 -0.99520018
15  1.29442907  0.60930763
16  0.49222447 -0.23321555
17 -0.51241903 -0.72746372
18  0.09245769 -1.96009906
19  0.23253796 -0.14303924
20  0.52546631  2.12944010

--
David Winsemius, MD
Heritage Laboratories
West Hartford, CT