r - apply calculation on multiple columns of each element in a list -
i have list consist of 23 elements 69 rows , 13 columns each. , need apply calculation on multiple columns each element of list.
as simple example, list looks this:
>list >$`1` > b c >1 2.1 1.4 3.4 >2 4.4 2.6 5.5 >3 2.6 0.4 3.0 ... >$`2` > b c >70 5.1 4.9 5.1 >71 4.4 7.6 8.5 >72 2.8 3.5 6.8 ...
what wish z = (a-b) / c
each element ($1
,$2
..., $23
)
i tried following code:
for( in 1:23) {
z = (list[[i]]$a - list[[i]]$b) / list[[i]]$c }
which gave me 49 values, rather 1566 values.
anyone have idea wrong code , able correct it? thank much!
you can function lapply()
. here example assuming in data frame columns have same name.
ll<-list(data.frame(a=1:3,b=4:6,c=7:9), data.frame(a=1:3,b=6:4,c=7:9), data.frame(a=1:3,b=4:6,c=9:7)) lapply(ll, with, (a-b)/c)
Comments
Post a Comment