【问题标题】:How to change cell value within a column in reference to another cell value within another column in r?如何参考r中另一列中的另一个单元格值更改列中的单元格值?
【发布时间】:2021-10-15 08:44:06
【问题描述】:

我想通过查看“周”值来替换“月”值。如果是第 52 周,那么月份应该是 12。如何跨数据执行此操作?

示例数据:

   year month week
    2010 1 52
    2010 12 52
    2011 1 52
    2011 12 52
    2012 1 52
    2012 12 52

预期数据:

year month week
2010 12 52
2010 12 52
2011 12 52
2011 12 52
2012 12 52
2012 12 52

【问题讨论】:

  • 有几个星期包含两个不同月份的天数,不是吗?所以你不能做你想做的事。
  • @MrSmithGoesToWashington 不过,这取决于 OP 对什么感兴趣:他们可能希望获得一周开始的月份。

标签: r weather monthcalendar


【解决方案1】:

正如@MrSmithGoesToWashington 指出的那样,如果从时间的角度来看,这是不可能的。但是,如果您只是询问如何根据另一列中的值更改任何值,则可以这样做。

library(dplyr)
df <- data.frame(year = c(2010, 2010),
                 month = c(1, 12),
                 week = c(52, 52))

df %>% mutate(month = ifelse(week == 52, 12, df$month))

【讨论】:

    【解决方案2】:

    这是一个基本的 R 方式。
    如果年/周介于 12 月的第一天和最后一天的年/周之间,则月份为 12,否则为记录月份。

    yw <- with(df1, paste(year, week))
    yy01 <- paste(df1$year, 12, 1, sep = "-")
    yy31 <- paste(df1$year, 12, 31, sep = "-")
    yy01 <- format(as.Date(yy01), "%Y %U")
    yy31 <- format(as.Date(yy31), "%Y %U")
    ifelse(yy01 <= yw & yw <= yy31, 12, df1$month)
    #[1] 12 12 12 12 12 12
    

    并将此值分配给列month

    df1$month <- ifelse(yy01 <= yw & yw <= yy31, 12, df1$month)
    

    数据

    df1 <- read.table(text = "
     year month week
        2010 1 52
        2010 12 52
        2011 1 52
        2011 12 52
        2012 1 52
        2012 12 52
    ", header = TRUE)
    

    【讨论】:

      【解决方案3】:
      # Import data: df1 => data.frame
      df1 <- structure(list(year = c(2010L, 2010L, 2011L, 2011L, 2012L, 2012L
      ), week = c(52L, 52L, 52L, 52L, 52L, 52L)), class = "data.frame", 
      row.names = c(NA, -6L))
      
      # Generate a sequence of dates, store as a data.frame: 
      # date_range => data.frame
      date_range <- data.frame(
        date = seq(
          from = as.Date(
            paste(
              min(df1$year),
              "01-01",
              sep = "-"
            )
          ),
          to = as.Date(
            paste(
              max(df1$year),
              "12-31",
              sep = "-"
            )
          ),
          by = "days"
        )
      )
      
      # Derive the month: month_no => integer vector
      date_range$month_no <- as.integer(
        strftime(
          date_range$date,
          "%m"
        )
      )
      
      # Derive the week: week_no => integer vector
      date_range$week_no <- as.integer(
        strftime(
          date_range$date,
          "%V"
        )
      )
      
      # Derive the year: year_no => integer vector
      date_range$year_no <- as.integer(
        strftime(
          date_range$date,
          "%Y"
        )
      )
      
      # Create a lookup table: year_mon_week_lkp => data.frame
      year_mon_week_lkp <- transform(
        aggregate(
          month_no ~ year_no+week_no,
          data = date_range,
          FUN = max
        ),
        month_no = ifelse(week_no >= 52, 12, month_no)
      )
      
      # Resolve the month using the week_no and the year: 
      # month => integer vector
      df1$month <- with(
        df1, 
        year_mon_week_lkp$month_no[
          match(
            paste0(
              year,
              week
            ),
            paste0(
                year_mon_week_lkp$year_no, 
                year_mon_week_lkp$week_no
            )
          )
        ]
      )
      

      【讨论】:

        猜你喜欢
        • 2013-12-27
        • 2022-11-14
        • 2020-02-26
        • 1970-01-01
        • 2021-04-19
        • 1970-01-01
        • 2019-10-04
        • 1970-01-01
        • 2018-01-16
        相关资源
        最近更新 更多