【问题标题】:How to save multiple numbers in one cell in a matrix/dataframe?如何在矩阵/数据框中的一个单元格中保存多个数字?
【发布时间】:2014-11-25 15:13:10
【问题描述】:

这是 R 语言。

来自一个名为 temp_warnings 的矩阵,看起来像

    row.names  row  day   Tx     Hx      Tn
1   61         61   30   31.9   36.85   19.1
2   84         84   23   33.5   43.07   20.3
3   85         85   24   31.5   39.82   19.2
4   94         94   2    30.9   41.36   20.0
5   99         99   7    34.0   43.17   21.6
6   101       101   9    34.4   42.45   21.0
7   131       131   8    30.1   38.52   19.6
8   132       132   9    30.7   38.35   21.0

我希望使用行和日列将这些信息保存到一个名为 stn 的新矩阵中。

                             2001
Tmax >= 30 & Tmin >= 19     61, 84, 85, 94, 99, 101, 131, 132
May 
June                          30
July                        23, 24
August                      2, 7, 9
September                   8, 9

所以我希望将列行的内容保存在第一个单元格中。从 5 月 1 日到 9 月 30 日,Tx、Hx 和 Tn 的测试时间为 153 天,因此日列对应于当月的某一天。因此,对于列行号 1-31 是 5 月,32-61 是 6 月,依此类推。我也希望将日期列号保存在其月份的正确单元格中。

如果您需要任何其他信息,请告诉我, 谢谢, 尼克

【问题讨论】:

    标签: r matrix dataframe


    【解决方案1】:

    这是非常不寻常的格式,所以事情会变得一团糟:

    dat <- read.table(header = TRUE, text="row.names  row  day   Tx     Hx      Tn
    1   61         61   30   31.9   36.85   19.1
    2   84         84   23   33.5   43.07   20.3
    3   85         85   24   31.5   39.82   19.2
    4   94         94   2    30.9   41.36   20.0
    5   99         99   7    34.0   43.17   21.6
    6   101       101   9    34.4   42.45   21.0
    7   131       131   8    30.1   38.52   19.6
    8   132       132   9    30.7   38.35   21.0")
    
    
    ## creating a column for the months and pasting the days by month
    dat <- within(dat, {
      m <- cut(row, breaks = c(0, 31, 61, 91, 121, Inf), labels = month.abb[5:9])
      ms <- ave(dat$day, m, FUN = function(x) paste(x, collapse = ', '))
    #   'Tmax >= 30 & Tmin >= 19' <- paste(row, collapse = ', ')
    })
    
    ## creating the final data frame to merge into
    dat1 <- data.frame(' ' = c('Tmax >= 30 & Tmin >= 19', month.abb[5:9]),
                       '2001' = c(paste(dat$row, collapse = ', '), rep(NA, 5)),
                       check.names = FALSE)
    
    dat1 <- merge(dat1, dat[!duplicated(dat[c('m','ms')]), c('m','ms')],
                  by.x = ' ', by.y = 'm', all = TRUE)
    
    ## combining the two columns and some clean-up
    dat1 <- within(dat1, {
      '2001' <- gsub('NA', '', paste(`2001`, ms))
      ms <- NULL
      ' ' <- factor(` `, levels = c('Tmax >= 30 & Tmin >= 19', month.abb[5:9]))
    })
    
    ## and ordering the rows as desired
    dat1[with(dat1, order(` `)), ]
    
    #                                                         2001
    # 6 Tmax >= 30 & Tmin >= 19 61, 84, 85, 94, 99, 101, 131, 132 
    # 4                     May                                   
    # 3                     Jun                                 30
    # 2                     Jul                             23, 24
    # 1                     Aug                            2, 7, 9
    # 5                     Sep                               8, 9
    

    【讨论】:

      【解决方案2】:

      这就是我最终做的事情

      stn[1,1] <- toString(temp_warnings$row)
      stn[2,1] <- toString((subset(temp_warnings, row <= 31))$day)
      stn[3,1] <- toString((subset(temp_warnings, 31 < row & row <= 61))$day)
      stn[4,1] <- toString((subset(temp_warnings, 61 < row & row <= 92))$day)
      stn[5,1] <- toString((subset(temp_warnings, 92 < row & row <= 123))$day)
      stn[6,1] <- toString((subset(temp_warnings, 123 < row))$day)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-17
        • 2018-07-22
        • 1970-01-01
        • 2015-07-20
        • 2018-10-03
        相关资源
        最近更新 更多