I have a dataset with 460K observations loaded into a data frame named data. One of the variables is defined as follows:
$ exeroft1 <int> NA, 105, NA, 205, NA, 102, 220, 102, 102, 220, 230, NA, NA, 105, 102, 210, 203, NA, NA, 107, 103, NA, 203, NA, NA, 105, 107, NA, 102, NA, 107, NA, 107, 103, ... I need to pass each value of exeroft1 to the following function, which converts the value into another value:
calculateWeeklyExercise <- function(value) { if (value > 200) { timesWeekly = (value - 200) / 4 } else { timesWeekly = (value - 100) } timesWeekly } Here is some R code that does all the processing:
data %>% # Filter missing values filter(!is.na(exeroft1)) %>% # Add a column to the data frame which represents exercise rate mutate(weeklyExercise = calculateWeeklyExercise(exeroft1)) %>% # Select some values select(educa, sex, exeroft1, weeklyExercise) When I execute this code, I get the following warning, which I do not understand:
Warning message: In if (value > 200) { : the condition has length > 1 and only the first element will be used I'm not very experienced with R. It seems that the value I'm passing to the function is not being treated as a integer, even though it is. For any value < 200, the correct value is calculated. For any value > 200, it's not. So, essentially, in the function, only the else clause seems to ever get executed.
if/elseproblem when you have length > 1. UseifelseOr if we are applying for each row, thendata %>% # Filter missing values filter(!is.na(exeroft1)) %>% rowwise() %>%>and do it.if(1:3 >2) 1get the same warningcalculateWeeklyExercise <- Vectorize(calculateWeeklyExercise)and run again. but you need a condition to handle NA in this case