I would like to add a new column (class) to my data frame (counties) by using an if-else statement.
dataframe:
here's my code
#set working directory
setwd("C:/Users/weirc/OneDrive/Desktop/Undergrad Courses/Fall 2021 Classes/GHY 3814/final project/data")
#load packages
library(readr)
library(dplyr)
#load data
counties <- read_csv("vaxData_counties.csv")
calc <- if (counties$Series_Complete >= 75) {
print(4)
} else if (counties$Series_Complete >= 50) {
print(3)
} else if (counties$Series_Complete >= 25) {
print(2)
}else print(1)
#create new column for class
counties %>% mutate(class=calc)
here's a screenshot of the error I receive in the console:
What am I doing wrong? Is there a better way to do this? TIA!
CodePudding user response:
I'm not sure about your data, but in your if-else if-else
statement, conditions like counties$Series_Complete >= 75
are now comparing whole vector with single value, and if using print
, it may not give you proper result. Instead, try using dplyr::case_when
library(dplyr)
counties %>%
mutate(class = case_when(
Series_Complete >=75 ~ 4,
Series_Complete >= 50 ~ 3,
Series_Complete >= 25 ~ 2,
TRUE ~ 1
))
CodePudding user response:
I like @Park's answer. If you didn't want to use dplyr
though, you could use ifelse
. You can chain ifelse
to mimic the behavior of case when
statements: https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/ifelse
counties$class <- ifelse(counties$Series_Complete >= 75, 4,
ifelse(counties$Series_Complete >= 50, 3,
ifelse(counties$Series_Complete >= 25, 2, 1)))