我有以下資料:
id animal color shape
1 bear orange circle
2. dog NA triangle
3. NA yellow square
4. cat yellow square
5. NA yellow rectangle
如果我運行此代碼:
df1 <- df %>%
pivot_longer(
-id,
names_to = "Variable",
values_to = "Level"
) %>%
group_by(Variable, Level) %>%
summarise(freq = n()) %>%
mutate(percent = freq/sum(freq)*100) %>%
mutate(Variable = ifelse(duplicated(Variable), NA, Variable)) %>%
ungroup()
我可以得到以下輸出:
Variable Level freq(n=5) percent
animal bear 1 33.3
dog 1 33.3
cat 1 33.3
color orange 1 25.0
yellow 3 75.0
shape circle 1 20.0
triangle 1 20.0
square 2 40.0
rectangle 1 20.0
但是,我還想在每個變數后添加一行,其中包含總計:
Variable Level freq(n=5) percent
animal bear 1 33.3
dog 1 33.3
cat 1 33.3
total 3 100.0
color orange 1 25.0
yellow 3 75.0
total 4 100.0
shape circle 1 20.0
triangle 1 20.0
square 2 40.0
rectangle 1 20.0
total 5 100.0
我嘗試了 mutate 和 summary 的不同變體,但不斷收到錯誤“引數的無效'型別'(閉包)”。
uj5u.com熱心網友回復:
如果我們在定義的時候少走一步df1,
df1 <- df %>%
pivot_longer( -id, names_to = "Variable", values_to = "Level" ) %>%
group_by(Variable, Level) %>%
summarise(freq = n()) %>%
mutate(percent = freq/sum(freq)*100)
df1
# # A tibble: 11 x 4
# # Groups: Variable [3]
# Variable Level freq percent
# <chr> <chr> <int> <dbl>
# 1 animal bear 1 20
# 2 animal cat 1 20
# 3 animal dog 1 20
# 4 animal <NA> 2 40
# 5 color orange 1 20
# 6 color yellow 3 60
# 7 color <NA> 1 20
# 8 shape circle 1 20
# 9 shape rectangle 1 20
# 10 shape square 2 40
# 11 shape triangle 1 20
然后我們可以使用組摘要對其進行擴充(并重新排序):
df1 %>%
group_by(Variable) %>%
summarize(Level = "total", across(freq:percent, sum)) %>%
bind_rows(df1) %>%
arrange(Variable, !is.na(Level), Level == "total", Level) %>%
mutate(Variable = ifelse(duplicated(Variable), NA, Variable))
# # A tibble: 14 x 4
# Variable Level freq percent
# <chr> <chr> <int> <dbl>
# 1 animal <NA> 2 40
# 2 <NA> bear 1 20
# 3 <NA> cat 1 20
# 4 <NA> dog 1 20
# 5 <NA> total 5 100
# 6 color <NA> 1 20
# 7 <NA> orange 1 20
# 8 <NA> yellow 3 60
# 9 <NA> total 5 100
# 10 shape circle 1 20
# 11 <NA> rectangle 1 20
# 12 <NA> square 2 40
# 13 <NA> triangle 1 20
# 14 <NA> total 5 100
uj5u.com熱心網友回復:
library(dplyr)
library(tidyr)
library(purrr)
library(janitor)
df1 %>%
pivot_longer(
-id,
names_to = "Variable",
values_to = "Level"
) %>%
group_by(Variable, Level) %>%
summarise(freq = n()) %>%
mutate(percent = freq/sum(freq)*100) %>%
group_split() %>%
map_dfr(. , ~.x %>%
adorn_totals(name = "total")) %>%
mutate(Variable = ifelse(duplicated(Variable) & Variable != "total", NA, Variable)) %>%
ungroup()
#> Variable Level freq percent
#> animal bear 1 20
#> <NA> cat 1 20
#> <NA> dog 1 20
#> <NA> <NA> 2 40
#> total - 5 100
#> color orange 1 20
#> <NA> yellow 3 60
#> <NA> <NA> 1 20
#> total - 5 100
#> shape circle 1 20
#> <NA> rectangle 1 20
#> <NA> square 2 40
#> <NA> triangle 1 20
#> total - 5 100
資料:
read.table(text = "id animal color shape
1 bear orange circle
2 dog NA triangle
3 NA yellow square
4 cat yellow square
5 NA yellow rectangle", header = T, stringsAsFactors = F) -> df1
uj5u.com熱心網友回復:
這是完成任務的一種方法:
library(dplyr)
library(tidyr)
library(janitor)
df %>%
pivot_longer(
-id,
names_to = "Variable",
values_to = "Level"
) %>%
group_by(Variable, Level) %>%
summarise(freq = n()) %>%
mutate(percent = freq/sum(freq)*100) %>%
ungroup() %>%
group_by(Variable) %>%
group_split() %>%
adorn_totals() %>%
bind_rows() %>%
mutate(Level = ifelse(Level == last(Level), last(Variable), Level)) %>%
mutate(Variable = ifelse(duplicated(Variable) |
Variable == "Total", NA, Variable))
Variable Level freq percent
animal bear 1 20
<NA> cat 1 20
<NA> dog 1 20
<NA> <NA> 2 40
<NA> Total 5 100
color orange 1 20
<NA> yellow 3 60
<NA> <NA> 1 20
<NA> Total 5 100
shape circle 1 20
<NA> rectangle 1 20
<NA> square 2 40
<NA> triangle 1 20
<NA> Total 5 100
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/383102.html
下一篇:改變小標題串列中的所有列
