如何从字符类型的向量中提取唯一字符?

时间:2017-11-07 23:27:31

标签: r

所以我有一个如下的矢量:

log4j.category.io.vertx = TRACE

我想输出所有这些字符串中使用的字母列表,但这些字母在最终输出中只显示一次。

如何做到这一点?

谢谢!

2 个答案:

答案 0 :(得分:0)

这是你要找的吗?

library(tidyverse)

test %>% 
  str_extract_all(boundary("character")) %>% 
  unlist() %>% 
  unique()

返回:

 [1] "A" "S" "F" "G" "H" "P" "W" "E" "Y" "N" "T" "R" "K" "V"

可替换地:

test %>% 
  str_extract_all(boundary("character")) %>% 
  unlist() %>% 
  as_tibble() %>% 
  count(value)

# A tibble: 14 x 2
   value     n
   <chr> <int>
 1     A     5
 2     E     2
 3     F     3
 4     G     2
 5     H     1
 6     K     1
 7     N     1
 8     P     1
 9     R     2
10     S    12
11     T     3
12     V     1
13     W     1
14     Y     1

答案 1 :(得分:0)

任何方式

test <- c("ASSFGH","PSSWEE","ASSYNT","ASSFRK","ASSFGT","ASSRTV")

# create vector of letters used
individual_characters = unique(unlist(strsplit(Reduce(function(x, y) paste0(x, y), test), '')))
# combine into one string
Reduce(function(x,y) paste0(x,y), individual_characters)