将第一行转换为数据帧列表中的列名,然后删除第一行

时间:2014-01-07 14:00:48

标签: r

我对R来说相对较新,并且在尝试将数据框列表的第一行转换为列名时遇到了问题。

我有一个120行的数据帧,前10行的输入在下面给出。

> dput(head(mmcmon,10))
structure(list(`mmcall[1:120, ]` = structure(c(121L, 1L, 1L, 
1L, 1L, 103L, 125L, 1L, 1L, 1L), .Label = c("0", "0.000120774", 
"0.000411971", "0.000418782", "0.001009412", "0.002133363", "0.006300886", 
"0.007512761", "0.01520653", "0.01922036", "0.02122252", "0.03000229", 
"0.03125497", "0.09864875", "0.1038622", "0.1110728", "0.1716216", 
"0.301216", "0.3091952", "0.3409163", "0.3741058", "0.3840781", 
"0.4457962", "0.554204", "0.6159262", "0.6258942", "0.6590838", 
"0.6908052", "0.6987841", "0.8283813", "0.8889272", "0.8961378", 
"0.9013513", "0.968745", "0.9699977", "0.9787775", "0.9807797", 
"0.9847939", "0.9924875", "0.9936991", "0.9978666", "0.9989906", 
"0.9995812", "0.999588", "0.9998792", "0.9999928", "0.9999952", 
"0.9999953", "0.9999976", "0.9999999", "1", "1.02E-274", "1.06E-170", 
"1.14E-177", "1.33E-08", "1.34E-164", "1.36E-26", "1.50E-67", 
"1.70E-87", "1.74E-137", "1.79E-10", "1.79E-23", "1.80E-220", 
"1.88E-83", "1.92E-09", "1.97E-13", "1.98E-40", "2.13E-69", "2.23E-78", 
"2.37E-08", "2.38E-06", "2.38E-11", "2.55E-74", "2.58E-41", "2.76E-44", 
"2.79E-275", "2.90E-72", "3.06E-56", "3.09E-64", "3.50E-128", 
"3.50E-300", "3.55E-258", "3.61E-95", "3.99E-22", "4.31E-53", 
"4.44E-134", "4.72E-06", "4.84E-06", "5.15E-09", "5.18E-281", 
"5.19E-128", "5.25E-165", "5.34E-66", "5.51E-247", "5.52E-08", 
"5.80E-43", "6.08E-33", "6.14E-46", "6.65E-208", "7.22E-06", 
"7.49E-10", "7.88E-51", "8.24E-130", "8.32E-162", "8.48E-99", 
"8.76E-120", "8.78E-24", "8.81E-28", "9.83E-32", "ABS1,ABS2", 
"ABS1,AR1", "ABS1,AR2", "ABS1,BS", "ABS2,ABS1", 
"ABS2,AR1", "ABS2,AR2", "ABS2,BS", "AR1,ABS1", 
"AR1,ABS2", "AR1,AR2", "AR1,BS", "AR2,ABS1", 
"AR2,ABS2", "AR2,AR1", "AR2,BS", "BS,ABS1", 
"BS,ABS2", "BS,AR1", "BS,AR2"), class = "factor")), .Names = "mmcall[1:120, ]", row.names = c(NA, 
10L), class = "data.frame")

我通过使用以下命令将数据帧分成5行数据帧(每行24行)来获取数据帧列表:

cmonlist=split(mmcmon, (seq(nrow(mmcmon))-1) %/% 24)

由此获得的第一元素的输出如下:

  

