在R中,如何在不使用分隔符的情况下分割字符串

发布于 2024-12-01 23:00:10 字数 239 浏览 0 评论 0原文

我正在尝试 split 方法,我想要一个仅包含 2 个元素的字符串的第二个元素。字符串的大小是 2。

示例:

string= "AC"

结果应该是第一个字母(“A”)之后的分割,我得到:

res=    [,1] [,2]
     [1,] "A" "C"

我尝试使用分割,但我不知道如何在第一个元素之后分割?

i am try split method and i want to have the second element of a string containing only 2 elemnts. The size of the string is 2.

examples :

string= "AC"

result shouldbe a split after the first letter ("A"), that I get :

res=    [,1] [,2]
     [1,] "A" "C"

I tryed it with split, but I have no idea how to split after the first element??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

顾铮苏瑾 2024-12-08 23:00:10

strsplit() 会做你想做的事(如果我理解你的问题)。您需要拆分 "" 来拆分其元素上的字符串。下面是一个示例,展示了如何对字符串向量执行您想要的操作:

strs <- rep("AC", 3) ## your string repeated 3 times

三个字符串中的每一个

sstrs <- strsplit(strs, "")

接下来,拆分生成的

> sstrs
[[1]]
[1] "A" "C"

[[2]]
[1] "A" "C"

[[3]]
[1] "A" "C"

这是一个列表,因此我们可以使用 lapply()处理它>sapply()。我们需要对 sstrs 的每个元素进行子集化以选择第二个元素。为此,我们应用 [ 函数:

sapply(sstrs, `[`, 2)

它会产生:

> sapply(sstrs, `[`, 2)
[1] "C" "C" "C"

如果您只有一个字符串,那么

strsplit("AC", "")[[1]][2]

它会给出:

> strsplit("AC", "")[[1]][2]
[1] "C"

strsplit() will do what you want (if I understand your Question). You need to split on "" to split the string on it's elements. Here is an example showing how to do what you want on a vector of strings:

strs <- rep("AC", 3) ## your string repeated 3 times

next, split each of the three strings

sstrs <- strsplit(strs, "")

which produces

> sstrs
[[1]]
[1] "A" "C"

[[2]]
[1] "A" "C"

[[3]]
[1] "A" "C"

This is a list so we can process it with lapply() or sapply(). We need to subset each element of sstrs to select out the second element. Fo this we apply the [ function:

sapply(sstrs, `[`, 2)

which produces:

> sapply(sstrs, `[`, 2)
[1] "C" "C" "C"

If all you have is one string, then

strsplit("AC", "")[[1]][2]

which gives:

> strsplit("AC", "")[[1]][2]
[1] "C"
时常饿 2024-12-08 23:00:10

split 不用于这种字符串操作。您正在寻找的是 strsplit,在您的情况下将使用如下内容:

strsplit(string,"",fixed = TRUE)

您可能不需要 fixed = TRUE,但这是我的习惯,因为我倾向于避免正则表达式。您似乎表明您希望结果类似于矩阵。 strsplit 将返回一个列表,因此您需要如下所示的内容:

strsplit(string,"",fixed = TRUE)[[1]]

然后将结果传递给 matrix

split isn't used for this kind of string manipulation. What you're looking for is strsplit, which in your case would be used something like this:

strsplit(string,"",fixed = TRUE)

You may not need fixed = TRUE, but it's a habit of mine as I tend to avoid regular expressions. You seem to indicate that you want the result to be something like a matrix. strsplit will return a list, so you'll want something like this:

strsplit(string,"",fixed = TRUE)[[1]]

and then pass the result to matrix.

我只土不豪 2024-12-08 23:00:10

如果您确定它始终是两个字符字符串(通过 all(nchar(x)==2) 检查)并且您只想要第二个,那么您可以使用 sub 或 <代码>子字符串:

x <- c("ab", "12")
sub(".", "", x)
# [1] "b" "2"
substr(x, 2, 2)
# [1] "b" "2"

If you sure that it's always two char string (check it by all(nchar(x)==2)) and you want only second then you could use sub or substr:

x <- c("ab", "12")
sub(".", "", x)
# [1] "b" "2"
substr(x, 2, 2)
# [1] "b" "2"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文