I am trying to get crosstabs of multiple columns using tidyverse code.
Sample data:
df <- data.frame(col1=c("a", "b", "c", "c"), col2=c(NA, "d", "d","e")) %>%
mutate_all(as.character)
df
col1 col2
1 a <NA>
2 b d
3 c d
4 c e
Using apply, I would do the following:
apply(df, 2, function(x) data.frame(table(x)))
I have tried the code below which does not work:
df %>%
map_df(function(.x) {
group_by(.x) %>% summarise(n=n()) %>% print()
})
It can be done using purrr::map
like below:
library(purrr)
map(df, ~as.data.frame(table(.x)))
#> $col1
#> .x Freq
#> 1 a 1
#> 2 b 1
#> 3 c 2
#>
#> $col2
#> .x Freq
#> 1 d 2
#> 2 e 1
lapply
选项
lapply(df, function(x) as.data.frame(table(x)))
In tidyverse
you can do this in couple of ways.
purrr::map
library(dplyr)
purrr::map(names(df), ~df %>% count(.data[[.x]]))
df %>%
tidyr::pivot_longer(cols = everything()) %>%
count(name, value)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.