R 从包含逗号分隔文本的csv单元格中获取唯一值
我有一个csv文件,在某个单元格中包含多个值R 从包含逗号分隔文本的csv单元格中获取唯一值,r,csv,R,Csv,我有一个csv文件,在某个单元格中包含多个值 test <- c("a, b", "c", "d", "e, f", "g") data.frame(test) test 1 a, b 2 c 3 d 4 e, f 5 g 然而,我希望它是这样的 [1] "a" "b" "c" "d"
test <- c("a, b", "c", "d", "e, f", "g")
data.frame(test)
test
1 a, b
2 c
3 d
4 e, f
5 g
然而,我希望它是这样的
[1] "a" "b" "c" "d" "e" "f" "g"
我们可以拆分“测试”并获得唯一的
unique(unlist(strsplit(test, ",\\s*")))
#[1] "a" "b" "c" "d" "e" "f" "g"
在
tidyverse
中,我们还可以
library(tibble)
library(dplyr)
library(tidyr)
tibble(col1 = test) %>%
separate_rows(col1) %>%
distinct
library(tibble)
library(dplyr)
library(tidyr)
tibble(col1 = test) %>%
separate_rows(col1) %>%
distinct