I have to run a function against all variables in a data frame, basically change wind direction in degrees to direction names. I have written a function that works if I give it individual values but not working against whole data frame.
Here is my function:
compute_aggregate_wind_dir <- function(wind_dir) {
if(wind_dir >= 0 && wind_dir <= 45) {
wind_dir = 'N' } else if(wind_dir > 45 && wind_dir <= 90) {
wind_dir = 'NE' } else if(wind_dir > 90 && wind_dir <= 135) {
wind_dir = 'E' } else if(wind_dir > 135 && wind_dir <= 180) {
wind_dir = 'SE' } else if(wind_dir > 180 && wind_dir <= 225) {
wind_dir = 'S' } else if(wind_dir > 225 && wind_dir <= 270) {
wind_dir = 'SW' } else if(wind_dir > 270 && wind_dir <= 315) {
wind_dir = 'W' } else {
wind_dir = 'NW' }
wind_dir
}
Here is my data frame:
wind_direction <- data.frame(
wind_dir = c(0,51,95,229,175)
)
# Print the data frame.
print(wind_direction)
Here is how I am calling it to update the direction in data frame:
wind_direction = compute_aggregate_wind_dir(wind_dir)
Its printing only NW when I print the results.
> print(wind_direction)
wind_dir
1 0
2 51
3 95
4 229
5 175
> wind_direction = compute_aggregate_wind_dir(wind_dir)
> wind_direction
[1] "NW"
What I want to do the whole data frame is updated with the direction names instead of degrees after calling the function-
How to make it working ?