Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 从包含逗号分隔文本的csv单元格中获取唯一值_R_Csv - Fatal编程技术网

R 从包含逗号分隔文本的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"

我有一个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" "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