我有一个数据集,如下所示
id Description Value Date Indicator
462269 Toyota 9.23 2013-10-21 Post
462269 Toyota 57.45 2013-07-11 None
462269 Ford 32.95 2013-01-25 Pre
462269 Mazda 12.29 2013-04-13 None
462269 Chevy 11.24 2013-05-12 Pre
806284 Toyota 12.11 2014-05-15 Pre
806284 Mazda 12.56 2014-04-16 Pre
806284 Hyundai 89.95 2014-09-15 Pre
806284 Chevy 18.74 2014-02-19 Post
953303 Toyota 4.83 2012-08-14 Post
953303 Mazda 95.46 2012-04-04 None
953303 Mazda 12.78 2012-03-17 Pre
df = structure(list(id = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 3L, 3L, 3L), .Label = c("462269", "806284", "953303"), class = "factor"),
Description = structure(c(5L, 5L, 2L, 4L, 1L, 5L, 4L, 3L,
1L, 5L, 4L, 4L), .Label = c("Chevy", "Ford", "Hyundai", "Mazda",
"Toyota"), class = "factor"), Value = structure(c(11L, 9L,
7L, 3L, 1L, 2L, 4L, 10L, 6L, 8L, 12L, 5L), .Label = c("11.24",
"12.11", "12.29", "12.56", "12.78", "18.74", "32.95", "4.83",
"57.45", "89.95", "9.23", "95.46"), class = "factor"), Date = structure(c(8L,
7L, 4L, 5L, 6L, 11L, 10L, 12L, 9L, 3L, 2L, 1L), .Label = c("2012-03-17",
"2012-04-04", "2012-08-14", "2013-01-25", "2013-04-13", "2013-05-12",
"2013-07-11", "2013-10-21", "2014-02-19", "2014-04-16", "2014-05-15",
"2014-09-15"), class = "factor"), Indicator = structure(c(2L,
1L, 3L, 1L, 3L, 3L, 3L, 3L, 2L, 2L, 1L, 3L), .Label = c("None",
"Post", "Pre"), class = "factor")), .Names = c("id", "Description",
"Value", "Date", "Indicator"), row.names = c(NA, -12L), class = "data.frame")
我的目标是将此数据集转换为多级列表,其中ID
是主要列表元素,Description
是嵌套在每个ID
和{{1}下的第二级别是第三级元素。所需的输出如下所示
Value, Date, Indicator
我想强调第三级元素(价值,日期,指标)
462269
Toyota
Value Date Indicator
9.23 2013-10-21 Post
57.45 2013-07-11 None
Hyundai
Value Date Indicator
NA NA NA
Mazda
Value Date Indicator
12.29 2013-04-13 None
Chevy
Value Date Indicator
11.24 2013-05-12 Pre
Ford
Value Date Indicator
32.95 2013-01-25 Pre
806284
Toyota
Value Date Indicator
12.11 2014-05-15 Pre
Hundai
Value Date Indicator
89.95 2014-09-15 Pre
Mazda
Value Date Indicator
12.56 2014-04-16 Pre
Chevy
Value Date Indicator
18.74 2014-02-19 Post
Ford
Value Date Indicator
NA NA NA
953303
Toyota
Value Date Indicator
4.83 2012-08-14 Post
Hundai
Value Date Indicator
NA NA NA
Mazda
Value Date Indicator
95.46 2012-04-04 None
12.78 2012-03-17 Pre
Chevy
Value Date Indicator
NA NA NA
Ford
Value Date Indicator
NA NA NA
应采用矩阵格式。
非常感谢任何帮助实现这一目标。提前谢谢。
答案 0 :(得分:0)
两次使用split
将创建您想要的嵌套列表结构。
lapply(split(df, df$id),function(x) split(x,x$Description))