[英]Split character vector by element in R?
Given a vector: 给定一个向量:
c("kuku", "pupu", "lilu","","ff","rrrr", "", "rrr")
How can I split it by ""
? 如何用
""
分割?
To get 3 vectors: 要获得3个向量:
c("kuku", "pupu", "lilu")
c("ff","rrrr")
c("rrr")
We can get the cumulative sum of logical vector to create the grouping index for split
我们可以得到逻辑向量的累积和创造分组指数
split
i1 <- v1 == ""
i1
#[1] FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE
grp <- cumsum(i1)
grp
#[1] 0 0 0 1 1 1 2 2
Note that with cumulative sum, it adds 1 at every TRUE value. 请注意,对于累计和,它在每个TRUE值处加1。 Then, we do a subset of the vector to not include the
""
. 然后,我们对向量进行子集处理以不包含
""
。 By negating ( !
) the TRUE beomes FALSE and FALSE -> TRUE. 否定(
!
),则TRUE beomes为FALSE和FALSE-> TRUE。
v1[!i1]
#[1] "kuku" "pupu" "lilu" "ff" "rrrr" "rrr"
Similarly, the 'grp' is also subsetted as we want both vectors to be of the same length and do the split
同样,由于我们希望两个向量的长度相同并进行
split
,因此也将“ grp”作为子集
split(v1[!i1], grp[!i1])
#$`0`
#[1] "kuku" "pupu" "lilu"
#$`1`
#[1] "ff" "rrrr"
#$`2`
#[1] "rrr"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.