避免if else声明

时间:2017-07-04 15:06:33

标签: r if-statement

我有以下ifelse声明:

Years=c(2016, 2021, 2026, 2031, 2035)
if (Year == Years[1]){OD = subset(data,data$YEAR>=Years[1] & data$YEAR <= Years[1]+2)}
if (Year == Years[2]){OD = subset(data,data$YEAR>=Years[2]-2 & data$YEAR <= Years[2]+2)}
if (Year == Years[3]){OD = subset(data,data$YEAR>=Years[3]-2 & data$YEAR <= Years[3]+2)}
if (Year == Years[4]){OD = subset(data,data$YEAR>=Years[4]-2 & data$YEAR <= Years[4]+2)}
if (Year == Years[5]){OD = subset(data,data$YEAR>=Years[5]-1 & data$YEAR <= Years[5])}

我想在没有ifelse声明的情况下写一个有效的方法。

2 个答案:

答案 0 :(得分:5)

我不确定用于定义年份下限和上限的规则是什么,但是如何首先创建定义下限和上限的向量:

Years <- c(2016, 2021, 2026, 2031, 2035)
Years.lower <- c(2016, 2021-2, 2026-2, 2031-2, 2035-1)
Years.upper <- c(2016+2, 2021+2, 2026+2, 2031+2, 2035)

然后根据对象data对应的Years对应的子集Year

OD <- subset(data, data$YEAR>=Years.lower[which(Years==Year)] & data$YEAR <= Years.upper[which(Years==Year)])

答案 1 :(得分:1)

根据我的理解,你正在以第一个数据点为中心的5年片段切片数据,并在最后一个数据点上设置上限,所以我希望这样做:

Years=c(2016, 2021, 2025) # shortenned for the demo
set.seed(1)
data = data.frame(Year = sample(2010:2030,20,replace=TRUE),anything=sample(letters,20,replace=TRUE))

# Year anything
# 1  2015        y
# 2  2017        f
# 3  2022        q
# 4  2029        d
# 5  2014        g
# 6  2028        k
# 7  2029        a
# 8  2023        j
# 9  2023        w
# 10 2011        i
# 11 2014        m
# 12 2013        p
# 13 2024        m
# 14 2018        e
# 15 2026        v
# 16 2020        r
# 17 2025        u
# 18 2030        c
# 19 2017        s
# 20 2026        k

# for all of the years of your data we assign a number, the id of the slice it'll be attributed to
group0 <- (data$Year - (min(Years)-7)) %/% 5
# it can contain negative numbers and slices after your max year though so we fix that
group0[data$Year > max(Years) | data$Year < min(Years) ] <- NA
# now we can assign a group to all your rows
data$group <- Years[group0]

#    Year anything group
# 1  2015        y  2016
# 2  2017        f  2016
# 3  2022        q  2021
# 4  2029        d    NA
# 5  2014        g  2016
# 6  2028        k    NA
# 7  2029        a    NA
# 8  2023        j  2021
# 9  2023        w  2021
# 10 2011        i    NA
# 11 2014        m  2016
# 12 2013        p    NA
# 13 2024        m  2025
# 14 2018        e  2016
# 15 2026        v    NA
# 16 2020        r  2021
# 17 2025        u  2025
# 18 2030        c    NA
# 19 2017        s  2016
# 20 2026        k    NA


# and subset it
OD_list <- lapply(Years,function(x){subset(data,group == x)})

# [[1]]
# Year anything group
# 1  2015        y  2016
# 2  2017        f  2016
# 5  2014        g  2016
# 11 2014        m  2016
# 14 2018        e  2016
# 19 2017        s  2016
# 
# [[2]]
# Year anything group
# 3  2022        q  2021
# 8  2023        j  2021
# 9  2023        w  2021
# 16 2020        r  2021
# 
# [[3]]
# Year anything group
# 13 2024        m  2025
# 17 2025        u  2025