1

我有一些事件我试图根据发生的顺序在我的数据中枚举,但条件是另一个变量(在这种情况下Date)没有丢失。我有点卡住了。我的问题是我只需要使用 base-R 来完成它。

这是一个工作示例:

df <-
structure(list(Date = c("Sep 02 2012", "Sep 10 2012", "Sep 22 2012", 
"Sep 23 2012", "Sep 23 2012", "Sep 23 2012", "Sep 24 2012", "Sep 24 2012", 
"Sep 24 2012", "Dec 01 2012", "Sep 09 2014", NA), Event = c("005", 
"006", "002", "003", "004", "007", "008", "010", "011", "012", 
"009", "001"), Type = c("IND", "IND", "CON", "OUT", "OUT", "IND", 
"CMA", "ASH", "CON", "ASH", "CMA", "IND")), .Names = c("Date", 
"Event", "Type"), class = "data.frame", row.names = c(13L, 14L, 
10L, 11L, 12L, 15L, 16L, 18L, 19L, 20L, 17L, 9L))

给我这个数据框,

df
          Date Event Type
13 Sep 02 2012   005  IND
14 Sep 10 2012   006  IND
10 Sep 22 2012   002  CON
11 Sep 23 2012   003  OUT
12 Sep 23 2012   004  OUT
15 Sep 23 2012   007  IND
16 Sep 24 2012   008  CMA
18 Sep 24 2012   010  ASH
19 Sep 24 2012   011  CON
20 Dec 01 2012   012  ASH
17 Sep 09 2014   009  CMA
9         <NA>   001  IND

我想得到什么,

new.df

       Date    Event Type Num_Type
13 Sep 02 2012   005  IND       1
14 Sep 10 2012   006  IND       2
10 Sep 22 2012   002  CON       1
11 Sep 23 2012   003  OUT       1
12 Sep 23 2012   004  OUT       2
15 Sep 23 2012   007  IND       3
16 Sep 24 2012   008  CMA       1
18 Sep 24 2012   010  ASH       1
19 Sep 24 2012   011  CON       1
20 Dec 01 2012   012  ASH       2
17 Sep 09 2014   009  CMA       2
9         <NA>   001  IND      NA
4

2 回答 2

2

你可以用aveand做到这一点cumsum

df$Num_Type <- 1
df$Num_Type <- ave(df$Num_Type, df$Type, FUN=cumsum)
is.na(df$Num_Type) <- which(is.na(df$Date))
于 2012-12-05T21:25:23.083 回答
1

你可以先订购df你想要的变量吗?像:

df <- df[order(df$Event, df$Type),]

然后...

df$Num_Type  <- sequence(rle(as.character(df$Type))$lengths)
于 2012-12-05T21:18:52.280 回答