2

Consider the following array assignments:

temp=array(list(),2)
temp[[2]][[2]]=c("a","b")
temp[[1]][[2]]="c"

This produces the following result:

temp
[[1]]
[1] NA  "c"

[[2]]
[[2]][[1]]
NULL

[[2]][[2]]
[1] "a" "b"

Instead, I want the result to be:

temp
[[1]]
[[1]][[1]]
NULL
[[1]][[2]]
[1] "c"

[[2]]
[[2]][[1]]
NULL

[[2]][[2]]
[1] "a" "b"

How do I make the assignment so that the former is produced rather than the latter?

2
  • 1
    Try list(list(NULL, "c"), list(NULL, c("a", "b"))) Commented Sep 22, 2014 at 1:43
  • In my actual code I create the contents of this array using loops and other functions with variable outputs. I can't manually solve the problem in the middle of my function; I need some way to insert the result directly to the proper place in the array. Commented Sep 22, 2014 at 1:48

1 Answer 1

3

You can initialize the list(s) with replicate instead of array. Lists and arrays behave differently

x <- replicate(2, list())
x[[1]][[2]] <- "c"
x[[2]][[2]] <- c("a", "b")
x

Note:

is.array(x)
# [1] FALSE
sapply(x, is.array)
# [1] FALSE FALSE
Sign up to request clarification or add additional context in comments.

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.