list 如何在R列表中放入标签?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4253201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 01:34:46  来源:igfitidea点击:

How to put in labels in list for R?

listrlabel

提问by George Tyler

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, alphaand betaare themselves arrays. I want to give each an appropriate label.

我目前正在使用 R,我想知道在声明列表时如何标记列表对象。例如:return(list(xhat,alpha,beta)), and xhat, alphaandbeta本身就是数组。我想给每个人一个合适的标签。

Thank you.

谢谢你。

回答by VitoshKa

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

对于简单的情况,迈克尔的回答会奏效。有时,尽管您有一个名称向量my_names,您想用它来命名/重命名输出。至少有以下三种方式:

  • 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)
    
  • 使用names<-

    out <- list(xhat,alpha,beta)  
    names(out) <- my_names  
    out
    
  • 使用setNames()

    setNames(out, my_names)
    
  • 使用structure()

    structure(out, names=my_names)
    

回答by Michael Dunn

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

所有你需要的是 list(x=xhat, a=alpha, b=beta)