'how to produce a vector of a nested character string
In a data.frame a the variable b is a nested character list.
> class(a$b)
[1] "character"
> a$b[2]
[1] "[1034974, 1008535, 1008552, ..., 1008682]"
dput(a$b[1:2)
c("[1034974, 1008535, 1008552, ..., 1008578]",
"[1034974, 1008535, 1008552, 1000001, ..., 1301205, 1008682]")
I want to produce a vector of all these values. thx in advance
Solution 1:[1]
You need to split the string values and convert them to integers. See the below code as an example:
require(stringr)
string1 <- "[1034974, 1008535, 1008552, 1008682]"
string2 = str_remove(string1, "\\[") #remove the open bracket [
string3 = str_remove(string2, "\\]") #remove the close bracket ]
string4 = str_replace_all(string3, " ", "") #remove the spaces
string5 = strsplit(string4, ",")[[1]] #split the string values
int = as.integer(string5) #convert to integer
print(int)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mohammad |
