So I understand how the apply function should be used but I am not sure how to integrate with an IF statement. Here is my attempt and can someone please push me in the right direction:
data <- mtcars
apply(data, 1, function(x) {
if (data$mpg < 20) {
data$colour <- "blue"
} else {
data$colour <- "red"
}
})
I just want to add a column to data for each row of the data frame for values in data$mpg between certain ranges.
sapplynotapplyorapplywithin a margin of 2, applying onmpg.