I didn't do all 20 variables, but I imagine something like this should be sufficient. I didn't know what you meant by "one code", but this can be done in one chunk of an .rmd.
library(tidyverse)
studyid <- 1:1000
numeric <- sample(1:25, 1000, replace=TRUE)
color <- sample(c("red","blue","yellow"), 1000, replace=TRUE)
gender <- sample(c("male", "female"), 1000, replace=TRUE)
age <- sample(c("old","older","oldest"), 1000, replace=TRUE)
df <- data.frame(studyid, numeric, color, gender, age)
df %>%
select(numeric, color) %>%
group_by(color) %>%
summarize(
count = n(),
mean = mean(numeric, na.rm = TRUE),
sd = sd(numeric, na.rm = TRUE)
)
#> # A tibble: 3 × 4
#> color count mean sd
#> <chr> <int> <dbl> <dbl>
#> 1 blue 342 12.8 7.31
#> 2 red 324 12.9 6.86
#> 3 yellow 334 13.4 7.32
df %>%
select(numeric, gender) %>%
group_by(gender) %>%
summarize(
count = n(),
mean = mean(numeric, na.rm = TRUE),
sd = sd(numeric, na.rm = TRUE)
)
#> # A tibble: 2 × 4
#> gender count mean sd
#> <chr> <int> <dbl> <dbl>
#> 1 female 485 13.0 7.25
#> 2 male 515 13.0 7.09
df %>%
select(numeric, age) %>%
group_by(age) %>%
summarize(
count = n(),
mean = mean(numeric, na.rm = TRUE),
sd = sd(numeric, na.rm = TRUE)
)
#> # A tibble: 3 × 4
#> age count mean sd
#> <chr> <int> <dbl> <dbl>
#> 1 old 330 12.0 6.95
#> 2 older 347 12.9 7.12
#> 3 oldest 323 14.2 7.27
Created on 2022-01-05 by the reprex package (v2.0.1)