I have a large data frame in a panel structure (201720 rows; 3 columns) which looks as follows:
Name <- c("A", "A", "A", "B", "B", "B")
Inception <- c(as.Date("2007-12-31"), as.Date("2007-12-31"), as.Date("2007-12-31"),
as.Date("1990-12-31"), as.Date("1990-12-31"), as.Date("1990-12-31"))
Months <- c(as.Date("2010-01-01"), as.Date("2010-02-01"), as.Date("2010-03-01"),
as.Date("2010-01-01"), as.Date("2010-02-01"), as.Date("2010-03-01"))
df <- data.frame(Name, Inception, Months)
I want to calculate the difference in months of «Inception» and «Months» for each row and assign it to a new column named «Age». If the result is negative, it should fill in with NA. I came up with the following solution and it worked. However, the computation of it is not very fast.
for (i in 1:nrow(df)){
if(df[i,2]>df[i,3]){
df[i,"Age"] <- NA
} else {
df[i,"Age"] <- interval(df[i,2],
df[i,3]) %/% months(1)
}
}
Is there a more efficient way to calculate this difference?