我有一个列表对象,其中包含许多单独的值列表,这些列表的长度大多为12个。并非所有列表都具有长度为12(如下面的数据所示)。
我想将值提取到x * 12数据框中,其中x是列表的数量(在这种情况下为50)。如果列表没有全部12个值,我希望输入一个空白。因此,如果只有一个值(如第35行),则会进入第35行和第1列,该行的第2到第12列将插入空白。
到目前为止,我的工作主要集中在下面的test105,以解释行数和列数的变化。有没有办法事先标准化以允许数据帧格式?
test105=sapply(test99,"[[",1)
我还转换了列表结构" test99"使用as.matrix命令,但除了更清楚地看到数据之外,这似乎没有给我更大的灵活性。
感谢您的帮助。
dput(test99transfer)
structure(list(`1` = structure(c("3.631412983", "1.351914048",
"-1.82656002", "-3.531716108", "0.468971014", "-0.510936022",
"2.025001049", "4.369469166", "0.574441016", "-0.397388995",
"3.314476013", "2.016448021"), .Names = c("Feb 2012", "Mar 2012",
"Apr 2012", "May 2012", "Jun 2012", "Jul 2012", "Aug 2012", "Sep 2012",
"Oct 2012", "Nov 2012", "Dec 2012", "Jan 2013")), `2` = structure(c("1.351914048",
"-1.82656002", "-3.531716108", "0.468971014", "-0.510936022",
"2.025001049", "4.369469166", "0.574441016", "-0.397388995",
"3.314476013", "2.016448021", "0.041545"), .Names = c("Mar 2012",
"Apr 2012", "May 2012", "Jun 2012", "Jul 2012", "Aug 2012", "Sep 2012",
"Oct 2012", "Nov 2012", "Dec 2012", "Jan 2013", "Feb 2013")),
`3` = structure(c("-1.82656002", "-3.531716108", "0.468971014",
"-0.510936022", "2.025001049", "4.369469166", "0.574441016",
"-0.397388995", "3.314476013", "2.016448021", "0.041545",
"1.944175005"), .Names = c("Apr 2012", "May 2012", "Jun 2012",
"Jul 2012", "Aug 2012", "Sep 2012", "Oct 2012", "Nov 2012",
"Dec 2012", "Jan 2013", "Feb 2013", "Mar 2013")), `4` = structure(c("-3.531716108",
"0.468971014", "-0.510936022", "2.025001049", "4.369469166",
"0.574441016", "-0.397388995", "3.314476013", "2.016448021",
"0.041545", "1.944175005", "0.898332"), .Names = c("May 2012",
"Jun 2012", "Jul 2012", "Aug 2012", "Sep 2012", "Oct 2012",
"Nov 2012", "Dec 2012", "Jan 2013", "Feb 2013", "Mar 2013",
"Apr 2013")), `5` = structure(c("0.468971014", "-0.510936022",
"2.025001049", "4.369469166", "0.574441016", "-0.397388995",
"3.314476013", "2.016448021", "0.041545", "1.944175005",
"0.898332", "1.043239951"), .Names = c("Jun 2012", "Jul 2012",
"Aug 2012", "Sep 2012", "Oct 2012", "Nov 2012", "Dec 2012",
"Jan 2013", "Feb 2013", "Mar 2013", "Apr 2013", "May 2013"
)), `6` = structure(c("-0.510936022", "2.025001049", "4.369469166",
"0.574441016", "-0.397388995", "3.314476013", "2.016448021",
"0.041545", "1.944175005", "0.898332", "1.043239951", "0.722914994"
), .Names = c("Jul 2012", "Aug 2012", "Sep 2012", "Oct 2012",
"Nov 2012", "Dec 2012", "Jan 2013", "Feb 2013", "Mar 2013",
"Apr 2013", "May 2013", "Jun 2013")), `7` = structure(c("2.025001049",
"4.369469166", "0.574441016", "-0.397388995", "3.314476013",
"2.016448021", "0.041545", "1.944175005", "0.898332", "1.043239951",
"0.722914994", "-0.349180996"), .Names = c("Aug 2012", "Sep 2012",
"Oct 2012", "Nov 2012", "Dec 2012", "Jan 2013", "Feb 2013",
"Mar 2013", "Apr 2013", "May 2013", "Jun 2013", "Jul 2013"
)), `8` = structure(c("4.369469166", "0.574441016", "-0.397388995",
"3.314476013", "2.016448021", "0.041545", "1.944175005",
"0.898332", "1.043239951", "0.722914994", "-0.349180996",
"0.074822001"), .Names = c("Sep 2012", "Oct 2012", "Nov 2012",
"Dec 2012", "Jan 2013", "Feb 2013", "Mar 2013", "Apr 2013",
"May 2013", "Jun 2013", "Jul 2013", "Aug 2013")), `9` = structure(c("0.574441016",
"-0.397388995", "3.314476013", "2.016448021", "0.041545",
"1.944175005", "0.898332", "1.043239951", "0.722914994",
"-0.349180996", "0.074822001", "-1.258324027"), .Names = c("Oct 2012",
"Nov 2012", "Dec 2012", "Jan 2013", "Feb 2013", "Mar 2013",
"Apr 2013", "May 2013", "Jun 2013", "Jul 2013", "Aug 2013",
"Sep 2013")), `10` = structure(c("-0.397388995", "3.314476013",
"2.016448021", "0.041545", "1.944175005", "0.898332", "1.043239951",
"0.722914994", "-0.349180996", "0.074822001", "-1.258324027",
"1.153113008"), .Names = c("Nov 2012", "Dec 2012", "Jan 2013",
"Feb 2013", "Mar 2013", "Apr 2013", "May 2013", "Jun 2013",
"Jul 2013", "Aug 2013", "Sep 2013", "Oct 2013")), `11` = structure(c("3.314476013",
"2.016448021", "0.041545", "1.944175005", "0.898332", "1.043239951",
"0.722914994", "-0.349180996", "0.074822001", "-1.258324027",
"1.153113008", "1.344225049"), .Names = c("Dec 2012", "Jan 2013",
"Feb 2013", "Mar 2013", "Apr 2013", "May 2013", "Jun 2013",
"Jul 2013", "Aug 2013", "Sep 2013", "Oct 2013", "Nov 2013"
)), `12` = structure(c("2.016448021", "0.041545", "1.944175005",
"0.898332", "1.043239951", "0.722914994", "-0.349180996",
"0.074822001", "-1.258324027", "1.153113008", "1.344225049",
"2.703722954"), .Names = c("Jan 2013", "Feb 2013", "Mar 2013",
"Apr 2013", "May 2013", "Jun 2013", "Jul 2013", "Aug 2013",
"Sep 2013", "Oct 2013", "Nov 2013", "Dec 2013")), `13` = structure(c("0.041545",
"1.944175005", "0.898332", "1.043239951", "0.722914994",
"-0.349180996", "0.074822001", "-1.258324027", "1.153113008",
"1.344225049", "2.703722954", "-1.334722042"), .Names = c("Feb 2013",
"Mar 2013", "Apr 2013", "May 2013", "Jun 2013", "Jul 2013",
"Aug 2013", "Sep 2013", "Oct 2013", "Nov 2013", "Dec 2013",
"Jan 2014")), `14` = structure(c("1.944175005", "0.898332",
"1.043239951", "0.722914994", "-0.349180996", "0.074822001",
"-1.258324027", "1.153113008", "1.344225049", "2.703722954",
"-1.334722042", "1.17830801"), .Names = c("Mar 2013", "Apr 2013",
"May 2013", "Jun 2013", "Jul 2013", "Aug 2013", "Sep 2013",
"Oct 2013", "Nov 2013", "Dec 2013", "Jan 2014", "Feb 2014"
)), `15` = structure(c("0.898332", "1.043239951", "0.722914994",
"-0.349180996", "0.074822001", "-1.258324027", "1.153113008",
"1.344225049", "2.703722954", "-1.334722042", "1.17830801",
"1.743324995"), .Names = c("Apr 2013", "May 2013", "Jun 2013",
"Jul 2013", "Aug 2013", "Sep 2013", "Oct 2013", "Nov 2013",
"Dec 2013", "Jan 2014", "Feb 2014", "Mar 2014")), `16` = structure(c("1.043239951",
"0.722914994", "-0.349180996", "0.074822001", "-1.258324027",
"1.153113008", "1.344225049", "2.703722954", "-1.334722042",
"1.17830801", "1.743324995", "0.075165004"), .Names = c("May 2013",
"Jun 2013", "Jul 2013", "Aug 2013", "Sep 2013", "Oct 2013",
"Nov 2013", "Dec 2013", "Jan 2014", "Feb 2014", "Mar 2014",
"Apr 2014")), `17` = structure(c("0.722914994", "-0.349180996",
"0.074822001", "-1.258324027", "1.153113008", "1.344225049",
"2.703722954", "-1.334722042", "1.17830801", "1.743324995",
"0.075165004", "0.368221998"), .Names = c("Jun 2013", "Jul 2013",
"Aug 2013", "Sep 2013", "Oct 2013", "Nov 2013", "Dec 2013",
"Jan 2014", "Feb 2014", "Mar 2014", "Apr 2014", "May 2014"
)), `18` = structure(c("-0.349180996", "0.074822001", "-1.258324027",
"1.153113008", "1.344225049", "2.703722954", "-1.334722042",
"1.17830801", "1.743324995", "0.075165004", "0.368221998",
"1.140485048"), .Names = c("Jul 2013", "Aug 2013", "Sep 2013",
"Oct 2013", "Nov 2013", "Dec 2013", "Jan 2014", "Feb 2014",
"Mar 2014", "Apr 2014", "May 2014", "Jun 2014")), `19` = structure(c("0.074822001",
"-1.258324027", "1.153113008", "1.344225049", "2.703722954",
"-1.334722042", "1.17830801", "1.743324995", "0.075165004",
"0.368221998", "1.140485048", "-0.521915972"), .Names = c("Aug 2013",
"Sep 2013", "Oct 2013", "Nov 2013", "Dec 2013", "Jan 2014",
"Feb 2014", "Mar 2014", "Apr 2014", "May 2014", "Jun 2014",
"Jul 2014")), `20` = structure(c("-1.258324027", "1.153113008",
"1.344225049", "2.703722954", "-1.334722042", "1.17830801",
"1.743324995", "0.075165004", "0.368221998", "1.140485048",
"-0.521915972", "0.721825004"), .Names = c("Sep 2013", "Oct 2013",
"Nov 2013", "Dec 2013", "Jan 2014", "Feb 2014", "Mar 2014",
"Apr 2014", "May 2014", "Jun 2014", "Jul 2014", "Aug 2014"
)), `21` = structure(c("1.153113008", "1.344225049", "2.703722954",
"-1.334722042", "1.17830801", "1.743324995", "0.075165004",
"0.368221998", "1.140485048", "-0.521915972", "0.721825004",
"-0.468360007"), .Names = c("Oct 2013", "Nov 2013", "Dec 2013",
"Jan 2014", "Feb 2014", "Mar 2014", "Apr 2014", "May 2014",
"Jun 2014", "Jul 2014", "Aug 2014", "Sep 2014")), `22` = structure(c("1.344225049",
"2.703722954", "-1.334722042", "1.17830801", "1.743324995",
"0.075165004", "0.368221998", "1.140485048", "-0.521915972",
"0.721825004", "-0.468360007", "0.696716011"), .Names = c("Nov 2013",
"Dec 2013", "Jan 2014", "Feb 2014", "Mar 2014", "Apr 2014",
"May 2014", "Jun 2014", "Jul 2014", "Aug 2014", "Sep 2014",
"Oct 2014")), `23` = structure(c("2.703722954", "-1.334722042",
"1.17830801", "1.743324995", "0.075165004", "0.368221998",
"1.140485048", "-0.521915972", "0.721825004", "-0.468360007",
"0.696716011", "-0.181444004"), .Names = c("Dec 2013", "Jan 2014",
"Feb 2014", "Mar 2014", "Apr 2014", "May 2014", "Jun 2014",
"Jul 2014", "Aug 2014", "Sep 2014", "Oct 2014", "Nov 2014"
)), `24` = structure(c("-1.334722042", "1.17830801", "1.743324995",
"0.075165004", "0.368221998", "1.140485048", "-0.521915972",
"0.721825004", "-0.468360007", "0.696716011", "-0.181444004",
"0.585435987"), .Names = c("Jan 2014", "Feb 2014", "Mar 2014",
"Apr 2014", "May 2014", "Jun 2014", "Jul 2014", "Aug 2014",
"Sep 2014", "Oct 2014", "Nov 2014", "Dec 2014")), `25` = structure(c("1.17830801",
"1.743324995", "0.075165004", "0.368221998", "1.140485048",
"-0.521915972", "0.721825004", "-0.468360007", "0.696716011",
"-0.181444004", "0.585435987"), .Names = c("Feb 2014", "Mar 2014",
"Apr 2014", "May 2014", "Jun 2014", "Jul 2014", "Aug 2014",
"Sep 2014", "Oct 2014", "Nov 2014", "Dec 2014")), `26` = structure(c("1.743324995",
"0.075165004", "0.368221998", "1.140485048", "-0.521915972",
"0.721825004", "-0.468360007", "0.696716011", "-0.181444004",
"0.585435987"), .Names = c("Mar 2014", "Apr 2014", "May 2014",
"Jun 2014", "Jul 2014", "Aug 2014", "Sep 2014", "Oct 2014",
"Nov 2014", "Dec 2014")), `27` = structure(c("0.075165004",
"0.368221998", "1.140485048", "-0.521915972", "0.721825004",
"-0.468360007", "0.696716011", "-0.181444004", "0.585435987"
), .Names = c("Apr 2014", "May 2014", "Jun 2014", "Jul 2014",
"Aug 2014", "Sep 2014", "Oct 2014", "Nov 2014", "Dec 2014"
)), `28` = structure(c("0.368221998", "1.140485048", "-0.521915972",
"0.721825004", "-0.468360007", "0.696716011", "-0.181444004",
"0.585435987"), .Names = c("May 2014", "Jun 2014", "Jul 2014",
"Aug 2014", "Sep 2014", "Oct 2014", "Nov 2014", "Dec 2014"
)), `29` = structure(c("1.140485048", "-0.521915972", "0.721825004",
"-0.468360007", "0.696716011", "-0.181444004", "0.585435987"
), .Names = c("Jun 2014", "Jul 2014", "Aug 2014", "Sep 2014",
"Oct 2014", "Nov 2014", "Dec 2014")), `30` = structure(c("-0.521915972",
"0.721825004", "-0.468360007", "0.696716011", "-0.181444004",
"0.585435987"), .Names = c("Jul 2014", "Aug 2014", "Sep 2014",
"Oct 2014", "Nov 2014", "Dec 2014")), `31` = structure(c("0.721825004",
"-0.468360007", "0.696716011", "-0.181444004", "0.585435987"
), .Names = c("Aug 2014", "Sep 2014", "Oct 2014", "Nov 2014",
"Dec 2014")), `32` = structure(c("-0.468360007", "0.696716011",
"-0.181444004", "0.585435987"), .Names = c("Sep 2014", "Oct 2014",
"Nov 2014", "Dec 2014")), `33` = structure(c("0.696716011",
"-0.181444004", "0.585435987"), .Names = c("Oct 2014", "Nov 2014",
"Dec 2014")), `34` = structure(c("-0.181444004", "0.585435987"
), .Names = c("Nov 2014", "Dec 2014")), `35` = structure("0.585435987", .Names = "Dec 2014"),
`36` = structure(c("1.043239951", "0.722914994", "-0.349180996",
"0.074822001", "-1.258324027", "1.153113008", "1.344225049",
"2.703722954", "-1.334722042", "1.17830801", "1.126680017",
""), .Names = c("May 2013", "Jun 2013", "Jul 2013", "Aug 2013",
"Sep 2013", "Oct 2013", "Nov 2013", "Dec 2013", "Jan 2014",
"Feb 2014", "Mar 2014", "Apr 2014")), `37` = structure(c("0.722914994",
"-0.349180996", "0.074822001", "-1.258324027", "1.153113008",
"1.344225049", "2.703722954", "-1.334722042", "1.17830801",
"1.126680017", "", ""), .Names = c("Jun 2013", "Jul 2013",
"Aug 2013", "Sep 2013", "Oct 2013", "Nov 2013", "Dec 2013",
"Jan 2014", "Feb 2014", "Mar 2014", "Apr 2014", "May 2014"
)), `38` = structure(c("-0.349180996", "0.074822001", "-1.258324027",
"1.153113008", "1.344225049", "2.703722954", "-1.334722042",
"1.17830801", "1.126680017", "", "", ""), .Names = c("Jul 2013",
"Aug 2013", "Sep 2013", "Oct 2013", "Nov 2013", "Dec 2013",
"Jan 2014", "Feb 2014", "Mar 2014", "Apr 2014", "May 2014",
"Jun 2014")), `39` = structure(c("0.074822001", "-1.258324027",
"1.153113008", "1.344225049", "2.703722954", "-1.334722042",
"1.17830801", "1.126680017", "", "", "", ""), .Names = c("Aug 2013",
"Sep 2013", "Oct 2013", "Nov 2013", "Dec 2013", "Jan 2014",
"Feb 2014", "Mar 2014", "Apr 2014", "May 2014", "Jun 2014",
"Jul 2014")), `40` = structure(c("-1.258324027", "1.153113008",
"1.344225049", "2.703722954", "-1.334722042", "1.17830801",
"1.126680017", "", "", "", "", ""), .Names = c("Sep 2013",
"Oct 2013", "Nov 2013", "Dec 2013", "Jan 2014", "Feb 2014",
"Mar 2014", "Apr 2014", "May 2014", "Jun 2014", "Jul 2014",
"Aug 2014")), `41` = structure(c("1.153113008", "1.344225049",
"2.703722954", "-1.334722042", "1.17830801", "1.126680017",
"", "", "", "", "", ""), .Names = c("Oct 2013", "Nov 2013",
"Dec 2013", "Jan 2014", "Feb 2014", "Mar 2014", "Apr 2014",
"May 2014", "Jun 2014", "Jul 2014", "Aug 2014", "Sep 2014"
)), `42` = structure(c("1.344225049", "2.703722954", "-1.334722042",
"1.17830801", "1.126680017", "", "", "", "", "", "", ""), .Names = c("Nov 2013",
"Dec 2013", "Jan 2014", "Feb 2014", "Mar 2014", "Apr 2014",
"May 2014", "Jun 2014", "Jul 2014", "Aug 2014", "Sep 2014",
"Oct 2014")), `43` = structure(c("2.703722954", "-1.334722042",
"1.17830801", "1.126680017", "", "", "", "", "", "", "",
""), .Names = c("Dec 2013", "Jan 2014", "Feb 2014", "Mar 2014",
"Apr 2014", "May 2014", "Jun 2014", "Jul 2014", "Aug 2014",
"Sep 2014", "Oct 2014", "Nov 2014")), `44` = structure(c("-1.334722042",
"1.17830801", "1.126680017", "", "", "", "", "", "", "",
"", ""), .Names = c("Jan 2014", "Feb 2014", "Mar 2014", "Apr 2014",
"May 2014", "Jun 2014", "Jul 2014", "Aug 2014", "Sep 2014",
"Oct 2014", "Nov 2014", "Dec 2014")), `45` = structure(c("1.17830801",
"1.126680017", "", "", "", "", "", "", "", "", ""), .Names = c("Feb 2014",
"Mar 2014", "Apr 2014", "May 2014", "Jun 2014", "Jul 2014",
"Aug 2014", "Sep 2014", "Oct 2014", "Nov 2014", "Dec 2014"
)), `46` = structure(c("1.126680017", "", "", "", "", "",
"", "", "", ""), .Names = c("Mar 2014", "Apr 2014", "May 2014",
"Jun 2014", "Jul 2014", "Aug 2014", "Sep 2014", "Oct 2014",
"Nov 2014", "Dec 2014")), `47` = structure(c("0.297455013",
"-2.498219013", "-1.982529998", "0.710475028", "2.43479991",
"-1.629583001", "3.825987101", "1.375319004", "0.468528003",
"-1.180858016", "-0.791706979", "0.260260999"), .Names = c("Sep 2005",
"Oct 2005", "Nov 2005", "Dec 2005", "Jan 2006", "Feb 2006",
"Mar 2006", "Apr 2006", "May 2006", "Jun 2006", "Jul 2006",
"Aug 2006")), `48` = structure(c("-2.498219013", "-1.982529998",
"0.710475028", "2.43479991", "-1.629583001", "3.825987101",
"1.375319004", "0.468528003", "-1.180858016", "-0.791706979",
"0.260260999", "1.613098025"), .Names = c("Oct 2005", "Nov 2005",
"Dec 2005", "Jan 2006", "Feb 2006", "Mar 2006", "Apr 2006",
"May 2006", "Jun 2006", "Jul 2006", "Aug 2006", "Sep 2006"
)), `49` = structure(c("-1.982529998", "0.710475028", "2.43479991",
"-1.629583001", "3.825987101", "1.375319004", "0.468528003",
"-1.180858016", "-0.791706979", "0.260260999", "1.613098025",
"0.493414998"), .Names = c("Nov 2005", "Dec 2005", "Jan 2006",
"Feb 2006", "Mar 2006", "Apr 2006", "May 2006", "Jun 2006",
"Jul 2006", "Aug 2006", "Sep 2006", "Oct 2006")), `50` = structure(c("0.710475028",
"2.43479991", "-1.629583001", "3.825987101", "1.375319004",
"0.468528003", "-1.180858016", "-0.791706979", "0.260260999",
"1.613098025", "0.493414998", "0.659148991"), .Names = c("Dec 2005",
"Jan 2006", "Feb 2006", "Mar 2006", "Apr 2006", "May 2006",
"Jun 2006", "Jul 2006", "Aug 2006", "Sep 2006", "Oct 2006",
"Nov 2006"))), .Names = c("1", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18",
"19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29",
"30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40",
"41", "42", "43", "44", "45", "46", "47", "48", "49", "50"))
答案 0 :(得分:1)
这是使用基础R.的答案。
df <- as.data.frame(do.call("rbind", lapply(yourList, function(x) {
x2 <- as.numeric(x[x != ""])
vec <- rep(NA, 12)
vec[1:length(x2)] <- x2
return(vec)
})))
答案 1 :(得分:1)
请允许我将您的示例缩小到可接受的大小:
my_list <- structure(list(
A = structure(c("3.631412983", "1.351914048", "-1.82656002", "-3.531716108", "0.468971014", "-0.510936022", "2.025001049", "4.369469166", "0.574441016", "-0.397388995", "3.314476013", "2.016448021"), .Names = c("Feb 2012", "Mar 2012", "Apr 2012", "May 2012", "Jun 2012", "Jul 2012", "Aug 2012", "Sep 2012", "Oct 2012", "Nov 2012", "Dec 2012", "Jan 2013")),
B = structure(c("0.721825004", "-0.468360007", "0.696716011", "-0.181444004", "0.585435987"), .Names = c("Aug 2014", "Sep 2014", "Oct 2014", "Nov 2014", "Dec 2014")),
C = structure(c("-1.334722042", "1.17830801", "1.126680017", "", "", "", "", "", "", "", "", ""), .Names = c("Jan 2014", "Feb 2014", "Mar 2014", "Apr 2014", "May 2014", "Jun 2014", "Jul 2014", "Aug 2014", "Sep 2014", "Oct 2014", "Nov 2014", "Dec 2014"))))
转换数据,我会推荐dplyr和tidyr并使用stack(utils),mutate(dplyr)和separate(tidyr)
library(dplyr)
library(tidyr)
my_tidy_list <- my_list %>%
stack %>%
mutate(names = my_list %>% unlist %>% names) %>%
separate(names, into = c("id", "month", "year"), sep = "\\.|\ ") %>%
select(-ind) %>%
print
这样你就会得到一个长表格格式,这对于ggplot2的下游绘图更有用。如果你无论如何更喜欢宽表,请使用附加值:
my_tidy_list %>%
spread(month, values)
答案 2 :(得分:0)
您可以在bas R中使用此方法:
# the number of columns
maxcol <- max(sapply(test99transfer, length))
# create a list of one-row matrices
matlist <- lapply(test99transfer,
function(x) unname(t(c(x, rep("", maxcol - length(x))))))
# create a data frame
dat <- as.data.frame(do.call(rbind, matlist))