convert dataframe to new list

前端 未结 3 979
情歌与酒
情歌与酒 2021-01-29 12:22

I have a data.frame NOAA_OLR_TEST

NOAA_OLR_TEST <- structure(list(DATE_START = structure(c(1170720000, 1170806400,
1170892800, 1170979200, 1171065600, 1171152         


        
相关标签:
3条回答
  • 2021-01-29 12:59

    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
    
    0 讨论(0)
  • 2021-01-29 13:02

    The question and subsequent comments by the OP are confusing what result the OP is expecting. So, I offer different variants:

    Return a data.table with just columns 5:9

    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.

    Return columns 5:9 as 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
    

    Convert columns 5:9 as a list column of a data.table

    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>
    
    0 讨论(0)
  • 2021-01-29 13:08

    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])
    
    0 讨论(0)
提交回复
热议问题