基于在另一列上执行的减法创建新列

时间:2018-01-28 23:35:39

标签: r

我有这样的数据集

    day   visibility
 1    01      10000
 2    02      15000
 3    03      15000
 4    04       6000
 5    07      15000
 6    08      15000
 7    09      15000
 8    12      10000
 9    13      15000
10    20      10000
11    31      10000
12    01      10000
13    02       5000

我想要创建一个新列yesterday_visibility,如果visibility[i]则为day[i + 1] - day[i] == 1,否则为NA

类似这样的事情

  day   visibility yesterday_visibility
 1    01      10000  NA
 2    02      15000  10000
 3    03      15000  15000
 4    04       6000  15000
 5    07      15000  NA
 6    08      15000  15000
 7    09      15000  15000
 8    12      10000  NA
 9    13      15000  10000
10    20      10000  NA
11    31      10000  NA
12    01      10000  10000
13    02       5000  10000

我试过这个

mutate(data, yesterday_visibility = 
  for(i in 1:NROW(data$day)){
    ifelse((as.numeric(data$day[i + 1]) - as.numeric(data$day[i])) == 1, 
                                          data$visibility[i], NA )       
})

3 个答案:

答案 0 :(得分:2)

尝试此操作(假设您的数据为df):

df$yesterday_visibility <- NA # create the new column filled with NA
inds <- which(diff(c(-Inf, df$day, Inf))==1) # find consecutive days
df$yesterday_visibility[inds] <- df$visibility[inds-1] # update the new column

   # day visibility yesterday_visibility
# 1    1      10000                   NA
# 2    2      15000                10000
# 3    3      15000                15000
# 4    4       6000                15000
# 5    7      15000                   NA
# 6    8      15000                15000
# 7    9      15000                15000
# 8   12      10000                   NA
# 9   13      15000                10000
# 10  20      10000                   NA

答案 1 :(得分:1)

这样可行

dat <- data.frame(day = c(01, 02, 03, 04, 07, 08, 09, 12, 13, 20),
                  visibility = c(10000, 15000, 15000, 6000, 15000, 15000, 15000, 10000, 15000, 10000))

dat$New <- ifelse(dat$day - c(NA, dat$day[1:(nrow(dat)-1)]) == 1, c(NA, dat$day[1:(nrow(dat)-1)]), NA)
apply(dat, 1, function(x) if (!is.na(x[3])) dat$visibility[dat$day==x[3]] else NA)


dat

   day visibility   New
1    1      10000    NA
2    2      15000 10000
3    3      15000 15000
4    4       6000 15000
5    7      15000    NA
6    8      15000 15000
7    9      15000 15000
8   12      10000    NA
9   13      15000 10000
10  20      10000    NA

答案 2 :(得分:1)

   dat%>%mutate(yest=replace(d<-c(F,diff(day))==1,!d,NA),vis_yest=lag(visibility)*yest)

   day visibility yest vis_yest
1    1      10000   NA      NA
2    2      15000 TRUE   10000
3    3      15000 TRUE   15000
4    4       6000 TRUE   15000
5    7      15000   NA      NA
6    8      15000 TRUE   15000
7    9      15000 TRUE   15000
8   12      10000   NA      NA
9   13      15000 TRUE   10000
10  20      10000   NA      NA