合并列并按字母顺序排列其内容
Merging Columns and Alphabetizing Their Contents
我有一个名为 issuesdata
的数据框,它是我从一个更大的框架 clean.data
创建的
issue1 <- as.vector(clean.data$issue1)
issue2 <- as.vector(clean.data$issue2)
issue3 <- as.vector(clean.data$issue3)
issuesdata <- data.frame(issue1, issue2, issue3)
issuesdata %>% dplyr::slice(10:15)
issue1 issue2 issue3
1 economic <NA> <NA>
2 economic unification <NA>
3 economic <NA> <NA>
4 transportation aviation <NA>
5 justice <NA> <NA>
6 slavery economic humanrights
我有两个目标:
- 将这些列合并在一起,这样第四列就包含了一个字符串中的所有问题(列名:
allissues
)
allissues
中的问题文本按字母顺序排列
例如allissues
的第2行会保持economic unification
的形式,但是是一个字符串。第 4 行是 Aviation Transportation
,而第 6 行是 economic humanrights slavery
.
我该怎么做?
按行删除 NA 值,对它们进行排序并将它们粘贴在一起。
在基础 R 中:
issuesdata$combine <- apply(issuesdata, 1, function(x)
toString(sort(na.omit(x))))
或 dplyr
:
library(dplyr)
issuesdata %>%
rowwise() %>%
mutate(combine = toString(sort(na.omit(c_across()))))
# issue1 issue2 issue3 combine_data
# <chr> <chr> <chr> <chr>
#1 economic NA NA economic
#2 economic unification NA economic, unification
#3 economic NA NA economic
#4 transportation aviation NA aviation, transportation
#5 justice NA NA justice
#6 slavery economic humanrights economic, humanrights, slavery
我有一个名为 issuesdata
的数据框,它是我从一个更大的框架 clean.data
issue1 <- as.vector(clean.data$issue1)
issue2 <- as.vector(clean.data$issue2)
issue3 <- as.vector(clean.data$issue3)
issuesdata <- data.frame(issue1, issue2, issue3)
issuesdata %>% dplyr::slice(10:15)
issue1 issue2 issue3
1 economic <NA> <NA>
2 economic unification <NA>
3 economic <NA> <NA>
4 transportation aviation <NA>
5 justice <NA> <NA>
6 slavery economic humanrights
我有两个目标:
- 将这些列合并在一起,这样第四列就包含了一个字符串中的所有问题(列名:
allissues
) allissues
中的问题文本按字母顺序排列
例如allissues
的第2行会保持economic unification
的形式,但是是一个字符串。第 4 行是 Aviation Transportation
,而第 6 行是 economic humanrights slavery
.
我该怎么做?
按行删除 NA 值,对它们进行排序并将它们粘贴在一起。
在基础 R 中:
issuesdata$combine <- apply(issuesdata, 1, function(x)
toString(sort(na.omit(x))))
或 dplyr
:
library(dplyr)
issuesdata %>%
rowwise() %>%
mutate(combine = toString(sort(na.omit(c_across()))))
# issue1 issue2 issue3 combine_data
# <chr> <chr> <chr> <chr>
#1 economic NA NA economic
#2 economic unification NA economic, unification
#3 economic NA NA economic
#4 transportation aviation NA aviation, transportation
#5 justice NA NA justice
#6 slavery economic humanrights economic, humanrights, slavery