For循环以消除多个dfs中的列

For循环以消除多个dfs中的列,r,for-loop,dplyr,R,For Loop,Dplyr,我有大约10个数据帧。例如,这里有两个: name <- c("A", "B", "C") name.footnote <- c("this", "that", "the other") class <- c("one", "two", "three") class.footnote <- c("blank", "blank", "blank") df1 <- data.frame(name, name.footnote, class, class.footnot

我有大约10个数据帧。例如,这里有两个:

name <- c("A", "B", "C")
name.footnote <- c("this", "that", "the other")
class <- c("one", "two", "three")
class.footnote <- c("blank", "blank", "blank")

df1 <- data.frame(name, name.footnote, class, class.footnote)
df2 <- data.frame(name, name.footnote, class, class.footnote)
请参阅下面我尝试过的许多循环代码中的一些。我错过了什么

listofDfs <- list("df1","df2")
一,

二,


在定义列表时请尝试删除引号。在定义列表时请删除引号。很好,我明白了。将其作为答案提交,以便我标记所回答的问题?您可以将其简化为librarytidyverse;listdf1,df2%>%mapselect,-以“脚注”结尾。如果愿意,可以使用Lappy而不是purrr::map,或者尝试map_df以方便简化为data.frame,但在这种情况下,listdf1、df2%>%bind_rows%>%select-ends_和“footnote”也可以。同一个问题循环不如单独的行有效,不同的错误。通过这些循环,两个dfs组合在一起,而不是保存为唯一的dfs。新的dfs应替换旧的df1和df2。是否要在您的全局环境中替换它们?还是在列表中?还有什么不同的错误呢?是的,我想在我的全球环境中替换它们,而不是在列表中。没有实际的错误,我选词不好。只是仍然没有完全返回所需的结果。请参见编辑,了解如何在全局中按名称而不是从对象列表中使用对象使用df1、df2、…,dfn在R中是曲折的。您的代码将充满赋值和粘贴DF、n和一般尴尬。通常最好使用列表来避免所有这些问题。
Error in UseMethod("select_") :   no applicable method for 'select_'
 applied to an object of class "character".
listofDfs <- list("df1","df2")
lapply(listofDfs, function(df){
  df <- select(df, -ends_with("footnote"))
  return(df)
  }
)
for (i in listofDfs){
  i <- select(i, -ends_with("footnote"))
}
library(dplyr)

listofDfs <- list(df1,df2)

#using lapply
list_out1 <- lapply(listofDfs, function(df){
  df <- select(df, -ends_with("footnote"))
  return(df)
})

#using for loop
list_out2 <- vector("list", length(listofDfs))
for (i in seq_along(listofDfs)){
  list_out2[[i]] <- select(listofDfs[[i]], -ends_with("footnote"))
}
listofDfs <- list('df1','df2')

invisible(lapply(listofDfs, function(i){
  df <- select(get(i, globalenv()), -ends_with("footnote"))
  assign(i, df, envir = globalenv())
}))

for (i in listofDfs){
  df <- select(get(i, globalenv()), -ends_with("footnote"))
  assign(i, df, envir = globalenv())
}