从数据框中提取每个主题的最后一行

时间:2013-02-16 05:27:47

标签: r

我在R中有一个像这样的数据框。我想提取每个主题的最后一次访问。

SUBJID VISIT

   40161       3  
   40161       4  
   40161       5  
   40161       6  
   40161       9  
   40201       3  
   40202       6  
   40202       8  
   40241       3  
   40241       4 

所需的输出如下

SUBJID VISIT

   40161     9  
   40201     3  
   40202     8  

我应该如何在R中执行此操作?非常感谢您的帮助。

7 个答案:

答案 0 :(得分:6)

虽然agstudy是正确的,但stats包和聚合函数还有另一种方法。

df <- read.table(text="SUBJID VISIT
40161 3
40161 4
40161 5
40161 6
40161 9
40201 3
40202 6
40202 8
40241 3
40241 4", header=TRUE)


aggregate(VISIT ~ SUBJID, df, max)

  SUBJID VISIT
1  40161     9
2  40201     3
3  40202     8
4  40241     4

答案 1 :(得分:4)

要显示另一种选择,因为我喜欢其语法的简单性,您也可以使用data.table。假设您的data.frame被称为“df”:

library(data.table)
# data.table 1.8.7  For help type: help("data.table")
DT <- data.table(df, key = "SUBJID")
DT[, list(VISIT = max(VISIT)), by = key(DT)]
#    SUBJID V1
# 1:  40161  9
# 2:  40201  3
# 3:  40202  8
# 4:  40241  4

而且,虽然我们在R中分享了许多方法,但如果您对SQL语法感到满意,也可以使用sqldf,如下所示:

library(sqldf)
sqldf("select SUBJID, max(VISIT) `VISIT` from df group by SUBJID")
  SUBJID VISIT
1  40161     9
2  40201     3
3  40202     8
4  40241     4

答案 2 :(得分:3)

因为我们可以,另一个基本选项:

 do.call(rbind,
         lapply(split(dat, dat$SUBJID), 
                function(x) tail(x$VISIT, 1) ) )
#      [,1]
#40161    9
#40201    3
#40202    8
#40241    4

修改

正如@BenBolker建议的那样:

 do.call(rbind,
             lapply(split(dat, dat$SUBJID), 
                    function(x) tail(x, 1) ) )
如果您有更多列,

应适用于所有列。

答案 3 :(得分:1)

使用plyr包作为例子:

 ddply(dat,.(SUBJID),summarise,VISIT=tail(VISIT,1))
  SUBJID VISIT
1  40161     9
2  40201     3
3  40202     8
4  40241     4

dat是:

dat <- read.table(text ='SUBJID VISIT
40161 3
40161 4
40161 5
40161 6
40161 9
40201 3
40202 6
40202 8
40241 3
40241 4',head=T)

答案 4 :(得分:1)

以下是diff的简单解决方案:

dat[c(diff(dat$SUBJID) != 0, TRUE), ]

   SUBJID VISIT
5   40161     9
6   40201     3
8   40202     8
10  40241     4

也可以使用by

do.call(rbind, by(dat, dat$SUBJID, tail, 1))

      SUBJID VISIT
40161  40161     9
40201  40201     3
40202  40202     8
40241  40241     4

答案 5 :(得分:1)

它也可以使用sqldf软件包,     库(sqldf)

sqldf("SELECT SUBJID, MAX(VISIT) From df GROUP BY by SUBJID")

  SUBJID VISIT
1  40161     9
2  40201     3
3  40202     8
4  40241     4

答案 6 :(得分:0)

或者(带有@agstudy的数据)

 plugins: [
        new HtmlWebpackPlugin({
            template: 'src/index.html'
        }),
        // Copy Cesium Assets, Widgets, and Workers to a static directory
        new CopywebpackPlugin([ { from: path.join(cesiumSource, cesiumWorkers), to: 'Workers' } ]),
        new CopywebpackPlugin([ { from: path.join(cesiumSource, 'Assets'), to: 'Assets' } ]),
        new CopywebpackPlugin([ { from: path.join(cesiumSource, 'Widgets'), to: 'Widgets' } ]),
        new webpack.DefinePlugin({
            // Define relative base path in cesium for loading assets
            CESIUM_BASE_URL: JSON.stringify('')
        })
    ],

或带有data.table

g <- grouping(df$SUBJID)
df[g[attr(g, "ends")],]

   SUBJID VISIT
5   40161     9
6   40201     3
8   40202     8
10  40241     4