Skip to content
RDocumentation: strsplit
  • AI Chat
  • Code
  • Report
  • Note that this notebook was automatically generated from an RDocumentation page. It depends on the package and the example code whether this code will run without errors. You may need to edit the code to make things work.

    if(!require('base')) {
        install.packages('base')
        library('base')
    }
    noquote(strsplit("A text I want to display with spaces", NULL)[[1]])
    
    x <- c(as = "asfef", qu = "qwerty", "yuiop[", "b", "stuff.blah.yech")
    # split x on the letter e
    strsplit(x, "e")
    
    unlist(strsplit("a.b.c", "."))
    ## [1] "" "" "" "" ""
    ## Note that 'split' is a regexp!
    ## If you really want to split on '.', use
    unlist(strsplit("a.b.c", "[.]"))
    ## [1] "a" "b" "c"
    ## or
    unlist(strsplit("a.b.c", ".", fixed = TRUE))
    
    ## a useful function: rev() for strings
    strReverse <- function(x)
            sapply(lapply(strsplit(x, NULL), rev), paste, collapse = "")
    strReverse(c("abc", "Statistics"))
    
    ## get the first names of the members of R-core
    a <- readLines(file.path(R.home("doc"),"AUTHORS"))[-(1:8)]
    a <- a[(0:2)-length(a)]
    (a <- sub(" .*","", a))
    # and reverse them
    strReverse(a)
    
    ## Note that final empty strings are not produced:
    strsplit(paste(c("", "a", ""), collapse="#"), split="#")[[1]]
    # [1] ""  "a"
    ## and also an empty string is only produced before a definite match:
    strsplit("", " ")[[1]]    # character(0)
    strsplit(" ", " ")[[1]]   # [1] ""