使用塌陷R包按组计算观察值

问题描述 投票:0回答:3

我想将以下 R 代码从

tidyverse
翻译为
collapse
。以下代码按组对观察值进行计数,并将其作为一列附加到 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

现在想用

collapse R
包完成相同的任务。

以下代码按预期工作。

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

但是,无法将列附加到原始 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

任何提示,请。

r tidyverse aggregate
3个回答
4
投票

找到了一个非常简单的解决方案:

wlddev %>% 
  fmutate(Size = fnobs(income, income, TRA = "replace_fill"))  %>% 
  fselect(country, income, Size) %>% 
  funique()

2
投票

与在原始数据中创建列的

add_count
不同,
fnobs
是汇总数据,我们可以将其加入

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

1
投票

所以原则上

fnobs
计算非缺失值的数量,但你可以使用
fcount()
GRPN()

那么我们可以做

wlddev %>% 
  ftransform(Size = GRPN(income)) %>%
  fselect(country, income, Size) %>% 
  funique(cols = 1) # Observations are uniquely identified by country

# or 

wlddev %>% 
  fcount(income, name = "Size", add = TRUE) %>%
  fselect(country, income, Size) %>% 
  funique(cols = 1)

# or 

wlddev %>% 
  fgroup_by(income) %>%
  fmutate(Size = GRPN()) %>%
  fselect(country, income, Size) %>% 
  fungroup() %>%
  funique(cols = 1) 

© www.soinside.com 2019 - 2024. All rights reserved.