将列作为数字传递到单独的 R

Pass column as number into separate R

是否可以将 col 变量作为数字传递给 separate 函数?

?separate col 定义为 Bare column name.

Dataframe <- data.frame(Time = rep("1:00 AM", times = 20), Place = rep("Wawa", times = 20))
Dataframe2 <- data.frame(Time2 = rep("1:00 AM", times = 20), Place = rep("Wawa", times = 20))
Data <- list(Dataframe , Dataframe2)

Dataframe %>% separate(col = Time, into = c("col_name_1NEW", "col_name_2NEW"), sep =" ") #works

Dataframe2 %>% separate(col = Time2, into = c("col_name_1NEW", "col_name_2NEW"), sep =" ") #works

for(i in 1:length(Data)){
Data[[i]] %>% separate(col = Time, into = c("col_name_1NEW", "col_name_2NEW"), sep =" ")
} #returns error because of wrong colum name for Data[[2]]

我想通过将 col 参数作为列号而不是名称来使其更通用,例如:

for(i in 1:length(Data)){
    Data[[i]] %>% separate(col = 1, into = c("col_name_1NEW", "col_name_2NEW"), sep =" ")
    }

尝试separate_将列名称作为字符串传递:

lapply(Data, function(i){
  separate_(i, col = colnames(i)[1],
            into = c("col_name_1NEW", "col_name_2NEW"),
            sep = " ")
  })