New to R here and even coding. I have a character vector "change" in dataframe "df" that is made of values that I know require the input from an adjacent column.
change <- c("Apple","Bananas","Cheese")
I want to make it so in a new column "New" if the value in "ColumnA" matches any value in "change", then it gives me the value in "ColumnB", else it returns the value in "Column A".
╔══════════╦════════════╦════════════╗
║ ColumnA ║ ColumnB ║ New ║
╠══════════╬════════════╬════════════╣
║ Apples ║ ApplesNew ║ ApplesNew ║
║ Beer ║ BeerNew ║ Beer ║
║ Bananas ║ BananasNew ║ BananasNew ║
║ Bananas ║ BananasNew ║ BananasNew ║
║ Cheese ║ CheeseNew ║ CheeseNew ║
╚══════════╩════════════╩════════════╝
I tried this:
within(df, {
New <- ifelse(ColumnA %in% change, ColumnB, ColumnA)
}
)
Thanks in advance.