I have a string that is a composite of n substrings. It could look like this:
string <- c("A_AA", "A_BB", "A_BB_AAA", "B_AA", "B_BB", "B_CC")
Every subcomponent in this string is separated from any other by "_". Here, the first level consists of the values "A" and "B", the second level of "AA", "BB" and "CC", the third level of "AAA". Deeper nestings are possible and the solution should extend to those cases. The nestings are not necessarily balanced, e.g. "A" only has two children, while "B" has three, but it also has a grandchild which "B" has not.
Essentially, I want to recreate the nested structure in this string in some kind of R object, preferably a list. Thus, the nested list structure that would look like this:
list("A" = list("AA", "BB" = list("AAA")),
"B" = list("AA", "BB", "CC"))
> $A
$A[[1]]
[1] "AA"
$A$BB
$A$BB[[1]]
[1] "CCC"
$B
$B[[1]]
[1] "AA"
$B[[2]]
[1] "BB"
$B[[3]]
[1] "CC"
Any help on this is appreciated
You can make it into a matrix without too much fuss...
string <- c("A_AA", "A_BB", "A_BB_AAA", "B_AA", "B_BB", "B_CC")
splitted<-strsplit(string,"_")
cols<-max(lengths(splitted))
mat<-do.call(rbind,lapply(splitted, "length<-", cols))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With