【问题标题】:R-check the first and last record per day per id/ loopR-检查每个 id/循环每天的第一条和最后一条记录
【发布时间】:2014-05-22 07:06:12
【问题描述】:

如何查询同一ID内一天的FIRST和LAST记录?

样本数据:

sample<- data.frame(
id=c("A","A","A","A","A","C","C","C","D","D","E","E"),
location=c("US","US","US","US","SINGAPORE","CHINA","CHINA","JAPAN","JAPAN","JAPAN","SINGAPORE","SINGAPORE"),
Date =c("03/03/2013","03/03/2013","04/03/2013","04/03/2013","05/03/2013","03/03/2013","04/03/2013","04/03/2013","05/03/2013","06/03/2013","05/03/2013","05/03/2013")

) 

目前我尝试使用找出第一条和最后一条记录。 但似乎效果不太好。

尝试过的代码:

sample$FIRST <- !duplicated(sample$id)
sample$LAST<-FALSE
sample$LAST <- c(sample$id[-nrow(sample)]==sample$id[-1],TRUE)

如何实现代码以达到我的预期结果?

预期结果:

sample<- data.frame(
id=c("A","A","A","A","A","C","C","c","D","D","E","E"),
Date=c("03/03/2013","03/03/2013","04/03/2013","04/03/2013","05/03/2013","03/03/2013","04/03/2013","04/03/2013","05/03/2013","06/03/2013","05/03/2013","05/03/2013"),
FIRST =c("TRUE","FALSE","TRUE","FALSE","TRUE","TRUE","TRUE","FALSE","TRUE","TRUE","TRUE","FALSE"),
LAST =c("FALSE","TRUE","FALSE","TRUE","TRUE","TRUE","FALSE","TRUE","TRUE","TRUE","FALSE","TRUE")
)

提前谢谢... 注意:由于数据量很大,需要考虑优化...

【问题讨论】:

    标签: r loops optimization logic


    【解决方案1】:

    这是使用 dplyr 的一种方法:

    sample$Date <- as.Date(sample$Date, format="%d/%m/%Y")     #convert to dates
    
    require(dplyr)
    
    sample <- sample %.%                       #take the data.frame `sample` and store result of the following operation in `sample`. As I mentioned in the comment, the %.% chains several operations together.
      group_by(id, Date) %.%                   #now we make groups of the data for each combination of id and Date 
      mutate(count = 1:n(),                    #mutate adds new columns to sample. `count` counts the number of rows per group of id Date combination 
        FIRST = count == 1,                    #the column FIRST is TRUE if column count == 1
        LAST = count == max(count))            #the column LAST is TRUE if column count == maximum count per group, which is the last row
    
    sample$count <- NULL                     #since you dont need the count column, you can remove it later on
    

    输出是:

    #   id  location       Date FIRST  LAST
    #1   A        US 2013-03-03  TRUE FALSE 
    #2   A        US 2013-03-03 FALSE  TRUE
    #3   A        US 2013-03-04  TRUE FALSE
    #4   A        US 2013-03-04 FALSE  TRUE
    #5   A SINGAPORE 2013-03-05  TRUE  TRUE
    #6   C     CHINA 2013-03-03  TRUE  TRUE
    #7   C     CHINA 2013-03-04  TRUE FALSE
    #8   C     JAPAN 2013-03-04 FALSE  TRUE       <<-- this line had a lower case "c" as id in the input, I corrected it to capital C, otherwise it would produce a separate id
    #9   D     JAPAN 2013-03-05  TRUE  TRUE
    #10  D     JAPAN 2013-03-06  TRUE  TRUE
    #11  E SINGAPORE 2013-03-05  TRUE FALSE
    #12  E SINGAPORE 2013-03-05 FALSE  TRUE
    

    【讨论】:

    • 嗨,@beginneR 你为 %.% 使用了什么函数? [错误:找不到函数“%.%”],你能解释一下这些代码是如何工作的吗?谢谢!
    • @momoni 是包dplyr 的一部分。您必须安装该软件包并将其加载到您的库中。 %.% 运算符将多个操作链接在一起。包介绍见Introduction to dplyr
    • opps ,抱歉@beginneR 我更改了结果数据框的名称,它应该只有 1 个数据集
    • @momoni 当然,没问题。我将编辑我的答案并包含一些 cmets
    • 当我想安装包时它告诉我这个[包'dplyr'不可用(对于R版本3.0.1)]是我的R版本过时了吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-01
    • 1970-01-01
    • 2021-01-23
    相关资源
    最近更新 更多