Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.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_Plyr - Fatal编程技术网

R 从列表的子元素中列出对象

R 从列表的子元素中列出对象,r,plyr,R,Plyr,我想做的是做一个列表,然后从该列表的部分元素中创建一个列表。我可以使用subset和dlply分两步完成,但我想知道是否有更快的方法使用XXply方法 所以我有一个数据帧: data <- data.frame( biz = sample(c("telco","shipping","tech"), 50, replace = TRUE), region = sample(c("mideast","americas","asia"), 50, replace = TRUE),

我想做的是做一个列表,然后从该列表的部分元素中创建一个列表。我可以使用subset和dlply分两步完成,但我想知道是否有更快的方法使用XXply方法

所以我有一个数据帧:

data <- data.frame(
    biz = sample(c("telco","shipping","tech"), 50, replace = TRUE),
    region = sample(c("mideast","americas","asia"), 50, replace = TRUE),
    date = rep(seq(as.Date("2010-02-01"), length=10, by = "1 day"),5),
    revenue = sample(500:1000,50,replace=T),
    orders = sample(0:2,50,replace=T)
)

数据我不是100%清楚我明白你想做什么,但也许就是这样

lst <- lapply(
  split(data, data$region), 
  function(df) lapply(split(df, df$biz), identity)
)
lst[["americas"]][["shipping"]]
#         biz   region       date revenue orders
# 3  shipping americas 2010-02-03     621      2
# 23 shipping americas 2010-02-03     799      2
# 33 shipping americas 2010-02-03     920      0
# 34 shipping americas 2010-02-04     705      2

lst如果查看
attributes(list2)
可以看到一个名为
split\u labels
data.frame
attributes(list2)$split\u labels
。这就是你要找的吗?这就是我要找的区域栏中的分割。但我如何使用它们为每个区域创建单独的列表对象,这些区域是由“biz”组织的标识?这正是我想要做的。由于我试图确保尽可能多地理解plyr参数,因此通过plyr执行同样的操作将是:newlist@MarcTulla如果您想使用
dlply
,那么使用以下命令:
list2即可!但是现在我如何访问该列表的子元素呢?假设我想把这些都变成时间序列对象,这样我就可以按区域进行分析。对于单个级别的列表,我会这样做:new.xts@MarcTulla,我认为这已经成为一个新问题。另外,你可能想更全面地解释你想做什么(在一个新问题中)。我不相信嵌套列表是您在这里的最佳解决方案,但我无法确定您是否了解完整的工作流程。
list2 <- dlply(data, .(region, biz), identity)
lst <- lapply(
  split(data, data$region), 
  function(df) lapply(split(df, df$biz), identity)
)
lst[["americas"]][["shipping"]]
#         biz   region       date revenue orders
# 3  shipping americas 2010-02-03     621      2
# 23 shipping americas 2010-02-03     799      2
# 33 shipping americas 2010-02-03     920      0
# 34 shipping americas 2010-02-04     705      2