本文介绍了将tidyr ::应用于多个列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想遍历数据帧中的列,并根据分隔符将它们拆分为.我正在使用tidyr::separate
,当我一次执行一列时,它可以工作.
I would like to iterate over columns in a dataframe and split them into the based on a separator. I am using tidyr::separate
, which works when I do one column at a time.
例如:
df<- data.frame(a = c("5312,2020,1212"), b = c("345,982,284"))
df <- separate(data = df, col = "a",
into = paste("a", c("col1", "col2", "col3"),
sep = "_"), sep = ",")
返回:
a_col1 a_col2 a_col3 b
1 5312 2020 1212 345,982,284
当我尝试对df
R的每一列执行相同的操作时,都会返回错误
When I try to execute the same operation over each column of df
R returns an error
例如,我将其用于循环:
For example I used this for loop:
for(col in names(df)){
df <- separate(data = df, col = col,
into = paste(col, c("col1", "col2", "col3),
sep = "_"), sep = ",")
}
我期望得到以下输出:
a_col1 a_col2 a_col3 b_col1 b_col2 b_col3
1 5312 2020 1212 345 982 284
但是R返回此错误:
Error in if (!after) c(values, x) else if (after >= lengx) c(x, values) else c(x[1L:after], :
argument is of length zero
还有另一种方法可以将tidyr::separate
应用于数据帧中的多个列吗?
Is there another way to apply tidyr::separate
over multiple columns in a data frame?
推荐答案
您可以将自定义的separate_()
调用提供给Reduce()
.
You could feed a customized separate_()
call into Reduce()
.
sep <- function(...) {
dots <- list(...)
n <- stringr::str_count(dots[[1]][[dots[[2]]]], "\\d+")
separate_(..., into = sprintf("%s_col%d", dots[[2]], 1:n))
}
df %>% Reduce(f = sep, x = c("a", "b"))
# a_col_1 a_col_2 a_col_3 b_col_1 b_col_2 b_col_3
# 1 5312 2020 1212 345 982 284
否则,cSplit
也会这样做.
splitstackshape::cSplit(df, names(df))
# a_1 a_2 a_3 b_1 b_2 b_3
# 1: 5312 2020 1212 345 982 284
这篇关于将tidyr ::应用于多个列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!