输出仅显示列表中的元素数
Output only show number of element in a list
我有时间、状态和性别的向量,想做一个生存分析:
time <- c(306,455,1010,210,883,1022,310,361,218,166)
status <- c(0,1,0,1,0,0,1,0,1,1)
gender <- c("Male","Male","Female","Female","Male","Female","Female","Female","Female","Female")
A <- survfit(Surv(time, status)~gender)
然后我拆分table:
library(broom)
library(dplyr)
B<-tidy(A, censored = TRUE) %>%
split(.$strata)
在那table我想要特定的列所以我做了一个循环:
Time<-list()
Estimate<-list()
StdError<-list()
for (i in 1:length(B)){
Time[i] <- B[[i]][1]
Estimate[i] <- B[[i]][5]
StdError[i] <- B[[i]][6]
}
当我尝试使用 cbind
提取和合并列时,我得到了这个结果:
result <- cbind(Time, Estimate, StdError)
>result
Time Estimate StdError
[1,] Numeric,7 Numeric,7 Numeric,7
[2,] Numeric,3 Numeric,3 Numeric,3
有人可以向我解释为什么会发生这种情况并帮助我修复我的代码以便输出如下所示:
>result
Time Estimate StdError
[1,] 166 0.8571429 0.1543033
166 0.8571429 0.1543033
210 0.7142857 0.2390457
218 0.5714286 0.3273268
310 0.4285714 0.4364358
361 0.4285714 0.4364358
1010 0.4285714 0.4364358
1022 0.4285714 0.4364358
[2,] Time Estimate StdError
306 1.0 0.0000000
455 0.5 0.7071068
883 0.5 0.7071068
我们可以使用lapply
lapply(B, function(x) x[c('time', 'estimate', 'std.error')])
或不带匿名函数
lapply(B, `[`, c('time', 'estimate', 'std.error'))
#$`gender=Female`
# time estimate std.error
#1 166 0.8571429 0.1543033
#2 210 0.7142857 0.2390457
#3 218 0.5714286 0.3273268
#4 310 0.4285714 0.4364358
#5 361 0.4285714 0.4364358
#6 1010 0.4285714 0.4364358
#7 1022 0.4285714 0.4364358
#$`gender=Male`
# time estimate std.error
#8 306 1.0 0.0000000
#9 455 0.5 0.7071068
#10 883 0.5 0.7071068
'Time'、'Estimate' 和 'StdError' 对象是 list
,我们可以 cbind
通过遍历 list
的元素Map
Map(cbind, Time = Time, Estimate = Estimate, StdError = StdError)
如果我们想使用 for
循环,另一种选择是不创建单独的向量,而是直接对数据集进行子集化
out <- vector("list", length(B))
for(i in seq_along(B)) out[[i]] <- B[[i]][c(1, 5, 6)]
我有时间、状态和性别的向量,想做一个生存分析:
time <- c(306,455,1010,210,883,1022,310,361,218,166)
status <- c(0,1,0,1,0,0,1,0,1,1)
gender <- c("Male","Male","Female","Female","Male","Female","Female","Female","Female","Female")
A <- survfit(Surv(time, status)~gender)
然后我拆分table:
library(broom)
library(dplyr)
B<-tidy(A, censored = TRUE) %>%
split(.$strata)
在那table我想要特定的列所以我做了一个循环:
Time<-list()
Estimate<-list()
StdError<-list()
for (i in 1:length(B)){
Time[i] <- B[[i]][1]
Estimate[i] <- B[[i]][5]
StdError[i] <- B[[i]][6]
}
当我尝试使用 cbind
提取和合并列时,我得到了这个结果:
result <- cbind(Time, Estimate, StdError)
>result
Time Estimate StdError
[1,] Numeric,7 Numeric,7 Numeric,7
[2,] Numeric,3 Numeric,3 Numeric,3
有人可以向我解释为什么会发生这种情况并帮助我修复我的代码以便输出如下所示:
>result
Time Estimate StdError
[1,] 166 0.8571429 0.1543033
166 0.8571429 0.1543033
210 0.7142857 0.2390457
218 0.5714286 0.3273268
310 0.4285714 0.4364358
361 0.4285714 0.4364358
1010 0.4285714 0.4364358
1022 0.4285714 0.4364358
[2,] Time Estimate StdError
306 1.0 0.0000000
455 0.5 0.7071068
883 0.5 0.7071068
我们可以使用lapply
lapply(B, function(x) x[c('time', 'estimate', 'std.error')])
或不带匿名函数
lapply(B, `[`, c('time', 'estimate', 'std.error'))
#$`gender=Female`
# time estimate std.error
#1 166 0.8571429 0.1543033
#2 210 0.7142857 0.2390457
#3 218 0.5714286 0.3273268
#4 310 0.4285714 0.4364358
#5 361 0.4285714 0.4364358
#6 1010 0.4285714 0.4364358
#7 1022 0.4285714 0.4364358
#$`gender=Male`
# time estimate std.error
#8 306 1.0 0.0000000
#9 455 0.5 0.7071068
#10 883 0.5 0.7071068
'Time'、'Estimate' 和 'StdError' 对象是 list
,我们可以 cbind
通过遍历 list
的元素Map
Map(cbind, Time = Time, Estimate = Estimate, StdError = StdError)
如果我们想使用 for
循环,另一种选择是不创建单独的向量,而是直接对数据集进行子集化
out <- vector("list", length(B))
for(i in seq_along(B)) out[[i]] <- B[[i]][c(1, 5, 6)]