Reputation: 29
I have a data frame called df which looks like this:
ID DATE RAIN
79001 2013-11-28 2.0
79001 2013-11-29 34.0
79001 2013-11-30 3.0
79002 2013-10-09 17.2
79002 2013-10-10 5.0
79002 2013-10-11 12.0
79003 2013-10-14 14.8
79003 2013-10-15 4.8
79003 2013-10-16 0
There is a mistake in the data, such as that for ID 79001 the month is wrong, it should be September and not November. How can I fix the date from 2013-11-28 to 2013-09-28 ?. I tried the following:
df[df$DATUM == "2013-11-28"] <- "2013-09-28"
This however doesn't seem like a good way and it doesn't work. I thought about converting the Date to a character and then changing the value, but I don't know if it works and if it works, how to do it
I am thankful for any help
Thank you a lot
Upvotes: 0
Views: 174
Reputation: 2670
df <- read.table(textConnection('ID DATE RAIN
79001 2013-11-28 2.0
79001 2013-11-29 34.0
79001 2013-11-30 3.0
79002 2013-10-09 17.2
79002 2013-10-10 5.0
79002 2013-10-11 12.0
79003 2013-10-14 14.8
79003 2013-10-15 4.8
79003 2013-10-16 0'),header=T)
df$DATE <- as.Date(df$DATE)
df[df$DATE == "2013-11-28",]$DATE <- as.Date("2013-09-28")
df
output;
ID DATE RAIN
<int> <date> <dbl>
1 79001 2013-09-28 2
2 79001 2013-11-29 34
3 79001 2013-11-30 3
4 79002 2013-10-09 17.2
5 79002 2013-10-10 5
6 79002 2013-10-11 12
7 79003 2013-10-14 14.8
8 79003 2013-10-15 4.8
9 79003 2013-10-16 0
Upvotes: 1