I have a dataframe with multiple columns. For each column, I want to create a new column with a name that corresponds to the column, and some transformation of that column. For example, for column a, I want a new column that is named a_fit which all the values of a are multipled by 2. I would want the new columns named a_fit, b_fit, c_fit, etc. in the dataframe.
Then, ideally I would be able to call the function over a series of columns (20+ columns) in one call.. using lapply maybe?
mydata <- data.frame(a = 1:10, b = 2:11, c = 3:12, d = 4:13, e = 5:14)
newcol <- function(df, x) {
df[[paste(x, "_fit", sep='')]] <= df[[x]] * 2
}
newcol(mydata, a)
Thanks so much for the syntax help-- that cleared it up! However, when I call mydata after performing the function, the new column a_fit is not there. How do I ensure that the function is adding the new column to the existing data frame instead of creating a new one to output?