0

I am trying to create a descriptive statistics table using the table1 with p-values and data from an SAV file. I read in the file using read_sav from the haven package.

library(haven)
library(table1)
library(tidyverse)

df<- read_sav(filename)

outcome_var = 'treatment'
test_df <- tibble(treatment = c(1,0,0,0,1,0), x = 1:6, y = rnorm(6))

which reads in the data as a tibble. To create the table1, the treatment variable must be a factor type. Normally, I'd change the column using the link above like

library(MatchIt)
data(lalonde)

lalonde$treat    <- factor(lalonde$treat, levels=c(0, 1, 2), labels=c("Control", "Treatment", "P-value"))

However, when I do

factor(test_df[,outcome_var], levels=c(0, 1, 2), labels=c("Not Treated", "Treated", "P-value")

the treatment column is returned as NULL. If I use the as_factor function from haven, I can't pass levels or labels.

I expect an output like the table shown in the table1 link above.

How can I change the levels and labels using as_factor to include the p-values column? Or is there a way to use factor without it returning NULL to the column?

m13op22
  • 2,168
  • 2
  • 16
  • 35

1 Answers1

0

Convert the tibble data frame to a normal data frame, then you can use factor and your p-value script to produce your Table 1.

test_df <- as.data.frame(test_df)

test_df$treatment <- factor(test_df$treatment, levels=c(0, 1, 2), labels=c("Not Treated", "Treated", "P-value"))

table1(~ x + y | treatment, data = test_df, render = rndr)

Note that I used test_df$treatment instead of test_df[, outcome]. Each method returns a different structure and factor uses the one that test_df$treatment returns.

m13op22
  • 2,168
  • 2
  • 16
  • 35