Search code examples
rdplyrtidyverseplyrtidyr

take a part from a string without a specific pattern


I have a column where in

cell.1 is "UNIV ZURICH;NOTREPORTED;NOTREPORTED;NOTREPORTED" cell.2 is "UNIBG"

 s = c("UNIV ZURICH;NOTREPORTED;NOTREPORTED;NOTREPORTED", "UNIBG")
 s1 = unlist(strsplit(s, split=';', fixed=TRUE))[1]
 s1

and I want to get

cell.1 UNIV ZURICH
cell.2 UNIBG

many thanks in advance,


Solution

  • s = c("UNIV ZURICH;NOTREPORTED;NOTREPORTED;NOTREPORTED", "UNIBG")
    s1 = strsplit(s, split=';')
    result = data.frame(mycol = unlist(lapply(s1, function(x){x[1]})))
    
    > result
            mycol
    1 UNIV ZURICH
    2       UNIBG