rtidyverseaggregate

Counting observations by group using collapse R package


I want to translate the following R code from tidyverse to collapse. The following code count observations by group and append as a column to the data.frame.

library(tidyverse)
library(collapse)
head(wlddev)

wlddev %>% 
  group_by(income) %>% 
  add_count(., name = "Size") %>% 
  select(country, income, Size) %>% 
  distinct()
# A tibble: 216 x 3
# Groups:   income [4]
   country             income               Size
   <chr>               <fct>               <int>
 1 Afghanistan         Low income           1830
 2 Albania             Upper middle income  3660
 3 Algeria             Upper middle income  3660
 4 American Samoa      Upper middle income  3660
 5 Andorra             High income          4819
 6 Angola              Lower middle income  2867
 7 Antigua and Barbuda High income          4819
 8 Argentina           Upper middle income  3660
 9 Armenia             Upper middle income  3660
10 Aruba               High income          4819
# ... with 206 more rows

Now want to accomplish the same task with collapse R package.

The following code works as expected.

wlddev %>%
  fgroup_by(income) %>%
  fselect(country) %>% 
  fnobs()

               income country
1         High income    4819
2          Low income    1830
3 Lower middle income    2867
4 Upper middle income    3660

However, not able to append the column to original data.frame.

wlddev %>%
  fgroup_by(income) %>%
  fselect(country) %>% 
  fnobs() %>% 
  ftransform(.data = wlddev, Size = .)

Error in ftransform_core(.data, e) : 
  Lengths of replacements must be equal to nrow(.data) or 1, or NULL to delete columns

Any hints, please.


Solution

  • Unlike add_count which creates a column in the original data, the fnobs is a summarised data, which we can join

    library(collapse)
     wlddev %>% 
        fgroup_by(income) %>%
        fselect(country) %>%   
        fnobs() %>% 
        rename(size = country) %>% 
       left_join(wlddev %>% 
          slt(country, income), .) %>% 
       distinct