2017-04-19 98 views
0

我有一個字符串X這是一個列表,我想訪問它的元素。 例如:如何訪問R中字符串列表的元素?

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

我如何可以訪問'here'29'negative'

+0

這看起來像含有兩個Python元組到我的眼睛Python列表。你可以看看你是如何導入這些數據的。 – lmo

回答

1

我們可以使用strsplit

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

我們也可以使用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"