I am creating a balance test table using rstatix
.
I can produce the outcome I want for each variable, but cannot get to loop over a number of variables to produce a table to my linking in one go.
require(dplyr)
require(rstatix)
data <- data.frame(group=rep(c(1,2),5), v1=rnorm(10),v2=rnorm(10))
data %>%
t_test(v1 ~ group,detailed = TRUE) %>%
adjust_pvalue() %>%
add_significance(cutpoints = c(0, 1e-04, 0.001, 0.01, 0.05, 1),
symbols = c("****", "***", "**", "*", "ns")) %>%
select(c(".y.","estimate1","estimate2","statistic","p.adj","p.adj.signif")) %>%
dplyr::rename(variable = .y.,
'training' = estimate1,
'test' = estimate2,
't-test'=statistic,
p=p.adj,
sl=p.adj.signif)
This fails:
vars <- c("V1", "V2")
bt <- character(0)
for(i in 1:length(vars)){
bt_temp <- data %>%
t_test(vars[i] ~ group, detailed = TRUE) %>%
adjust_pvalue() %>%
add_significance(cutpoints = c(0, 1e-04, 0.001, 0.01, 0.05, 1),
symbols = c("****", "***", "**", "*", "ns")) %>%
select(c(".y.","estimate1","estimate2","statistic","p.adj","p.adj.signif")) %>%
dplyr::rename(variable = .y.,
'training' = estimate1,
'test' = estimate2,
't-test'=statistic,
p=p.adj,
sl=p.adj.signif)
bt <- rbind(bt, bt_temp)
The key is to use
paste0
to piece together a string that you can then convert into a formula. In the solution below, I have converted your for loop into a purer-style map.You had an unhelpful typo in
vars
, mixing capital and lower case "v/V". If you really want to use a for loop, you need to add a missing closing }. Also, createbefore your loop. Inside the loop, assign the result to
bt_temp[[i]]
. Bind the rows once after the loop. If you try to growbt
at each iteration, then your loop will be slow when there are many iterations.