根据另一列中的条件指定新列

时间:2018-04-21 12:33:46

标签: r sas

我正在为1890年以来的学校的学年注册数据工作,目前有月份(作为数字)和年份的列。我想找到一种方法将这些价值观分组到学年,以便8月至4月都来自同一学年。例如,8 / 2010-4 / 2011将来自2010学年。在SAS中,我会使用下面的代码但是我无法使用我的R代码工作,我不确定我缺少什么。我为我的R代码道歉,我还在学习。一个 SAS代码:

If Month="8" or Month="9" or Month= "10" or Month= "11" or Month="12" then SchoolYear=Year;
If Month= "1" or Month="2" or Month="3" or Month="4" then SchoolYear= Year-1;
If Month="5"  or Month="6" or Month="7"  then SchoolYear= "";

R代码和相应的错误:

for (i in nrow(df)) if(df$Month == 8 | df$Month == 9 |df$Month ==10| df$Month ==11 | df$Month == 12) {df$SchoolYear == df$Year} else if (df$Month == 1 | df$Month == 2 | df$Month == 3 | df$Month == 4) {df$SchoolYear == df$Year- 1} else {df$SchoolYear == "NA"}

the condition has length > 1 and only the first element will be used the condition has length > 1 and only the first element will be used

1 个答案:

答案 0 :(得分:1)

我们可以使用%in%进行多元素比较

library(dplyr)
df %>% 
  mutate(SchoolYear = case_when(Month %in% 8:12 ~ Year, 
                        Month %in% 1:4 ~  Year - 1L, 
                        Month %in% 5:7 ~ NA_integer_))

基于逻辑,可以进一步简化为

df$SchoolYear <- with(df,  (NA^(Month %in% 5:7)* Year) - (Month %in% 1:4))

数据

set.seed(24)
df <- data.frame(Month = sample(1:12, 30, replace = TRUE),
     Year = sample(1978:2001, 30, replace = TRUE))