从名称列创建首字母缩写列作为R

时间:2018-11-05 19:34:42

标签: r

这是一个示例df,我想从中提取大写字母:

df <- data.frame(a = c(1:5), b = c("Words are Here", "Okay", "Be Good Now", "WTF are you Doing", "it's good"))

使用str_extract_all(df$b, "[A-Z]", simplify = )时,我会得到所有大写字母的列表 使用simple = TRUE:

    [,1] [,2] [,3] [,4]
[1,] "W"  "H"  ""   ""  
[2,] "O"  ""   ""   ""  
[3,] "B"  "G"  "N"  ""  
[4,] "W"  "T"  "F"  "D" 
[5,] ""   ""   ""   ""  

或用simple = FALSE:

[[1]]
[1] "W" "H"

[[2]]
[1] "O"

[[3]]
[1] "B" "G" "N"

[[4]]
[1] "W" "T" "F" "D"

[[5]]
character(0)

我不知道如何采用这些方法来获得所需的输出,该输出是像这样的向量:

("WH", "O", "BGN", "WTFD", "")

1 个答案:

答案 0 :(得分:2)

我们可以使用gsub来匹配一个或多个不是大写字母的字符,然后将其替换为空白(""

gsub("[^A-Z]+", "", df$b)
#[1] "WH"   "O"    "BGN"  "WTFD" ""