Alex
Alex

Reputation: 1304

Change a row value in a date variable

I have a dataset that looks something like this

data <- structure(list(producer_id = c("10005", "10005", "10007", "10007", 
"10007"), date = structure(c(18297, NA, 15768, 16155, NA), class = "Date")), row.names = c(NA, 
-5L), class = c("data.table", "data.frame"))

   producer_id       date
1:       10005 2020-02-05
2:       10005       <NA>
3:       10007 2013-03-04
4:       10007 2014-03-26
5:       10007       <NA>

The final data should look something like this:

   producer_id       date
1:       10005 2020-02-05
2:       10005 2020-30-12 # changed this value
3:       10007 2013-03-04
4:       10007 2014-03-26
5:       10007       <NA>

In other words, I would like to change the specific date values based on conditions. Normally I would use datatable and do something like this.

data <– setDT(data)[producer_id==1005 & is.na(date),date:="2020-30-12"]
# or 
data <- setDT(data)[producer_id==1005 & is.na(date),date:=2020-30-12]

However, while I do not get any error, these approaches do not transform the data. Does anyone know what is the problem and how I could deal with it efficiently (preferably with data.table)?

Thanks a lot for your help

Upvotes: 2

Views: 41

Answers (3)

ThomasIsCoding
ThomasIsCoding

Reputation: 102700

Here is another data.table option

data[.("10005", NA), date := as.Date("2020-12-30"), by = .EACHI, on = .(producer_id, date)]

which gives

   producer_id       date
1:       10005 2020-02-05
2:       10005 2020-12-30
3:       10007 2013-03-04
4:       10007 2014-03-26
5:       10007       <NA>

Upvotes: 0

akrun
akrun

Reputation: 887881

Using dplyr

library(dplyr)
data %>%
    mutate(date = case_when(producer_id == 10005 & !is.na(date) ~ 
                     as.Date("2020-12-30"), TRUE ~ date))

Upvotes: 1

Ronak Shah
Ronak Shah

Reputation: 389265

Since you have date column of class "Date", change the replacement to the same class.

library(data.table)

data[producer_id == 10005 & is.na(date),date := as.Date("2020-12-30")]
data
#   producer_id       date
#1:       10005 2020-02-05
#2:       10005 2020-12-30
#3:       10007 2013-03-04
#4:       10007 2014-03-26
#5:       10007       <NA>

Upvotes: 2

Related Questions