Reputation: 789
I have a data frame. Here is a sample part.
Lines <- "Category date
desktop 2017-12-25
tablet 2016-05-13
desktop 2018-06-01
desktop 2017-08-06
tablet 2015-12-31"
DF <- read.table(text = Lines, header = TRUE)
DF$date <- as.Date(DF$date)
There are more than 1000 rows in the data frame.
What I would like to do is: if the category is desktop, how can I add 2 years to the existing date? If the category is tablet, how can I add only 1 year to the existing date? Thank you for the help!
Upvotes: 1
Views: 2241
Reputation: 23004
Or using only the lubridate package for its years()
function:
library(lubridate)
DF$date[DF$Category == "desktop"] <- DF$date[DF$Category == "desktop"] + years(2)
DF$date[DF$Category == "tablet"] <- DF$date[DF$Category == "tablet"] + years(1)
> DF
Category date
1 desktop 2019-12-25
2 tablet 2017-05-13
3 desktop 2020-06-01
4 desktop 2019-08-06
5 tablet 2016-12-31
Upvotes: 1
Reputation: 269431
This does not use any packages. The input data frame is DF
. The code converts the date
column to "POSIXlt"
class in the first line giving lt
, increments the year component of lt
in the second line and converts lt
back to "Date"
class in the third line. (Note that the second line could easily be modified if additional categories were to be implemented by simply adding on more terms.)
lt <- as.POSIXlt(DF$date)
lt$year <- lt$year + 2 * (DF$Category == "desktop") + (DF$Category == "laptop")
DF$date <- as.Date(lt)
giving:
Category date
1 desktop 2019-12-25
2 tablet 2016-05-13
3 desktop 2020-06-01
4 desktop 2019-08-06
5 tablet 2015-12-31
Upvotes: 2
Reputation: 31161
If you have big data.frames, you can do:
library(lubridate)
library(data.table)
addYear = function(date, nyear){year(date)=year(date)+nyear; date}
setDT(df)[,date:=as.Date(date)][
Category=='desktop', date:=addYear(date,2)][
Category=='tablet', date:=addYear(date,1)]
# Category date
#1: desktop 2019-12-25
#2: tablet 2017-05-13
#3: desktop 2020-06-01
#4: desktop 2019-08-06
#5: tablet 2016-12-31
Upvotes: 2
Reputation: 10954
Use ifelse
:
library(readr)
library(lubridate)
library(dplyr)
dfX = read_table("Category date
desktop 2017-12-25
tablet 2016-05-13
desktop 2018-06-01
desktop 2017-08-06
tablet 2015-12-31",
col_types = list(col_character(), col_date()))
dfX %>%
mutate(date_new = ifelse(Category == "desktop", date + dyears(2),
ifelse(Category == "tablet", date + dyears(1), date)))
Upvotes: 1
Reputation: 4024
This might be best done through merging.
library(dplyr)
library(lubridate)
additions =
data_frame(Category = c("desktop", "tablet"),
interval = c(2, 1))
data %>%
left_join(additions) %>%
mutate(date = ymd(date),
interval = interval %>% as.period("year"),
next_date = date + interval )
Upvotes: 0