dput(头(cmonlist [[1]]))       结构(列表(mmcall[1:120, ] =结构(c)(121L,1L,1L,       1L,1L,103L),. Label = c(“0”,“0.000120774”,“0.000411971”,       “0.000418782”,“0.001009412”,“0.002133363”,“0.006300886”,“0.007512761”,       “0.01520653”,“0.01922036”,“0.02122252”,“0.03000229”,“0.03125497”,       “0.09864875”,“0.1038622”,“0.1110728”,“0.1716216”,“0.301216”,       “0.3091952”,“0.3409163”,“0.3741058”,“0.3840781”,“0.4457962”,       “0.554204”,“0.6159262”,“0.6258942”,“0.6590838”,“0.6908052”,       “0.6987841”,“0.8283813”,“0.8889272”,“0.8961378”,“0.9013513”,       “0.968745”,“0.9699977”,“0.9787775”,“0.9807797”,“0.9847939”,       “0.9924875”,“0.9936991”,“0.9978666”,“0.9989906”,“0.9995812”,       “0.999588”,“0.9998792”,“0.9999928”,“0.9999952”,“0.9999953”,       “0.9999976”,“0.9999999”,“1”,“1.02E-274”,“1.06E-170”,“1.14E-177”,       “1.33E-08”,“1.34E-164”,“1.36E-26”,“1.50E-67”,“1.70E-87”,       “1.74E-137”,“1.79E-10”,“1.79E-23”,“1.80E-220”,“1.88E-83”,       “1.92E-09”,“1.97E-13”,“1.98E-40”,“2.13E-69”,“2.23E-78”,“2.37E-08”,       “2.38E-06”,“2.38E-11”,“2.55E-74”,“2.58E-41”,“2.76E-44”,“2.79E-275”,       “2.90E-72”,“3.06E-56”,“3.09E-64”,“3.50E-128”,“3.50E-300”,       “3.55E-258”,“3.61E-95”,“3.99E-22”,“4.31E-53”,“4.44E-134”,       “4.72E-06”,“4.84E-06”,“5.15E-09”,“5.18E-281”,“5.19E-128”,       “5.25E-165”,“5.34E-66”,“5.51E-247”,“5.52E-08”,“5.80E-43”,       “6.08E-33”,“6.14E-46”,“6.65E-208”,“7.22E-06”,“7.49E-10”,       “7.88E-51”,“8.24E-130”,“8.32E-162”,“8.48E-99”,“8.76E-120”,       “8.78E-24”,“8.81E-28”,“9.83E-32”,“ABS1,ABS2”,“ABS1,AR1”,       “ABS1,AR2”,“ABS1,BS”,“ABS2,ABS1”,“ABS2,AR1”,       “ABS2,AR2”,“ABS2,BS”,“AR1,ABS1”,“AR1,ABS2”,       “AR1,AR2”,“AR1,BS”,“AR2,ABS1”,“AR2,ABS2”,       “AR2,AR1”,“AR2,BS”,“BS,ABS1”,“BS,ABS2”,       “BS,AR1”,“BS,AR2”),class =“factor”)),. Name =“mmcall [1:120,]”,row.names = c(NA,   6L),class =“data.frame”)

列表中所有其他数据帧的部分结构相似。如您所见,每个数据框名称类似于'mmcall [1:120,]',第一行类似于'AR1,BS'。我想用第一行重命名列,然后删除第一行。所以我希望我的数据帧的列名为'AR1,BS',值为0,0,0,0,8.24E-130。

当我尝试使用lapply时,我不会收到任何错误,但会得到奇怪的结果。

  

trylist = lapply(cmonlist,function(x){colnames(x)= x [1,]})

> trylist[[1]]
[1] AR1,BS
129 Levels: 0 0.000120774 0.000411971 0.000418782 0.001009412 0.002133363 ... BS,AR2

单个数据框的类也会更改为'factor'。

> class(cmonlist[[1]]) 
[1] "data.frame"
> class(trylist[[1]]) 
[1] "factor"

有人可以帮忙吗?我不明白如何实现这一点,以及为什么它会变成一个因素。

此外,如果有人能够回答为什么这么小的数据框的输出看起来如此巨大,那就太棒了。

提前多多感谢。 -Shivam

1 个答案:

答案 0 :(得分:2)

lapply内的功能仅返回x[1, ]。您必须返回整个数据框。使用此命令:

lapply(cmonlist, function(x) "names<-"(x, x[1, ]))

如果您还想删除每个数据框的第一行,请使用:

lapply(cmonlist, function(x) tail("names<-"(x, x[1, ]), -1))