我是R和生存分析的新手,我有兴趣将幸存的结果输出到数据框中.
这个网站提供了一个很好的解决方案,但不是一个有分层的(https://stat.ethz.ch/pipermail/r-help/2014-October/422348.html).如何使用包含分层类型的额外列附加(或堆叠)每个层.提供的链接中的解决方案不适用于分层分组
library(survival)
data(lung)
mod <- with(lung, survfit(Surv(time, status)~ 1))
res <- summary(mod)
str(res)
# Extract the columns you want
cols <- lapply(c(2:6, 8:10) , function(x) res[x])
# Combine the columns into a data frame
tbl <- do.call(data.frame, cols)
str(tbl)
Run Code Online (Sandbox Code Playgroud)
谢谢先进,R新手
感谢您的帮助.需要将填充了分隔值的列拆分为以其分隔值命名的列,并且每个新列都要填充1或0,其中找到值或不找到值.
state <-
c('ACT',
'ACT|NSW|NT|QLD|SA|VIC',
'ACT|NSW|NT|QLD|TAS|VIC|WA',
'ACT|NSW|NT|SA|TAS|VIC',
'ACT|NSW|QLD|VIC',
'ACT|NSW|SA',
'ACT|NSW|NT|QLD|TAS|VIC|WA|SA',
'NSW',
'NT',
'NT|SA',
'QLD',
'SA',
'TAS',
'VIC',
'WA')
df <- data.frame(id = 1:length(state),state)
id state
1 1 ACT
2 2 ACT|NSW|NT|QLD|SA|VIC
3 3 ACT|NSW|NT|QLD|TAS|VIC|WA
4 4 ACT|NSW|NT|SA|TAS|VIC
...
Run Code Online (Sandbox Code Playgroud)
期望状态是具有相同维度的数据帧以及基于根据行填充1或0的状态的附加列.
詹姆斯