Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put in labels in list for R?

Tags:

list

r

label

I am using R currently and I want to know how can I label the list objects when I declare a list. For example: return(list(xhat,alpha,beta)), and xhat, alpha and beta are themselves arrays. I want to give each an appropriate label.

Thank you.

like image 565
George Tyler Avatar asked Nov 23 '10 05:11

George Tyler


People also ask

Can you name items in a list R?

The list can be created using list() function in R. Named list is also created with the same function by specifying the names of the elements to access them. Named list can also be created using names() function to specify the names of elements after defining the list.

How do I add an item to an existing list in R?

To append an element in the R List, use the append() function. You can use the concatenate approach to add components to a list. While concatenate does a great job of adding elements to the R list, the append() function operates faster.


2 Answers

For simple cases Michael's answer will work. Sometimes though you have a vector of names my_names which you would like to use to name/rename the output. There are at least three ways:

  • use names<-:

    out <- list(xhat,alpha,beta)  
    names(out) <- my_names  
    out
    
  • use setNames():

    setNames(out, my_names)
    
  • use structure():

    structure(out, names=my_names)
    
like image 185
VitoshKa Avatar answered Oct 06 '22 21:10

VitoshKa


All you need is list(x=xhat, a=alpha, b=beta)

like image 42
Michael Dunn Avatar answered Oct 06 '22 20:10

Michael Dunn