我在使用mutate{dplyr}
函数以向数据框添加新列时遇到问题。我希望一个新列是字符类型,并且包含来自其他列的排序单词的“concat”(也是字符类型)。例如,对于以下数据框:
> library(datasets)
> states.df <- data.frame(name = as.character(state.name),
+ region = as.character(state.region),
+ division = as.character(state.division))
>
> head(states.df, 3)
name region division
1 Alabama South East South Central
2 Alaska West Pacific
3 Arizona West Mountain
我想获得一个包含以下第一个元素的新列:
"Alamaba_East South Central_South"
我试过这个:
mutate(states.df,
concated_column = paste0(sort(name, region, division), collapse="_"))
但我收到一个错误:
Error in sort(1:50, c(2L, 4L, 4L, 2L, 4L, 4L, 1L, 2L, 2L, 2L, 4L, 4L, :
'decreasing' must be a length-1 logical vector.
Did you intend to set 'partial'?
感谢您提前提供任何帮助!