subset vector by first letter in R

前端 未结 2 846
小蘑菇
小蘑菇 2020-12-16 20:01

I am working in R and I have a character vector. I would like to subset this vector by the first letter of the string of characters. So, for example, how can I subset the ve

相关标签:
2条回答
  • 2020-12-16 20:38

    You could also use substr with tapply to get a list of all types:

    tapply(vector,toupper(substr(vector,1,1)),identity)
    
    $A
    [1] "apple" "Actor"
    
    $B
    [1] "banana"
    
    $F
    [1] "fox"
    
    0 讨论(0)
  • 2020-12-16 20:53

    you can use grep:

    vector = c("apple", "banana", "fox", "Actor")
    vector[grep("^[aA].*", vector)]
    
    [1] "apple" "Actor"
    
    0 讨论(0)
提交回复
热议问题