简体   繁体   English

使用“as_factor”更改级别和标签

[英]Change levels and labels using the `as_factor` R haven function for table1 output

I am trying to create a descriptive statistics table using the table1 with p-values and data from an SAV file.我正在尝试使用table1和 SAV 文件中的 p 值和数据创建一个描述性统计表。 I read in the file using read_sav from the haven package.我使用来自haven package 的read_sav读取文件。

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 .它将数据作为tibble读取。 To create the table1, the treatment variable must be a factor type.要创建 table1, treatment变量必须是因子类型。 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. treatment列返回为 NULL。 If I use the as_factor function from haven , I can't pass levels or labels.如果我使用 Haven 的as_factor haven ,我无法通过关卡或标签。

I expect an output like the table shown in the table1 link above.我希望 output 像上面 table1 链接中显示的表格一样。

How can I change the levels and labels using as_factor to include the p-values column?如何使用as_factor更改级别和标签以包含 p 值列? Or is there a way to use factor without it returning NULL to the column?或者有没有办法使用factor而不将 NULL 返回到列?

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.tibble数据框转换为普通数据框,然后您可以使用factor和 p 值脚本来生成表 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] .请注意,我使用test_df$treatment而不是test_df[, outcome] Each method returns a different structure and factor uses the one that test_df$treatment returns.每种方法都返回不同的结构,而factor使用test_df$treatment返回的结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM