Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 views
in Technique[技术] by (71.8m points)

r - tabulate one variable in a data.frame against all others using janitor::tabyl

Data:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Data

x <- read.table(text = "product     q1  q2  q3
1   5   9   NA
1   6   7   NA
2   4   4   9
2   9   6   8
2   8   4   NA", header = TRUE)

To make tabyls showing your product variable against all other columns, you can first make the data "long", then use the functions you originally had:

x %>%
  pivot_longer(q1:q3, "q", "value") %>%
  mutate(value = factor(value, levels = 1:9)) %>% # this is for your 1-9 scale
  tabyl(value, product, q, show_na = FALSE) %>%
  adorn_percentages("col") %>%
  adorn_pct_formatting(digits = 0) %>%
  adorn_title()

Result

$q1
       product    
 value       1   2
     1      0%  0%
     2      0%  0%
     3      0%  0%
     4      0% 33%
     5     50%  0%
     6     50%  0%
     7      0%  0%
     8      0% 33%
     9      0% 33%

$q2
       product    
 value       1   2
     1      0%  0%
     2      0%  0%
     3      0%  0%
     4      0% 67%
     5      0%  0%
     6      0% 33%
     7     50%  0%
     8      0%  0%
     9     50%  0%

$q3
       product    
 value       1   2
     1       -  0%
     2       -  0%
     3       -  0%
     4       -  0%
     5       -  0%
     6       -  0%
     7       -  0%
     8       - 50%
     9       - 50%

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...