I am trying to replace values in a R dataframe by column. I would like to loop though a given list of columns of the dataframe and replace all "Yes" values by 1 and all the other values by 0.
I tried to do this using transform() and ifelse() functions with the something like this:
# List of selected Columns:
ColumnNames = c("Frigori", "Microond" , "Arca", "Aspira")
# Replace Values in dataframe
for(i in 1:length(ColumnNames)){
dataframe <- transform(dataframe, ColumnNames[i] = ifelse(Columnames[i] == "Yes", 1, 0))
}
This piece of code works fine with explicit column names outside the loop, but with the array it will give me the following error:
Error: unexpected = in:
"for(i in 1:length(Appliances)){
dataframe <- transform(dataframe, ColumnNames[i] ="
I don t know what goes wrong here, but the problem has to be related with the variable substitution.