Seyma Kalay
Seyma Kalay

Reputation: 2861

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,

Upvotes: 1

Views: 31

Answers (2)

Kevin Cazelles
Kevin Cazelles

Reputation: 1255

Your strplit() approach is a good idea, it gives:

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

[[2]]
[1] "UNIBG"

In order to get what you are looking for, you need to extract the first element of each element of the list you obtained and then merge them, here is a way to do so (btw, fixed=TRUE is now required for this example).

s1 <- unlist(lapply(strsplit(s, split=';', fixed=TRUE), `[`, 1))

Previously, you were merging all elements in one list:

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

and then you were taking the first element of this vector.

Upvotes: 1

user1357015
user1357015

Reputation: 11686

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

Upvotes: 1

Related Questions