简体   繁体   English

如何根据最后两行的值对 data.frame 进行子集化?

[英]How to subset a data.frame according to the values of last two rows?

###the original data ###原始数据

df1 <- data.frame(a=c(2,2,5,5,7), b=c(1,5,4,7,6))
df2 <- data.frame(a=c(2,2,5,5,7,7), b=c(1,5,4,7,6,3))

when the a column value of the last two rows are not equal (here the 4th row is not equal to the 5th row, namely, 5!=7), I want to subset the last row only.当最后两行的a列值不相等时(这里第4行不等于第5行,即5!=7),我只想对最后一行进行子集化。

#input #输入

 > df1
      a b
    1 2 1
    2 2 5
    3 5 4
    4 5 7
    5 7 6

#output #输出

> df1
  a b
1 7 6

when the a column value of the last two rows are equal (here 5th row is equal to the 6th row, namely, 7=7, I want to subset the last two rows当最后两行的a列值相等时(这里第5行等于第6行,即7=7,我想对最后两行进行子集

#input #输入

> df2
  a b
1 2 1
2 2 5
3 5 4
4 5 7
5 7 6
6 7 3

#output #输出

> df2
  a b
1 7 6
2 7 3

You can write a function to check last two row values for a column :您可以编写一个函数来检查最后两个行值的a列:

return_rows <- function(data) {
  n <- nrow(data)
  if(data$a[n] == data$a[n - 1])
      tail(data, 2)
  else tail(data, 1)
}

return_rows(df1)
#  a b
#5 7 6

return_rows(df2)
#  a b
#5 7 6
#6 7 3

try it this way试试这个方法

library(tidyverse)
df %>% 
  filter(a == last(a))

  a b
5 7 6

  a b
5 7 6
6 7 3

我们可以使用base R subset

subset(df1, a == a[length(a)])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM