Vera Marya
Vera Marya

Reputation: 27

How can I store a value in a name?

I use the neotoma package where I get data from a geographical site, which is marked by an ID. What I want to do is to "store" the number in a term, like Sitenum, so I can just need to write down the ID once and then use it.

What I did:

Site<-get_download(20131, verbose = TRUE)
taxa<-as.vector(Site$'20131'$taxon.list$taxon.name)

What I want to do:

Sitenum <-20131
Site<-get_download(Sitenum, verbose = TRUE) # this obv. works
taxa<-as.vector(Site$Sitenum$taxon.list$taxon.name) # this doesn't work

The structure of the dataset:

str(Site)
    List of 1
     $ 20131:List of 6
      ..$ taxon.list  :'data.frame':    84 obs. of  6 variables:
      .. ..$ taxon.name      : Factor w/ 84 levels "Alnus","Amaranthaceae",..: 1 2 3 4 5 6 7 8 9 10 ...

Upvotes: 0

Views: 44

Answers (1)

lmo
lmo

Reputation: 38500

I constructed an object that mimics yours as follows:

Site <- list("2043"=list(other=data.frame(that=1:10)))

Note that the structure is essentially identical.

str(Site)
List of 1
 $ 2043:List of 1
  ..$ other:'data.frame':   10 obs. of  1 variable:
  .. ..$ that: int [1:10] 1 2 3 4 5 6 7 8 9 10

Now, I save the value of the first term:

temp <- 2043

Then use the code in my comment to access the inner vector:

Site[[as.character(temp)]]$other$that
 [1]  1  2  3  4  5  6  7  8  9 10

I could also use recursive referencing like this

Site[[c(temp,"other", "that")]]
 [1]  1  2  3  4  5  6  7  8  9 10

because c will coerce temp to be a character vector in the presence of "other" and "that" character vectors.

Upvotes: 1

Related Questions