I have a data.frame NOAA_OLR_TEST
NOAA_OLR_TEST <- structure(list(DATE_START = structure(c(1170720000, 1170806400,
1170892800, 1170979200, 1171065600, 1171152
You can do this with 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
The question and subsequent comments by the OP are confusing what result the OP is expecting. So, I offer different variants:
library(data.table)
NOAA_OLR_TEST_new <- setDT(NOAA_OLR_TEST)[, .SD, .SDcols = 5:9]
str(NOAA_OLR_TEST_new)
Classes ‘data.table’ and 'data.frame': 10 obs. of 5 variables: $ OLR_DATA_1: num 150 146 146 142 NA 150 158 155 143 142 $ OLR_DATA_2: num 146 146 142 141 150 NA 155 143 142 138 $ OLR_DATA_3: num 146 NA 141 150 158 155 143 142 138 135 $ OLR_DATA_4: num 142 141 150 158 155 143 142 138 135 NA $ OLR_DATA_5: num 141 150 NA 155 143 142 138 135 140 139 - attr(*, ".internal.selfref")=<externalptr>
Note that data.frames and data.tables are special forms of a list.
NOAA_OLR_TEST_new <- as.list(setDT(NOAA_OLR_TEST)[, .SD, .SDcols = 5:9])
str(NOAA_OLR_TEST_new)
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
NOAA_OLR_TEST_new <- setDT(NOAA_OLR_TEST)[, .(as.list(.SD)), .SDcols = 5:9]
str(NOAA_OLR_TEST_new)
Classes ‘data.table’ and 'data.frame': 5 obs. of 1 variable: $ V1:List of 5 ..$ : num 150 146 146 142 NA 150 158 155 143 142 ..$ : num 146 146 142 141 150 NA 155 143 142 138 ..$ : num 146 NA 141 150 158 155 143 142 138 135 ..$ : num 142 141 150 158 155 143 142 138 135 NA ..$ : num 141 150 NA 155 143 142 138 135 140 139 ..- attr(*, ".data.table.locked")= logi TRUE - attr(*, ".internal.selfref")=<externalptr>
Just use 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
Or with lapply
:
lst <- lapply(5:9, function(i) NOAA_OLR_TEST[, i])