【发布时间】:2021-12-31 16:45:18
【问题描述】:
我想使用 if-else 语句向我的数据框(县)添加一个新列(类)。
数据框:
这是我的代码
#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)
这是我在控制台中收到的错误的屏幕截图:
我做错了什么?有一个更好的方法吗?蒂亚!
【问题讨论】:
标签: r dataframe if-statement data-manipulation