我正在尝试使用 lapply 将函数应用于某些数据帧。 我有一个包含我想要使用的数据帧名称的列表。 问题是,当我在列表上使用 lapply 时,它会给我一个错误,因为它试图将该函数应用于字符向量而不是对象。如果可能的话,我想留在 R 基地(没有 rlang 等)
谢谢您的帮助!
示例:
#using dataset iris included in R
data1 <- iris
data2 <- iris
data3 <- iris
list1<-c("data1","data2","data3")
print_func<-function(x){
print(x[1,1]) #print the first object of each dataframe
}
lapply(list1,print_func)
Error in x[1, 1] : incorrect number of dimensions
print_func(data1)
[1] 5.1 # works because its evaluating the list obj
print_func <- function(df_name){
df <- get(df_name)
print(df[1, 1])
}
lapply(list1, print_func)
[1] 5.1
[1] 5.1
[1] 5.1
[[1]]
[1] 5.1
[[2]]
[1] 5.1
[[3]]
[1] 5.1