繁体   English   中英

如何在R中访问字符串列表的元素?

[英]How can I access the elements of a string list in R?

我有一个字符串X ,它是一个列表,我想访问它的元素。 例:

"[('here', 29), ('negative', 1.0)]"

如何访问'here' ,“ 29'negative'

我们可以使用strsplit

v1 <- strsplit(x, "[[:punct:] ]")[[1]]
v1[nzchar(v1)][1:3]
#[1] "here"     "29"       "negative"

我们也可以使用stringr::str_split

library(stringr)
x <- "[('here', 29), ('negative', 1.0)]"
v1 <- str_split(x, "[[:punct:] ]")[[1]]
v1[nchar(v1)>1]
#[1] "here"     "29"       "negative"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM