Daniel Medina Sada
Daniel Medina Sada

Reputation: 478

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

I have a String X that is a List and I want to access its elements. example:

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

How can I access the 'here', 29 and 'negative'?

Upvotes: 0

Views: 143

Answers (2)

setempler
setempler

Reputation: 1751

We can also use 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"

Upvotes: 0

akrun
akrun

Reputation: 887851

We can use strsplit

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

Upvotes: 1

Related Questions