将数据帧转换为新列表

时间:2017-11-24 13:24:47

标签: r list dataframe data.table

我有一个data.frame NOAA_OLR_TEST

NOAA_OLR_TEST <- structure(list(DATE_START = structure(c(1170720000, 1170806400,
1170892800, 1170979200, 1171065600, 1171152000, 1171238400, 1171324800,
1171411200, 1171497600), class = c("POSIXct", "POSIXt")), 
DATE_END = structure(c(1171065600,1171152000, 1171238400, 1171324800, 
1171411200, 1171497600, 1171584000,1171670400, 1171756800, 1171843200), 
class = c("POSIXct", "POSIXt")), LONGITUDE = c(-89.5, -89.5, -89.5, -89.5, 
-89.5, -88.5, -88.5,-88.5, -88.5, -88.5), LATITUDE = c(-179.5, -179.5, -179.5, 
-179.5,-179.5, -179.5, -179.5, -179.5, -179.5, -179.5), OLR_DATA_1 = c(150,146, 
146, 142, NA, 150, 158, 155, 143, 142), OLR_DATA_2 = c(146,146, 142, 141, 150, 
NA, 155, 143, 142, 138), OLR_DATA_3 = c(146,NA, 141, 150, 158, 155, 143, 142, 
138, 135), OLR_DATA_4 = c(142,141, 150, 158, 155, 143, 142, 138, 135, NA), 
OLR_DATA_5 = c(141,150, NA, 155, 143, 142, 138, 135, 140, 139)), 
.Names = c("DATE_START","DATE_END", "LONGITUDE", "LATITUDE", "OLR_DATA_1", 
"OLR_DATA_2","OLR_DATA_3", "OLR_DATA_4", "OLR_DATA_5"), row.names = c(NA,10L), 
class = "data.frame") 

我希望将数据帧NOAA_OLR_TEST [5:9]的No.5转换为No.9列到列表

2 个答案:

答案 0 :(得分:0)

只需使用c(...)

lst <- c(NOAA_OLR_TEST)[5:9];
#lst;
#List of 5
# $ OLR_DATA_1: num [1:10] 150 146 146 142 NA 150 158 155 143 142
# $ OLR_DATA_2: num [1:10] 146 146 142 141 150 NA 155 143 142 138
# $ OLR_DATA_3: num [1:10] 146 NA 141 150 158 155 143 142 138 135
# $ OLR_DATA_4: num [1:10] 142 141 150 158 155 143 142 138 135 NA
# $ OLR_DATA_5: num [1:10] 141 150 NA 155 143 142 138 135 140 139

lapply

lst <- lapply(5:9, function(i) NOAA_OLR_TEST[, i])

答案 1 :(得分:0)

您可以使用data.table

执行此操作
library(data.table)
setDT(NOAA_OLR_TEST)
NOAA_OLR_TEST[,xy.list:=list(OLR_DATA_1,OLR_DATA_2,OLR_DATA_3,OLR_DATA_4,OLR_DATA_5)]
NOAA_OLR_TEST[,(5:9):= NULL]

is.list(NOAA_OLR_TEST$xy.list)
[1] TRUE