r - Ungroup after grouping by just one variable in dplyr -


i have lot of units measured repeated times

>df item value  year 1     20     1990 1     20     1991 2     30     1990 2     15     1990 2     5      1991 3     10     1991 4     15     1990 5     10     1991 5      5     1991 

i trying use dplyr remove values have low number of observations. on toy data lets want remove data has fewer 2 counts

>df <- df %>%    group_by(item) %>%    tally() %>%    filter(n>1)  item  n 1     2 2     3 5     2 

the problem expand was, filter. attempted using ungroup command, seems have effect when grouping 2 variables. how can filter item counts original variables i.e value , year. should this

>df item value  year 1     20     1990 1     20     1991 2     30     1990 2     15     1990 2     5      1991 5     10     1991 5      5     1991 

more simply, use dplyr's row_number()

library(dplyr)  df <- read.table("clipboard", header = true, stringsasfactors = false)  df %>%    group_by(item) %>%    filter(max(row_number()) > 1) %>%   ungroup()  # tibble: 7 x 3 # groups:   item [3]    item value  year   <int> <int> <int> 1     1    20  1990 2     1    20  1991 3     2    30  1990 4     2    15  1990 5     2     5  1991 6     5    10  1991 7     5     5  1991 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -