我想将一个函数应用于数据框列表。该函数从每个数据框中的两列中获取元素,然后将输出添加到新列中的每个数据框中。
创建虚拟数据:
df.1 <- data.frame(data=cbind(rnorm(5, 0), rnorm(5, 2), rnorm(5, 5)))
df.2 <- data.frame(data=cbind(rnorm(5, 0), rnorm(5, 2), rnorm(5, 5)))
names(df.1) <- c("a", "b", "c")
names(df.2) <- c("a", "b", "c")
ls.1<- list(df.1,df.2)
names(ls.1) <- c("cat", "dog")
ls.1
查看数据:
> ls.1
$cat
a b c
1 0.7031868 1.730499 4.286386
2 0.1527551 2.794084 4.348707
3 1.1151157 0.154562 4.647605
4 0.5786497 1.407386 4.118078
5 0.9223104 2.995469 5.065981
$dog
a b c
1 0.04024872 1.6760609 5.013490
2 0.18095899 2.1015250 3.452313
3 -0.86588484 2.1371948 6.389203
4 -0.39499567 0.5996709 5.399724
5 -1.31850123 3.0058084 5.530989
我想做的伪代码:
my.fun <- function(b, c) {
out.put <- b + c
ls.1[i]$d <- out.put
}
我希望输出看起来像:
> ls.1
$cat
a b c d
1 0.7031868 1.730499 4.286386 6.689551
2 0.1527551 2.794084 4.348707 5.553838
3 1.1151157 0.154562 4.647605 8.526398
4 0.5786497 1.407386 4.118078 5.999395
5 0.9223104 2.995469 5.065981 8.536797
$dog
a b c d
1 0.04024872 1.6760609 5.013490 6.689551
2 0.18095899 2.1015250 3.452313 5.553838
3 -0.86588484 2.1371948 6.389203 8.526398
4 -0.39499567 0.5996709 5.399724 5.999395
5 -1.31850123 3.0058084 5.530989 8.536797
我认为应该可以通过mapply或类似的东西轻松实现,但我无法理解......
答案 0 :(得分:7)
lapply
在这里运作正常。请注意,此处需要return(x)
,否则我们只会返回新的向量。
res <- lapply(ls.1, function(x){
x$d <- x$b + x$c
return(x)
})
答案 1 :(得分:3)
lapply
您对每个列表项的功能。该函数应添加两列,然后返回更改的对象:
my.fun <- function(data,x,y,out) {data[out] <- data[[x]] + data[[y]]; data}
lapply(ls.1, my.fun, x="b", y="c", out="d")
#$cat
# a b c d
#1 -0.3762835 0.8775672 3.599283 4.476851
#2 1.4728001 2.2384841 3.093956 5.332440
#...
#
#$dog
# a b c d
#1 -0.7669203 2.8219880 4.284088 7.106076
#2 0.4413280 1.1619882 4.440192 5.602180
#...
答案 2 :(得分:3)
以下是使用for循环的替代解决方案
for (e in names(ls.1)) {
ls.1[[e]]$d <- ls.1[[e]]$b + ls.1[[e]]$c
}
$cat
a b c d
1 1.2720987 2.03251108 4.395936 6.428447
2 0.1048982 1.47652374 3.556955 5.033479
3 0.1332581 2.97334172 5.262912 8.236254
4 0.8064055 2.44649336 6.605434 9.051927
5 -1.5200194 -0.08463335 4.279925 4.195292
$dog
a b c d
1 0.73350259 1.133349 5.223977 6.357326
2 0.40957903 1.431519 5.332924 6.764442
3 -0.92179742 3.778318 6.924693 10.703011
4 1.92143657 1.055262 4.318544 5.373806
5 -0.07847994 2.045227 6.311894 8.357121