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

时间:2017-04-20 19:14:48

标签: r dplyr data-cleaning

是否可以将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 =" ")
    }

1 个答案:

答案 0 :(得分:3)

尝试the_content()将列名称作为字符串传递:

trigger_error("Some info",E_USER_ERROR);