I'm having trouble adding the next date value from a column to the same row as the first date, if that makes sense. Each 'event' has a start date and an end date, both contained within a date column. I want to take the next date for a given 'event' ("Well_ID" column) and put it in the Lead_Date column so I can select data between those dates. The following doesn't give errors, but it won't populate the Lead_Date column, and I'm not sure why.
library(dplyr)
library(lubridate)
library(randomForest)
library(stringr)
library(caTools)
# Map 1-based optional input ports to variables
dataset1 <- maml.mapInputPort(1) # class: data.frame
dataset1 %>% group_by(Well_ID)
dataset1$Lead_Date <- ""
# Add a column of the next event for each Well_ID:
dataset1 %>% mutate(Lead_Date = lead(Date, order_by = Date))
I'm very new to R, so any help is greatly appreciated!
%>%
) works. For instance, thegroup_by
call will not carry over to the next call. Also, the beauty ofmutate
is that you no longer need to create a placeholder column before hand (dataset1$Lead_Date <- ""
)dput