13

我有以下数据框:

id       <- c(1,1,2,3,3)
date     <- c("23-01-08","01-11-07","30-11-07","17-12-07","12-12-08")
df       <- data.frame(id,date)
df$date2 <- as.Date(as.character(df$date), format = "%d-%m-%y")


id     date      date2
1   23-01-08 2008-01-23
1   01-11-07 2007-11-01
2   30-11-07 2007-11-30
3   17-12-07 2007-12-17
3   12-12-08 2008-12-12

现在我需要创建第四列并在其中插入每个交易的最大日期id。决赛桌应如下:

id     date      date2        max
1   23-01-08 2008-01-23 2008-01-23
1   01-11-07 2007-11-01   0
2   30-11-07 2007-11-30 2007-11-30 
3   17-12-07 2007-12-17   0
3   12-12-08 2008-12-12 2008-12-12

如果您能帮我解决这个问题,我将不胜感激。

4

6 回答 6

21
id<-c(1,1,2,3,3)
date<-c("23-01-08","01-11-07","30-11-07","17-12-07","12-12-08")
df<-data.frame(id,date)
df$date2<-as.Date(as.character(df$date), format = "%d-%m-%y")
# aggregate can be used for this type of thing
d = aggregate(df$date2,by=list(df$id),max)
# And merge the result of aggregate 
# with the original data frame
df2 = merge(df,d,by.x=1,by.y=1)
df2

  id     date      date2          x
1  1 23-01-08 2008-01-23 2008-01-23
2  1 01-11-07 2007-11-01 2008-01-23
3  2 30-11-07 2007-11-30 2007-11-30
4  3 17-12-07 2007-12-17 2008-12-12
5  3 12-12-08 2008-12-12 2008-12-12

编辑:由于您希望在日期与最大日期不匹配时最后一列为“空”,因此您可以尝试下一行。

df2[df2[,3]!=df2[,4],4]=NA

df2
  id     date      date2          x
1  1 23-01-08 2008-01-23 2008-01-23
2  1 01-11-07 2007-11-01       <NA>
3  2 30-11-07 2007-11-30 2007-11-30
4  3 17-12-07 2007-12-17       <NA>
5  3 12-12-08 2008-12-12 2008-12-12

当然,清理 colnames 等总是很好的,但我把它留给你。

于 2012-08-26T02:23:37.373 回答
11

另一种方法是使用plyr包:

library(plyr)
ddply(df, "id", summarize, max = max(date2))

#  id        max
#1  1 2008-01-23
#2  2 2007-11-30
#3  3 2008-12-12

现在这不是您所追求的格式,因为它只显示id一次。不要害怕,我们可以使用transform代替summarize

ddply(df, "id", transform, max = max(date2))

#  id     date      date2        max
#1  1 01-11-07 2007-11-01 2008-01-23
#2  1 23-01-08 2008-01-23 2008-01-23
#3  2 30-11-07 2007-11-30 2007-11-30
#4  3 12-12-08 2008-12-12 2008-12-12
#5  3 17-12-07 2007-12-17 2008-12-12

正如@seandavi 的回答一样,这会重复max每个id. 如果您想将重复项更改为NA,则可以执行以下操作:

within(ddply(df, "id", transform, max = max(date2)), max[max != date2] <- NA)
于 2012-08-26T06:37:30.657 回答
8

添加dplyr解决方案以防有人正在寻找:

library(dplyr)

df %>%
  group_by(id) %>%
  mutate(max = if_else(date2 == max(date2), date2, as.Date(NA))) 

结果:

# A tibble: 5 x 4
# Groups:   id [3]
     id     date      date2        max
  <dbl>   <fctr>     <date>     <date>
1     1 23-01-08 2008-01-23 2008-01-23
2     1 01-11-07 2007-11-01         NA
3     2 30-11-07 2007-11-30 2007-11-30
4     3 17-12-07 2007-12-17         NA
5     3 12-12-08 2008-12-12 2008-12-12
于 2017-09-25T21:32:43.000 回答
2

您不能使用 0 作为 Date 值,因此您需要放弃将其保留为 Date 或接受 NA 值:

# Date values:
df$maxdt <- ave(df$date2, df$id, 
                    FUN=function(x) ifelse( x == max(x), as.character(x), NA) ) 
str(ave(df$date2, df$id, FUN=function(x) ifelse( x == max(x), as.character(x), NA) ) )
# Date[1:5], format: "2008-01-23" NA "2007-11-30" NA "2008-12-12"

机器做了一些奇怪的ifelse类型检查,使用x上面的第二个参数失败了,但仍然返回日期类向量。去搞清楚!下面是字符向量选项。

# Character values:
df$maxdt <- ave(as.character(df$date2), df$id, 
                   FUN=function(x) ifelse( x == max(x), x,  "0") )
ave(as.character(df$date2), df$id, FUN=function(x) ifelse( x == max(x), x,  "0") )
[1] "2008-01-23" "0"          "2007-11-30" "0"          "2008-12-12"
于 2012-08-26T03:46:03.077 回答
2
library(sqldf)
tables<- '(SELECT * FROM df
           )
           AS t1,
           (SELECT id,max(date2) date2 FROM df GROUP BY id
           )
           AS t2'

out<-fn$sqldf("SELECT t1.*,t2.date2 mdate FROM $tables WHERE  t1.id=t2.id")
out$mdate<-as.Date(out$mdate)
out$mdate[out$date2!=out$mdate]<-NA
#  id     date      date2      mdate
#1  1 01-11-07 2007-11-01       <NA>
#2  1 23-01-08 2008-01-23 2008-01-23
#3  2 30-11-07 2007-11-30 2007-11-30
#4  3 12-12-08 2008-12-12 2008-12-12
#5  3 17-12-07 2007-12-17       <NA>
于 2012-08-26T03:18:11.163 回答
0

当我想查看列的最小/最大日期时,我发现这会有所帮助

最大:head(df %>% distinct(date) %>% arrange(desc(date)))
最小:head(df %>% distinct(date) %>% arrange(date))

最大值将按降序对日期列进行排序,以便您查看最大值。最小值将按升序排序,以便您查看最小值。

您需要为此使用该dplyr软件包。

于 2018-07-25T22:25:31.177 回答