在列表中的多个列表中定义对象的名称(使用lapply?)(Define names of objects in multiple lists within a list (using lapply?)

编程入门 行业动态 更新时间:2024-10-15 20:21:27
在列表中的多个列表中定义对象的名称(使用lapply?)(Define names of objects in multiple lists within a list (using lapply?))

我对这个有点令人困惑的标题表示道歉(欢迎提出任何改进建议)..

假设我有一个列表,其中包含几个(例如四个)列表,我希望稍后在其中存储20个对象:

mylist <- vector(mode="list",length=4) names(mylist) <- c("One","Two","Three","Four") mylist$One <- mylist$Two <- mylist$Three <- mylist$Four <- vector(mode="list", length=20)

我想事先定义这些对象的名称。 当然,我可以这样做:

names(mylist$One) <- c("A","B","C","D","E","F","G","H","I","J", "K","L","M","N","O","P","Q","R","S","T") names(mylist$Two) <- names(mylist$Three) <- names(mylist$Four) <- names(mylist$One)

但是如果列表的数量会增加(就像在我的实际数据中那样),这会变得相当麻烦,所以我试图用lapply这样的函数来做这个:

mylist <- lapply(mylist,FUN=function(x) {names(x) <- c("A","B","C","D","E","F","G","H","I","J", "K","L","M","N","O","P","Q","R","S","T")})

然而,这并没有给我相同的结果,但我似乎无法弄清楚我在这里忽略了什么。 有什么建议么?

谢谢!

My apologies for the somewhat confusing title (any suggestion for improvement are welcome)..

Suppose I have a list which contains several (e.g. four) lists in which I would like to store 20 objects later on:

mylist <- vector(mode="list",length=4) names(mylist) <- c("One","Two","Three","Four") mylist$One <- mylist$Two <- mylist$Three <- mylist$Four <- vector(mode="list", length=20)

I would like to define the names of those objects beforehand. Of course, I can do that as following:

names(mylist$One) <- c("A","B","C","D","E","F","G","H","I","J", "K","L","M","N","O","P","Q","R","S","T") names(mylist$Two) <- names(mylist$Three) <- names(mylist$Four) <- names(mylist$One)

But if the number of the lists would increase (as is the case in my actual data), this becomes rather cumbersome, so I was trying to do this with a function such as lapply :

mylist <- lapply(mylist,FUN=function(x) {names(x) <- c("A","B","C","D","E","F","G","H","I","J", "K","L","M","N","O","P","Q","R","S","T")})

This, however, does not give me the same result, but I can not seem to figure out what I am overlooking here. Any suggestions?

Thanks!

最满意答案

您需要在lapply调用中返回一个值:

mylist <- lapply(mylist,FUN=function(x) {names(x) <- c("A","B","C","D","E","F","G","H","I","J", "K","L","M","N","O","P","Q","R","S","T") x ## <- note the x here; you could also use return(x) }) mylist # $One # A B C D E F G H I J K L M N O P Q R S T # "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" # # $Two # A B C D E F G H I J K L M N O P Q R S T # "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" # # $Three # A B C D E F G H I J K L M N O P Q R S T # "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" # # $Four # A B C D E F G H I J K L M N O P Q R S T # "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T"

You need to return a value in your lapply call:

mylist <- lapply(mylist,FUN=function(x) {names(x) <- c("A","B","C","D","E","F","G","H","I","J", "K","L","M","N","O","P","Q","R","S","T") x ## <- note the x here; you could also use return(x) }) mylist # $One # A B C D E F G H I J K L M N O P Q R S T # "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" # # $Two # A B C D E F G H I J K L M N O P Q R S T # "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" # # $Three # A B C D E F G H I J K L M N O P Q R S T # "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" # # $Four # A B C D E F G H I J K L M N O P Q R S T # "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T"

更多推荐

本文发布于:2023-08-02 22:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1381936.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   列表中   定义   对象   名称

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!