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?