Hi, I am a beginner in R and I would like to create a function that adds 2 values from a dataframe, then the result is then return with an additional column in dataframe without having to mutate, I have used the below code chunk and have error. Not sure which step is wrong. Can someone please help me? Also I am not sure if I can also include in the function to have a new column from it. Many thanks in advance.
df <- data.frame(A=1:10, B=2:11)
df
add = function(data,col2, col2){
for(i in 1:length(col)){
#print(columns[i])
total = (data[ col1]) + (data[ col2])
}
return(total)
}
add_vec= Vectorize(add)
ans= add_vec(data = df, col1 = 'A', col2 = 'B')
ans