如何更改一列中的单元格值以引用 r 中另一列中的另一个单元格值?

How to change cell value within a column in reference to another cell value within another column in r?

我想通过查看 'week' 值来替换 'month' 值。如果是第 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指出的,如果从时间的角度来看,是不可能的。但是,如果您只是询问如何根据另一列中的值更改任何值,您可以这样做。

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))

这是基本的 R 方式。
如果 year/week 在 12 月的第一天和最后一天的 year/week 之间,则该月为 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)
# 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
      )
    )
  ]
)