本文介绍了对数据列表中的多个列执行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我在列表中有以下数据帧:
Suppose I have the following data frames in a list:
df1 <- data.frame(x = runif(3), y = runif(3))
df2 <- data.frame(x = runif(3), y = runif(3))
df.list <- list(df1, df2)
现在假设我想添加x和y列以获得z列我知道在带有mutate的数据框中执行此操作很容易:
Now suppose I want to add column x and y to get column zI know to do this in a dataframe with mutate is as easy as:
dplyr::mutate(lapply(df.list, z = x + y))
如何使用lapply对列表中的多个列执行操作?
How do I perform operations on multiple columns in a list using lapply?
推荐答案
我们可以将transform
与lapply
lapply(df.list, transform, z= x+y)
如果我们需要对多个列执行此操作,
If we need to do this for multiple columns,
lapply(df.list, transform, z= x+y, w= x*y)
另一个选择是使用library(purr)
(来自dplyr
的作者)
Another option would be using library(purr)
(from the authors of dplyr
)
library(dplyr)
library(purrr)
df.list %>%
map(mutate, z=x+y, w= z*y)
这篇关于对数据列表中的多个列执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!