从没有特定模式的字符串中取一部分

take a part from a string without a specific pattern

我有一列

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

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

我想得到

cell.1 UNIV ZURICH
cell.2 UNIBG

非常感谢,

您的 strplit() 方法是个好主意,它给出了:

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

[[2]]
[1] "UNIBG"

为了得到你要找的东西,你需要提取你得到的列表的每个元素的第一个元素,然后合并它们,这里有一个方法可以做到这一点(顺便说一句,fixed=TRUE此示例现在需要)。

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

之前,您将所有元素合并到一个列表中:

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

然后你取了这个向量的第一个元素。

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