[英]how to refer specific column of an entire list of dataframes in R function?
I have a multitude of dataframes in R that are like this & I know how to modify an entire column.我在 R 中有大量这样的数据框,我知道如何修改整个列。
id fire earth water
1 5 7 9
2 23 34 54
3 1 2 3
df$fire <- (df$fire-5.5)/4.5
Im having a hard time finding the specific syntax to refer to this specific column but of every df (lets say fire)我很难找到具体的语法来引用这个特定的列,但是每个 df (比如说火)
df.list <- list(df1,df2,df3)
fire_function <- function(){(.$fire-5.5)/4.5}
lapply(df.list, fire_function)
Sorry if this question was already asked, i searched and couldn't find how to refer a specific column of all dataframes.抱歉,如果已经问过这个问题,我搜索并找不到如何引用所有数据框的特定列。 Maybe my apply function is not appropriate either.
也许我的申请 function 也不合适。
thank you谢谢你
We can use transform
我们可以使用
transform
df.list <- list(df1 = df1, df2 = df2, df3 = df3)
df.list <- lapply(df.list, transform, fire = (fire - 5.5)/4.5)
If we need to update the original datasets, use list2env
如果我们需要更新原始数据集,请使用
list2env
list2env(df.list, .GlobalEnv)
Or we may need to assign and then return the data或者我们可能需要先赋值然后返回数据
df.list <- lapply(df.list, function(x) {
x$fire <- (x$fire - 5.5)/4.5
x})
Or create the fire_function
as或将
fire_function
创建为
fire_function <- function(x) {
x$fire <- (x$fire - 5.5)/4.5
x
}
df.list <- lapply(df.list, fire_function)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.