我有这个数据框:
w$disease<-c(1,0,1,0,1,1,0,0,1,0,1,1,1,0,0,0,1,0,1,0,1,0,1,0,1,1,0,0)
w$location<-c("A","A","A","A","A","A","A","A","A","A","A","B","B","B","B","A","B","B","C","C","D", "D","D","D","A","D","A","D")
w$date<-c("2010-01-01","2010-01-01","2010-05-01","2010-05-01","2010-07-01","2010-07-01","2010-07-01","2010-07-01","2010-08-02","2010-08-02","2010-08-07","2010-08-07","2010-08-07","2010-08-07","2010-08-07","2010-08-09","2010-10-02","2010-10-02","2011-01-15","2011-01-15","2011-02-14","2011-02-14","2012-07-14","2011-07-14","2012-01-02","2012-01-02","2012-01-02","2012-01-02")
我想订购w,以便m符合以下数据
w$m<-c(1,1,2,2,3,4,3,4,5,5,6,7,8,6,7,8,9,9,10,10,11,11,12,12,13,14,13,14)
其中
w[1] is 1 A "2010-01-01" and w$m[1] is 1,
then w[2] is 0 A "2010-01-01" and w$m[2] is 1,
w[28] is 0 D "2012-01-02" and w$m[28] is 14
这就是我写的
w$pd<-do.call(paste, c(w[c("date","location")], sep=" "))
w<-w[order(w$pd),]
j<-1
m<-1
for(i in 1:length(w$disease)){
if(w$disease[i]==1){
m[i]<-j
j<-ifelse(w$d[i+1]==0,j+1,1)
}else{
m[i]<-j
j<-ifelse(w$d[i+1]==1,j+1,j)
}
}
这一直有效,直到我进入有1,1或0,0实例的阶段。希望这是任何一个清洁工。任何的想法? 非常感谢, 马可
答案 0 :(得分:2)
IIUC(仅通过查看答案),使用ave
- R附带的内置函数:
w$m <- with(w, ave(disease, disease, FUN=seq_along))
> w$m
# [1] 1 1 2 2 3 4 3 4 5 5 6 7 8 6 7 8 9 9 10 10 11 11 12 12 13 14 13 14
另一种方式:
使用data.table
(外部包),您可以使用以下命令安装:
# installation can be done by
install.packages("data.table")
## load the package
require(data.table) ## 1.9.2
setDT(w)[, m := 1:.N, by=disease]
.N
是一个特殊变量,包含每个组的元素数量(此处为disease
)。 setDT
会将您的data.frame
转换为data.table
。
修改:如果setDT
不起作用,请在控制台中运行update.packages("data.table")
并重试
答案 1 :(得分:0)
使用dplyr
library(dplyr)
w%>%
group_by(disease) %>%
mutate(n=row_number())%>%
subset(select=n, drop=TRUE)
#[1] 1 1 2 2 3 4 3 4 5 5 6 7 8 6 7 8 9 9 10 10 11 11 12 12 13
#[26] 14 13 14