Reputation: 67
I have a sample dataset as shown below
sampDate <- as.Date(c("2011-09-01","2011-09-02","2011-09-03","2011-09-04","2011-09-05", "2011-09-06"), format ="%Y-%m-%d")
p1 <- c( 10, 6.1, 11.1, 9.1, 10.1, 7)
p2 <- rep(1,6)
df <- data.frame(sampDate,p1,p2);df
sampDate p1 p2
1 2011-09-01 10.0 1
2 2011-09-02 6.1 1
3 2011-09-03 11.1 1
4 2011-09-04 9.1 1
5 2011-09-05 10.1 1
6 2011-09-06 7.0 1
I'd like to replace the 1's in column p2, which are from date 2011-09-01 till 2011-09-03, with zeros instead, so that the resulting dataframe will be like this;
sampDate p1 p2
1 2011-09-01 10.0 0
2 2011-09-02 6.1 0
3 2011-09-03 11.1 0
4 2011-09-04 9.1 1
5 2011-09-05 10.1 1
6 2011-09-06 7.0 1
I have been trying to use ifelse
and other indexing operators but i'm just running into a brick wall. If i would get an indexing method, it would be the best because of my large dataset.
Upvotes: 0
Views: 111
Reputation: 81693
within(df, p2 <- replace(p2, sampDate>="2011-09-01" & sampDate<="2011-09-03", 0))
will do the trick.
sampDate p1 p2
1 2011-09-01 10.0 0
2 2011-09-02 6.1 0
3 2011-09-03 11.1 0
4 2011-09-04 9.1 1
5 2011-09-05 10.1 1
6 2011-09-06 7.0 1
Upvotes: 2