我有一个包含三个字段的数据集。我想将其中包含“年度”的任何行替换为适当的日期。
数据:
df <- structure(list(`Grants Period Month` = c("Annual", "01-2014-12",
"Annual", "Annual", "01-2013-06", "Annual"), `Due Date` = structure(c(16525,
16437, 16160, 17256, 15888, 16160), class = "Date"), `Late/Timely Flag` = c("On-Time",
"Late", "Late", "Late", "On-Time", "Late")), row.names = c(NA,
-6L), class = c("tbl_df", "tbl", "data.frame"))
#here's a look at my data
# A tibble: 6 x 2
`Grants Period Month` `Due Date` `Late/Timely Flag`
<chr> <date> <chr>
1 Annual 2015-03-31 On-Time
2 01-2014-12 2015-01-02 Late
3 Annual 2014-03-31 Late
4 Annual 2017-03-31 Late
5 01-2013-06 2013-07-02 On-Time
6 Annual 2014-03-31 Late
我想将其中包含“年度”条目的任何行更改为该特定行的到期日期条目。
#Expected result
# A tibble: 6 x 2
`Grants Period Month` `Due Date` `Late/Timely Flag`
<chr> <date> <chr>
1 2015-03-31 2015-03-31 On-Time
2 01-2014-12 2015-01-02 Late
3 2014-03-31 2014-03-31 Late
4 2017-03-31 2017-03-31 Late
5 01-2013-06 2013-07-02 On-Time
6 2014-03-31 2014-03-31 Late
答案 0 :(得分:1)
您可以做类似的事情
# df is your data frame
is_annual <- df[["Grants Period Month"]] == "Annual"
df[["Grants Period Month"]][is_annual] <- as.character(df[["Due Date"]][is_annual])
答案 1 :(得分:1)
library(dplyr)
mutate(df, `Grants Period Month` = ifelse(`Grants Period Month` == "Annual",
as.character(`Due Date`),
`Grants Period Month`))
首先使用df %>% janitor::clean_names()
。它将为您提供简洁的名称和上面的代码,通常,使用这些新名称,您的数据将变得更加清晰易懂。
我追求快速又肮脏,没有背no,通过手机来做...这是我从适当的键盘和R会话中获得的收获。请注意,所有无法解析的日期都将转换为到期日期。我还将新数据添加到新列中,而不是覆盖。
df <- structure(list(`Grants Period Month` = c("Annual", "01-2014-12", "Annual", "Annual", "01-2013-06", "Annual"), `Due Date` = structure(c(16525, 16437, 16160, 17256, 15888, 16160), class = "Date"), `Late/Timely Flag` = c("On-Time", "Late", "Late", "Late", "On-Time", "Late")), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"))
library(dplyr)
df %>%
mutate(grants_period = lubridate::myd(`Grants Period Month`),
due_date = lubridate::ymd(`Due Date`)) %>%
mutate(new_col=ifelse(is.na(grants_period),
due_date,
grants_period)) %>%
mutate(new_col = lubridate::as_date(new_col))
产生
# A tibble: 6 x 6
`Grants Period Month` `Due Date` `Late/Timely Flag` grants_period due_date new_col
<chr> <date> <chr> <date> <date> <date>
1 Annual 2015-03-31 On-Time NA 2015-03-31 2015-03-31
2 01-2014-12 2015-01-02 Late 2014-01-12 2015-01-02 2014-01-12
3 Annual 2014-03-31 Late NA 2014-03-31 2014-03-31
4 Annual 2017-03-31 Late NA 2017-03-31 2017-03-31
5 01-2013-06 2013-07-02 On-Time 2013-01-06 2013-07-02 2013-01-06
6 Annual 2014-03-31 Late NA 2014-03-31 2014-03-31