Need help in creating a function to use values from a dataframe

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

For someone coming to R from an imperative/procedural language, such as C/C++ or Python, this will seem uncomfortably foreign because it follows the functional programming paradigm.

# avoid naming objects after built-in names such as df and data
# it is usually not a problem but can be for operations that 
# treat the object as if it were a closure

DF <- data.frame(A=1:10, B=2:11)

DF["tot"] <- DF[,1] + DF[,2]

DF
#>     A  B tot
#> 1   1  2   3
#> 2   2  3   5
#> 3   3  4   7
#> 4   4  5   9
#> 5   5  6  11
#> 6   6  7  13
#> 7   7  8  15
#> 8   8  9  17
#> 9   9 10  19
#> 10 10 11  21

Thanks so much but is it possible to create a function for it so it can be used for any dataframe?

Adds to @Yarnabrina a parameter to take the name of the data frame as an argument.

DF <- data.frame(A=1:10, B=2:11)

sum_two <- function(w,x,y,z) x[w] = x[,y] + x[,z]

sum_two("tot",DF,1,2)

DF
#>     A  B
#> 1   1  2
#> 2   2  3
#> 3   3  4
#> 4   4  5
#> 5   5  6
#> 6   6  7
#> 7   7  8
#> 8   8  9
#> 9   9 10
#> 10 10 11

I am still getting error running it :frowning: :disappointed_relieved: :cry:. I believe as there is 2 vectors involved, there is a need to vectorize the function right and for loop is also require in order to solve this.

Error: unexpected symbol in "add <- function(dataset_name, column_1_name, column_2_name, sum_column_name) { dataset_name[sum_column_name] <- dataset_name[column_1_name] + dataset_name[column_2_name] dataset_name"

I have now tried and used the below but not sure where went wrong that the numbers that are taken for the equation is wrong :
df3 = data.frame(A=1:10, B=2:11)
df3
add = function(d,col1, col2){
for(i in 1:length(col)){
#print(columns[i])

total = (d[ col1]) + (d[ col2])

}
return(total)
}

add_vec= Vectorize(add)
ans= add_vec(d = df3, col1 = df$A, col2 = df$B )

resC = data.frame(df3, ans)
resC

Hi,
thanks so much, I managed to solve it and vectorized the function as it is part of my class homework. Thanks so much.

Btw, will it be possible to create the answer to have another column in the dataframe within the same function?

library(tidyverse)
df <- data.frame(A=1:10, B=2:11)
glimpse(df <- data.frame(A=1:10, B=2:11))


So I guess what you need to do to add the sum column is -

df <- df %>% mutate(sum_A_B= A+B)
df

If you needed more columns it would be -

df <- df %>% mutate(sum_A_B= A+B, difference=abs(A-B))
df

thanks. appreciate everyone's kind help and patience.