提取与R中某个逻辑值相对应的所有行。

时间:2018-10-23 19:57:15

标签: r

我使用以下代码生成了一个数据框:

# Load package
library(tidyverse)

# Dataframe example
f.name<-c("A","A","A","B","B","C","C")
l.name<-c("X","X","X","Z","Z","V","V")
grade<-c(8,9,10,9,10,9,10)
year<-c(2013,2014,2015,2014,2015,2016,2017)

df<-data.frame(f.name,l.name,grade,year)
df


new.c<-df %>%
  group_by(f.name,l.name)%>%
  mutate(new.9=(grade==9 & year==2014 & row_number()==1))


  f.name l.name grade  year new.9
  <fct>  <fct>  <dbl> <dbl> <lgl>
1 A      X         8. 2013. FALSE
2 A      X         9. 2014. FALSE
3 A      X        10. 2015. FALSE
4 B      Z         9. 2014. TRUE 
5 B      Z        10. 2015. FALSE
6 C      V         9. 2016. FALSE
7 C      V        10. 2017. FALSE

有条件地在new.9列中找到TRUE值时,我想从数据框中提取与该TRUE值相对应的所有父记录。这样我就得到

4 B      Z         9. 2014. TRUE 
5 B      Z        10. 2015. FALSE

我该怎么做?

3 个答案:

答案 0 :(得分:0)

new.c %>%
  group_by(f.name, l.name) %>%
  filter(any(new.9))
  %>% ungroup()
# # A tibble: 2 x 5
#   f.name l.name grade  year new.9
#   <fct>  <fct>  <dbl> <dbl> <lgl>
# 1 B      Z          9  2014 TRUE 
# 2 B      Z         10  2015 FALSE

答案 1 :(得分:0)

以R为底

dfl <- split(new.c, new.c$l.name)
# this splits new.c into a data frame list - splitting by the column `l.name`

# select only those sub data frame in the data frame list
# which contain any TRUE values in the last column
# and by `Reduce(rbind, ...)`-ing the result, you make again a data frame out of
# the resulting data frame list -> so that you have a 
# data frame (tibble) at the end.
Reduce(rbind, dfl[sapply(dfl, function(df) any(df$l.name == TRUE))])

答案 2 :(得分:0)

filter并执行inner_join

new.c %>% filter(new.9 == TRUE) %>% 
    select(f.name,l.name) %>% 
inner_join(new.c)