我在R中有一个data.frame / data.table,如下所示:
df <- data.frame(
ID=c(rep("A", 20)),
year=c(1968, 1971, 1972, 1973, 1974, 1976, 1978, 1980, 1982, 1984, 1985,
1986, 1987, 1988, 1990, 1991, 1992, 1993, 1994, 1995))
我希望将df分组,以便仅保留连续五年的条目。在这个例子中,这是两个时期的情况(1984:1988和1990:1995)。我怎么能在R中这样做?感谢您的支持。
答案 0 :(得分:10)
使用diff
和cumsum
:
setDT(df)[, grp := cumsum(c(0, diff(year)) > 1), by = ID
][, if (.N > 4) .SD, by = grp][, grp := NULL][]
给出了期望的结果:
ID year
1: A 1984
2: A 1985
3: A 1986
4: A 1987
5: A 1988
6: A 1990
7: A 1991
8: A 1992
9: A 1993
10: A 1994
11: A 1995
说明:
grp := cumsum(c(0, diff(year)) > 1), by = ID
为每个ID
创建连续年份的(临时)分组变量。if (.N > 4) .SD, by = grp
,您只能选择连续5年或更长时间的小组。grp := NULL
删除(临时)分组变量。基础R中的比较方法:
i <- with(df, ave(year, ID, FUN = function(x) {
r <- rle(cumsum(c(0, diff(year)) > 1));
rep(r$lengths, r$lengths)
} ))
df[i > 4,] # or df[which(i > 4),]
会得到相同的结果。
答案 1 :(得分:6)
这是另一种方式:
df2 <- NULL
sapply(seq(nrow(df)), function(x)
{
ifelse((sum(diff(df[x:(x+4), "year"], 1)) == 4 &
sum(diff(df[x:(x+4), "year"], 1) == 1) == 4),
df2 <<- rbind(df2, df[x:(x+4),]),"")
})
df2 <- unique(df2)
答案 2 :(得分:4)
我们可以尝试
data.table
或使用library(data.table)
i1 <- setDT(df)[, ind := (year - shift(year, fill= year[1L]))==1L , ID][,
{i1 <- .I[.N * ind > 3]
.(v1 = head(i1,1)-1, v2 = tail(i1, 1))},
.(ID, rl = rleid(ind))][, seq(v1, v2) , rl]$V1
df[, ind := NULL][i1]
# ID year
# 1: A 1984
# 2: A 1985
# 3: A 1986
# 4: A 1987
# 5: A 1988
# 6: A 1990
# 7: A 1991
# 8: A 1992
# 9: A 1993
#10: A 1994
#11: A 1995
i1 <- setDT(df)[, (shift(year, type="lead", fill = year[.N])-year)==1 |
(year - shift(year, fill = year[1L]))==1, ID][, .I[.N>4 & V1] , .(rleid(V1), ID)]$V1
df[i1]
或稍微紧凑的选项
df <- data.frame(
ID=c(rep("A", 20)),
year=c(1968, 1971, 1972, 1973, 1974, 1976, 1978, 1980, 1982, 1984, 1985,
1986, 1987, 1988, 1990, 1991, 1992, 1993, 1994, 1995))
{{1}}
答案 3 :(得分:2)
使用rowid
的另一个选项:
DT[, c("rl", "rw") := {
iscons <- cumsum(c(0L, diff(year)!=1L))
.(iscons, rowid(ID, iscons))
}]
DT[rl %in% DT[rw>=5L]$rl]
数据:
#adding one more group
DT <- rbindlist(list(setDT(df), copy(df)[, ID := "B"]))
答案 4 :(得分:0)
我首先对元素进行了排序:
navigationView.setItemTextColor(AppCompatResources.getColorStateList(this, R.color.menu_item_text_color));
重建保留的那些:
sorted = sort(df$year, decreasing = F)
count = 0 ## count sequences
keep=c() ## which to keep
keep_num = c() ##counting the sequence length
keep_count=1
for(i in 2:length(sorted)){
if((sorted[i]- sorted[i-1]) == 1){ ## if they are in a row
count = count + 1
if(count == 4){ ## if there 4+1 years involved in a row
keep=c(keep, sorted[i]- 4)
}
if(count >= 4){ ## if length more than 5, update
keep_num[keep_count]=count
}
}
else{ ##reset
count =0
keep_count = keep_count + 1
}
}
keep_num = keep_num[!is.na(keep_num)]
我们想要保留的内容中的子集:
y = c()
for(i in 1:length(keep)){
y = c(y, seq(keep[i], keep[i]+keep_num[i]))
}
这将选择具有所需条件的行。
selected = df[match(y, df$year, nomatch = 0),]
答案 5 :(得分:0)
步骤1。数据表中的数据&#34; d&#34;
d
hdrY mvanoyP
1: 1981 -14.3520324
2: 1982 0.4900168
3: 1983 2.6518741
4: 1984 5.2284595
5: 1985 -6.2874634
6: 1986 -1.3287914
7: 1987 20.6385345
8: 1988 24.2090114
9: 1989 21.5302571
10: 1990 9.0267066
11: 1991 10.4148838
12: 1992 13.9189716
13: 1993 7.8816196
14: 1994 3.4650221
15: 1995 2.8722555
16: 1996 -4.1442363
17: 1997 -3.2359926
18: 1998 -5.7479137
19: 1999 2.3481127
20: 2000 0.8089402
21: 2001 -14.4741916
22: 2002 -22.9272540
23: 2003 -27.3105212
24: 2004 -13.9726022
25: 2005 -14.0055281
26: 2006 -15.8456991
27: 2007 -21.0369933
28: 2008 -13.1031347
29: 2009 4.1517341
30: 2010 20.3711446
31: 2011 27.4202037
步骤2。选择nvanoyP&lt; 0并连续找到6年
d %<>% data.table()
db <- d[mvanoyP < 0, ] %>%
.[, tag := cumsum(c(0, diff(hdrY)) > 1)] %>%
.[, if (.N > 6) .SD,.(tag)] #
if (nrow(db) > 0){
db[, start := min(hdrY), by = tag]
db[, end := max(hdrY), by = tag]
}
db
步骤3。输出
db
tag hdrY mvanoyP start end
1: 3 2001 -14.47419 2001 2008
2: 3 2002 -22.92725 2001 2008
3: 3 2003 -27.31052 2001 2008
4: 3 2004 -13.97260 2001 2008
5: 3 2005 -14.00553 2001 2008
6: 3 2006 -15.84570 2001 2008
7: 3 2007 -21.03699 2001 2008
8: 3 2008 -13.10313 2001 2008