我有以下数据框df
time <- c("01/01/1951", "02/01/1951", "03/01/1951", "04/01/1951", "05/01/1951", "06/01/1951", "07/01/1951", "08/01/1951", "09/01/1951", "10/01/1951", "11/01/1951", "12/01/1951", "13/01/1951", "14/01/1951", "15/01/1951", "16/01/1951", "17/01/1951", "18/01/1951", "19/01/1951", "20/01/1951", "21/01/1951", "22/01/1951", "23/01/1951")
member <- c(1,NA,NA,3,NA,NA,NA,NA,NA,1,1,NA,2,NA,NA,NA,NA,NA,1,NA,NA,NA,NA)
df <- data.frame(time, member)
df$time = as.Date(df$time,format="%d/%m/%Y")
我喜欢&#34;会员&#34;的NA值。在成员为1成为0的一天之前,除了1之前的一天(连续两个),我不希望1成为0,只是在1之前的NA值。
所需的数据框将是:
df
time member
1 01/01/1951 1
2 02/01/1951 NA
3 03/01/1951 NA
4 04/01/1951 3
5 05/01/1951 NA
6 06/01/1951 NA
7 07/01/1951 NA
8 08/01/1951 NA
9 09/01/1951 0
10 10/01/1951 1
11 11/01/1951 1
12 12/01/1951 NA
13 13/01/1951 2
14 14/01/1951 NA
15 15/01/1951 NA
16 16/01/1951 NA
17 17/01/1951 NA
18 18/01/1951 0
19 19/01/1951 1
20 20/01/1951 NA
21 21/01/1951 NA
22 22/01/1951 NA
23 23/01/1951 NA
想法?
答案 0 :(得分:0)
因此我们需要检查df$member
是否为NA
且下一个值为1.当两者都为真时,我们将df$member
设为0:
df$member[is.na(df$member) & c(df$member[-1] == 1, FALSE)] = 0
df
# time member
# 1 1951-01-01 1
# 2 1951-01-02 NA
# 3 1951-01-03 NA
# 4 1951-01-04 3
# 5 1951-01-05 NA
# 6 1951-01-06 NA
# 7 1951-01-07 NA
# 8 1951-01-08 NA
# 9 1951-01-09 0
# 10 1951-01-10 1
# 11 1951-01-11 1
# 12 1951-01-12 NA
# 13 1951-01-13 2
# 14 1951-01-14 NA
# 15 1951-01-15 NA
# 16 1951-01-16 NA
# 17 1951-01-17 NA
# 18 1951-01-18 0
# 19 1951-01-19 1
# 20 1951-01-20 NA
# 21 1951-01-21 NA
# 22 1951-01-22 NA
# 23 1951-01-23 NA