Taren Shaw
Taren Shaw

Reputation: 109

Using count/mutate combo with purrr:map

Sample data:

       ï..Employee_Name       PositionID              Position State  Zip
  1:   Adinolfi, Wilson  K         19  Production Technician I    MA 1960
  2: Ait Sidi, Karthikeyan         27                  Sr. DBA    MA 2148
  3:     Akinkuolie, Sarah         20 Production Technician II    MA 1810
  4:          Alagbe,Trina         19  Production Technician I    MA 1886
  5:       Anderson, Carol         19  Production Technician I    MA 2169
 ---                                                                     
307:        Woodson, Jason         20 Production Technician II    MA 1810
308:     Ybarra, Catherine         19  Production Technician I    MA 2458
309:      Zamora, Jennifer          6                      CIO    MA 2067
310:           Zhou, Julia          9             Data Analyst    MA 2148
311:         Zima, Colleen         19  Production Technician I    MA 1730

I wrote my own fucntion that counts instances of observations for a variable in my dataframe and then converts them into factors:

HRdata_factor_count <- function(df, var) {
        df %>% 
        count(.data[[var]], sort = T) %>% 
        mutate(!!var := fct_reorder(factor(.data[[var]]), n))
    }

And I would like to use it with the map function in the Purrr package but I get the following error:

> map(HRdata, ~HRdata_factor_count(.x))
 Error in UseMethod("count") : 
  no applicable method for 'count' applied to an object of class "character"

How would I fix this issue and have map return a list with the counts of instances for each variable within my df?

I tried this but got a weird output

    HRnames <- names(HRdata)
map2(HRdata, HRnames, ~HRdata_factor_count, df = .x, var =.y)

$Position
function(df, var) {
  df %>% 
    count(.data[[var]], sort = T) %>% 
    mutate(!!var := fct_reorder(factor(.data[[var]]), n))
}
<bytecode: 0x000001e2ce372f60>

$State
function(df, var) {
  df %>% 
    count(.data[[var]], sort = T) %>% 
    mutate(!!var := fct_reorder(factor(.data[[var]]), n))

Upvotes: 0

Views: 349

Answers (1)

Roman
Roman

Reputation: 17648

According your provided data, you can try purrr's imap

imap(mtcars, ~count(tibble(.x), !!.y := factor(.x)))

As function

foo <- function(x, y) count(tibble(x), !!y := factor(x))
imap(mtcars, foo)
  • .x = each column as vector. check map(mtcars, ~.) or imap(mtcars, ~.x)
  • .y = corresponding column name: names(mtcars)

Since count needs a data.frame or a tibble as input, one need to transform the input vectors again using tibble(x). Factors are specified within count.

Edit:

Add the factor reorder like this:

foo <- function(x, y){ count(tibble(x), tmp = factor(x)) %>% 
                       mutate(!!y := fct_reorder(tmp, n, .fun = sum)) %>% 
                       select(-tmp)}

Upvotes: 2

Related Questions