dimanche 21 novembre 2021

add new column to dataframe with if, else statement

I would like to add a new column (class) to my data frame (counties) by using an if-else statement.

dataframe: 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:

screenshot

What am I doing wrong? Is there a better way to do this? TIA!

Aucun commentaire:

Enregistrer un commentaire