申请后无法访问物品

发布于 12-29 07:50 字数 889 浏览 2 评论 0原文

我正在使用 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 技术交流群。

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

发布评论

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

评论(3

毁虫ゝ2025-01-05 07:50:20
 x=c('02D_48M_RHD', '34D_98M_AHR')


> lapply(x,strsplit,split='[DM]_',fixed=F)
[[1]]
[[1]][[1]]
[1] "02"  "48"  "RHD"


[[2]]
[[2]][[1]]
[1] "34"  "98"  "AHR"

这使得嵌套列表变得令人讨厌。我想你想要的是:

> lapply(strsplit(x,split='[DM]_',fixed=F),'[',1)
[[1]]
[1] "02"

[[2]]
[1] "34"
 x=c('02D_48M_RHD', '34D_98M_AHR')


> lapply(x,strsplit,split='[DM]_',fixed=F)
[[1]]
[[1]][[1]]
[1] "02"  "48"  "RHD"


[[2]]
[[2]][[1]]
[1] "34"  "98"  "AHR"

this makes a nasty nested list thing. I think what you want is:

> lapply(strsplit(x,split='[DM]_',fixed=F),'[',1)
[[1]]
[1] "02"

[[2]]
[1] "34"
葮薆情2025-01-05 07:50:19

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)

strsplit is already vectorized, so there's no need to wrap it in lapply. You're confused because a is a list of lists of vectors, not a list of vectors. I.e. a[[1]] is itself a one-element list that contains a vector.

Also, lists are "vectors". That's why is.vector returns TRUE. is.character should return FALSE.

You want something like:

splits    <- strsplit(res$description, "[DM]_", fixed=FALSE)
res$one   <- sapply(splits, "[", 1)
res$two   <- sapply(splits, "[", 2)
res$three <- sapply(splits, "[", 3)
只涨不跌2025-01-05 07:50:19

我认为您对 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

I don't think your call to lapply is necessary as strsplit already works on vectors. Something like this may help:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文