Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用R将向量对象的名称转换为向量列表的标记_R_List_Data Transform - Fatal编程技术网

使用R将向量对象的名称转换为向量列表的标记

使用R将向量对象的名称转换为向量列表的标记,r,list,data-transform,R,List,Data Transform,我有一些向量,像这样: months <- c("january", "february", "march", "october", "december") weekdays <- c("Sunday", "Monday", "Tuesday") seasons <- c("Summer", &qu

我有一些向量,像这样:

months <- c("january", "february", "march", "october", "december") 
weekdays <- c("Sunday", "Monday", "Tuesday")
seasons <- c("Summer", "Winter", "Fall", "autumn")
names(timeWords_list) <- c("months",  "weekdays", "seasons")
我想创建一个这样的列表

timeWords_list <- list(months,  weekdays, seasons)
有没有办法用矢量对象的名称直接标记列表中的对象

我想要的结果可以这样实现:

months <- c("january", "february", "march", "october", "december") 
weekdays <- c("Sunday", "Monday", "Tuesday")
seasons <- c("Summer", "Winter", "Fall", "autumn")
names(timeWords_list) <- c("months",  "weekdays", "seasons")

但是,有没有办法直接做到这一点?无需在字符串向量中重新写入这些名称?

如果您已经在使用tidyverse中的软件包,那么tibble::lst已经做到了:

tibble::一个月、一个工作日、一个季节 美元月 [1] 一月二月三月十月十二月 $平日 [1] 星期日星期一星期二 $seasons [1] 夏冬秋 如果你不是,你可以自己烘焙,从以下方面借鉴:


mylist试试下面的一行。第一种方法确实要求每个名称写两次,但不需要提供名称的字符向量,代码的意图非常清楚。第二种方法只要求名字写一次。第三种方法根本不要求写出名称,但只有在没有其他变量的名称以s结尾时才有效——如果有这样的名称,那么这些变量也将包括在列表中

没有使用任何软件包

L1 <- list(months = months, weekdays = weekdays, seasons = seasons)

L2 <- mget(c("months", "weekdays", "seasons"))

L3 <- mget(ls(pattern = "s$"))

tibble::如果你愿意,lst会为你做这件事。我本以为第一件事是已知的,但事后看来,无论如何都应该包括在内,以防万一。当get/assign函数被滥用时,我经常建议不要使用它们,以至于在这样的合法使用中我总是忘记它们。