First entry from string split

后端 未结 5 1966
陌清茗
陌清茗 2020-12-03 04:30

I\'ve got a column people$food that has entries like chocolate or apple-orange-strawberry.

I want to split people$food

相关标签:
5条回答
  • 2020-12-03 05:18

    dplyr/magrittr approach:

    library(magrittr)
    library(dplyr)
    
    word = c('apple-orange-strawberry', 'chocolate')
    
    strsplit(word, "-") %>% sapply(extract2, 1)
    # [1] "apple"     "chocolate"
    
    0 讨论(0)
  • 2020-12-03 05:19

    I would use sub() instead. Since you want the first "word" before the split, we can simply remove everything after the first - and that's what we're left with.

    sub("-.*", "", people$food)
    

    Here's an example -

    x <- c("apple", "banana-raspberry-cherry", "orange-berry", "tomato-apple")
    sub("-.*", "", x)
    # [1] "apple"  "banana" "orange" "tomato"
    

    Otherwise, if you want to use strsplit() you can round up the first elements with vapply()

    vapply(strsplit(x, "-", fixed = TRUE), "[", "", 1)
    # [1] "apple"  "banana" "orange" "tomato"
    
    0 讨论(0)
  • 2020-12-03 05:28

    I would suggest using head rather than [ in R.

    word <- c('apple-orange-strawberry','chocolate')
    sapply(strsplit(word, "-"), head, 1)
    # [1] "apple"     "chocolate"
    
    0 讨论(0)
  • 2020-12-03 05:30

    For example

    word <- 'apple-orange-strawberry'
    
    strsplit(word, "-")[[1]][1]
    [1] "apple"
    

    or, equivalently

    unlist(strsplit(word, "-"))[1].
    

    Essentially the idea is that split gives a list as a result, whose elements have to be accessed either by slicing (the former case) or by unlisting (the latter).

    If you want to apply the method to an entire column:

    first.word <- function(my.string){
        unlist(strsplit(my.string, "-"))[1]
    }
    
    words <- c('apple-orange-strawberry', 'orange-juice')
    
    R: sapply(words, first.word)
    apple-orange-strawberry            orange-juice 
                    "apple"                "orange"
    
    0 讨论(0)
  • 2020-12-03 05:32

    If you need to extract the first (or nth) entry from each split, use:

    word <- c('apple-orange-strawberry','chocolate')
    
    sapply(strsplit(word,"-"), `[`, 1)
    #[1] "apple"     "chocolate"
    

    Or faster and more explictly:

    vapply(strsplit(word,"-"), `[`, 1, FUN.VALUE=character(1))
    #[1] "apple"     "chocolate"
    

    Both bits of code will cope well with selecting whichever value in the split list, and will deal with cases that are outside the range:

    vapply(strsplit(word,"-"), `[`, 2, FUN.VALUE=character(1))
    #[1] "orange" NA  
    
    0 讨论(0)
提交回复
热议问题