Dominique Paul Dominique Paul - 12 days ago 6
R Question

R function executes without error but doesnt change column content in data frame

data3 is a data frame with a column called new of which all elements are 1. The function should change all values of the column new to 2. When executing the code there is no error but the value of the column "new" is still 1.

When only executing data3$new = 2 everything works

insertElementToColumn = function(){
data3$new = 2
}
insertElementToColumn(new, data3)

Answer

Try this:

data3 = c(1,2,3,4,5)
data3 = data.frame(data3)
data3$new = c(1,1,1,1,1)
insertElementToColumn = function(x, column){
  data3$new = 2
  data3
}
data3 = insertElementToColumn(new, data3)