我该如何使用base高效地在第一个逗号处拆分以下字符串?
感谢您的提前帮助。
编辑:没想到要提到这个。这需要能够推广到一个列、字符串向量,如下所示:
结果可以是两列或一个长向量(我可以取每隔一个元素),或者是一个字符串列表,其中每个索引([n])有两个字符串。
对于缺乏清晰度的问题,我们深表歉意。
x <- "I want to split here, though I don't want to split elsewhere, even here."
strsplit(x, ???)
期望的结果(2个字符串):
[[1]]
[1] "I want to split here" "though I don't want to split elsewhere, even here."
感谢您的提前帮助。
编辑:没想到要提到这个。这需要能够推广到一个列、字符串向量,如下所示:
y <- c("Here's comma 1, and 2, see?", "Here's 2nd sting, like it, not a lot.")
结果可以是两列或一个长向量(我可以取每隔一个元素),或者是一个字符串列表,其中每个索引([n])有两个字符串。
对于缺乏清晰度的问题,我们深表歉意。
list(head(y[[1]],1), paste(tail(y[[1]],-1), collapse = ","))
这样的东西怎么样?其中y
是strsplit(x, ...)
的输出? - Chasestr_locate_all(string=y, ',')
将找到您模式(在此为逗号)的所有索引位置,然后可以将其应用于从向量或列中进行选择。 - John