申请后无法访问物品
我正在使用 lapply
尝试拆分数据框中的字符串。这些字符串看起来都类似于“02D_48M_RHD”
。我试图获取“D”之前和“M”之前的数字。
我对 lapply 的使用似乎有效:
a <- lapply(res$description, strsplit, split="[DM]_", fixed=FALSE)
> a[[1]]
[[1]]
[1] "02" "48" "RHD"
但是,我一生都无法弄清楚如何访问 a[1]。 文档建议a[[1 ]][1]
应该给我第一个元素,但这就是发生的情况:
> a[[1]][1]
[[1]]
[1] "02" "48" "RHD"
我不明白为什么这不起作用。 R 告诉我这是一个向量,但它也说它的长度为一。
> is.vector(a[[1]])
[1] TRUE
> length(a[[1]])
[1] 1
我不确定我误会了什么。 lapply
是否以某种不同于我预期的方式提供输出?我期望一个长度为三的向量列表,这就是它的样子。或者,这就是我得到的,但我试图错误地访问它们?
最终,我想在我的数据框中添加三列,每一条信息对应一列,因此任何可以帮助我朝这个方向前进的东西都将不胜感激。
I'm using lapply
to try to split up a character string in a data frame. The strings all look similar to "02D_48M_RHD"
. I'm trying to grab the numbers before the "D" and before the "M".
My use of lapply
seems to be working:
a <- lapply(res$description, strsplit, split="[DM]_", fixed=FALSE)
> a[[1]]
[[1]]
[1] "02" "48" "RHD"
However I cannot, for the life of me, figure out how to access just the first element of the vector in a[1]. The documentation suggests that a[[1]][1]
should give me the first element, but this is what happens:
> a[[1]][1]
[[1]]
[1] "02" "48" "RHD"
I don't understand why this doesn't work. R tells me that this is a vector, but it also says that it has length of one.
> is.vector(a[[1]])
[1] TRUE
> length(a[[1]])
[1] 1
I'm not sure what I'm misunderstanding. Is lapply
giving output in some way other than what I expect? I expect a list of vectors of length three, and that's what it looks like. Or, is that what I'm getting but I'm trying to access them wrong?
Eventually, I'd like to add three columns to my data frame, one for each of these pieces of information, so anything that could help me move in that direction would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(3)
strsplit
已经矢量化,因此无需将其包装在 lapply
中。您很困惑,因为 a
是向量列表的列表,而不是向量列表。即a[[1]]
本身是一个包含向量的单元素列表。
此外,列表是“向量”。这就是 is.vector
返回 TRUE
的原因。 is.character
应返回 FALSE
。
你想要这样的东西:
splits <- strsplit(res$description, "[DM]_", fixed=FALSE)
res$one <- sapply(splits, "[", 1)
res$two <- sapply(splits, "[", 2)
res$three <- sapply(splits, "[", 3)
我认为您对 lapply 的调用没有必要,因为 strsplit 已经适用于向量。像这样的事情可能会有所帮助:
a <- "02D_48M_RHD"
#Create a vector of values to splot
aa <- c(a,a,a,a,a,a,a)
#rbind them together and make a data.frame
> data.frame(do.call("rbind", strsplit(aa, split="[DM]_", fixed=FALSE)))
X1 X2 X3
1 02 48 RHD
2 02 48 RHD
3 02 48 RHD
4 02 48 RHD
5 02 48 RHD
6 02 48 RHD
7 02 48 RHD
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
这使得嵌套列表变得令人讨厌。我想你想要的是:
this makes a nasty nested list thing. I think what you want is: