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.
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.
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.
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)
All you need is list(x=xhat, a=alpha, b=beta)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With