1

I am trying to create a nested list where the value from one vector becomes the name and the process repeats until the last vector which stores an actual value. I was thinking that the code I have below would work, but it isn't

chips[[toString(aCor[i])]]=list(toString(bCor[i])=list(toString(cCor[i])=list(toString(dCor[i])=eCor[i])))

I was expecting something like this if aCor=c(1,2,2,1), bCor=c(4,5,6,4), cCor=c(3,3,2,3), dCor=c(1,4,5,1), eCor=c(1,3,4,7)

Resulting list ["1"=["4"=["3"=["1"= 7]]], "2"=["5"=["3"=["4"=3]],"6"=["2"=["5"=4]]]]

$1
$1$4
$1$4$3
$1$4$3$1
[1] 7

$2
$2$5
$2$5$3
$1$4$3$4
[1] 3

$2
$2$6
$2$6$2
$1$6$2$5
[1] 4

Sorry if the expected list is formatted poorly. I wasnt sure the best way to do it. If there is a better way of doing this than a list I am open to suggestions, I would have used a dictionary in python and this was the closest I could find that would replicate it. I am currently getting this error

Error in parse(text = script) : parse error in text argument: unexpected '=' in function argument before

23
  • Please show a small reproducible example with dput Commented Aug 6, 2020 at 6:35
  • @akrun I am currently getting this error so I cant give a reproducible example Error in parse(text = script) : parse error in text argument: unexpected '=' in function argument before Commented Aug 6, 2020 at 6:37
  • It is just to understand what your input and expected would be Commented Aug 6, 2020 at 6:38
  • @akrun I have that. it is the 2 paragraphs before the end Commented Aug 6, 2020 at 6:39
  • 1
    Would this be helpful? stackoverflow.com/questions/52872581/… Commented Aug 6, 2020 at 7:31

2 Answers 2

2

We could use lst from purrr or dplyr (or need setNames) which would work with assignment (:=)

nm1 <- 'a'
nm2 <- 'b'
dplyr::lst(!! nm1 := lst(!! nm2 := 5))
#$a
#$a$b
#[1] 5

If we need to create a nested list, use a for loop

lst1 <- list()

for(i in seq_along(aCor)) {
      an <- as.character(aCor[i])
      
      bn <- as.character(bCor[i])
      cn <- as.character(cCor[i])
      dn <- as.character(dCor[i])
      lst1[[an]][[bn]][[cn]][[dn]] <- eCor[[i]]
      

}

-output

lst1
#$`1`
#$`1`$`4`
#$`1`$`4`$`3`
#$`1`$`4`$`3`$`1`
#[1] 7




#$`2`
#$`2`$`5`
#$`2`$`5`$`3`
#$`2`$`5`$`3`$`4`
#[1] 3



#$`2`$`6`
#$`2`$`6`$`2`
#$`2`$`6`$`2`$`5`
#[1] 4

data

aCor <- c(1,2,2,1)
bCor <- c(4,5,6,4)
cCor <- c(3,3,2,3)
dCor <- c(1,4,5,1)
eCor <- c(1,3,4,7)
Sign up to request clarification or add additional context in comments.

13 Comments

why are you using := instead of = and why the !!
@NikitaBelooussov The = won't evaluate an object it would give list(nm1 = 5)
and the !! does what?
It evaluates the object
Thanks for your help. I am not sure why your code didnt work, I put up the code that seems like it is going to work in my case
|
1

This is the code I ended up making with the help of akrun in this question and another code that I found on this page written by IRTFM.

require(dplyr)

aCor <- c(1,2,2,1)
bCor <- c(4,5,6,4)
cCor <- c(3,3,2,3) 
dCor <- c(1,4,5,1)
eCor <- c(1,3,4,7)

appendList <- function (x, val) 
{
   stopifnot(is.list(x), is.list(val))
   xnames <- names(x)
   for (v in names(val)) {
      x[[v]] <- if (v %in% xnames && is.list(x[[v]]) && is.list(val[[v]])) 
        appendList(x[[v]], val[[v]])
        else c(val[[v]])
    }
   x
}


chips <- list()

for(i in seq_along(aCor)) {
   an <- as.character(aCor[i]) 
   bn <- as.character(bCor[i])
   cn <- as.character(cCor[i])
   dn <- as.character(dCor[i])
   #chips[[an]]=lst(!! bn := lst(!! cn := lst(!! dn := eCor[i])))
   list1=lst(!! an := lst(!! bn := lst(!! cn  := lst(!! dn:= eCor[i]))))
   chips=appendList(chips,list1)
}

chips

Output:

$1
$1$4
$1$4$3
$1$4$3$1
[1] 7

$`2`
$`2`$`5`
$`2`$`5`$`3`
$`2`$`5`$`3`$`4`
[1] 3

$`2`$`6`
$`2`$`6`$`2`
$`2`$`6`$`2`$`5`
[1] 4

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.