[R] Lists heading in an array of lists in R
Duncan Murdoch
murdoch.duncan at gmail.com
Fri Jan 22 09:45:56 CET 2016
On 22/01/2016 2:29 AM, TJUN KIAT TEO wrote:
> I am trying to populate an array of lists in R . Here is my code
>
> TunePar<-matrix(list(Null),2,2)
>
> TunePar[1,1]=list(G=2)
>
> But when I type TunePar[1,1,], all I get is 2. The G has disappeared. why?
>
> If I do this
>
> Test=list(G=2)
>> Test
> $G
> [1] 2
>
Matrices generally don't keep the names of elements assigned to them.
What you did is similar to
x <- matrix(0, 2,2)
x[1,1] <- c(G=2)
x
which loses the name.
Working with lists is different, because you need to distinguish between
subsets and elements. So you'd get what you want by assigning your list
to the element using
TunePar[[1,1]] <- list(G=2)
At this point, TunePar[1,1] is a list containing list(G=2): it prints as
[[1]]
[[1]]$G
[1] 1
To get the result you want, you also need to access the element instead
of the subset: TunePar[[1,1]] will print
$G
[1] 1
Duncan Murdoch
More information about the R-help
mailing list