How to pass columns as arguments to a function, and then create a new column that is a function of the other two? For example, taking this great function to add months to a date and taking this sample data frame:
df <- structure(
list(
date = structure(
c(
17135,
17105,
17105,
17074,
17286,
17317,
17317,
17347,
17105,
17317
),
class = "Date"
),
monthslater = c(10,
11, 13, 14, 3, 3, 3, 3, 4, NA)
),
.Names = c("date", "monthslater"),
row.names = c(NA, 10L),
class = "data.frame"
)
I would like to create a new column in which I will give the recording of the columns date
and monthslater
in function add.months
. I would think something like this would work:
df$newdate <- add.months(df$date, df$monthslater)
But this is not so.
Full code for the function:
add.months <- function(date,n) seq(date, by = paste(n, "months"), length = 2)[2]