CI_95_outcomes_male
我想你忘了把colnames 放在CI_95_outcomes_male 周围。但这里还有另一个问题。我假设mean_outcomes_male 是一个向量,在这种情况下colnames(mean_outcomes_male) 是NULL。
data.frame(mean_outcomes_male,CI_95_outcomes_male)
即使CI_95_outcomes_male 正确,上述命令也会生成一个 4x5 数据框,第一列是 mean_outcomes_male 向量,第二列是第一个变量的 CI_95_outcomes_male 值(对每一行重复),...,第五列是您的第四个变量的 CI_95_outcomes_male 值(对每一行重复)。
你需要做这样的事情:
set.seed(42)
# Generate a random dataset for outcomes_male_dt with 4 variables and n rows
n <- 100
outcomes_male_dt <- data.frame(x1=runif(n),x2=runif(n),x3=runif(n),x4=runif(n))
# I'm assuming you want the 95th percentile of each variable in outcomes_male_dt and store them in CI_95_outcomes_male
ptl <- .95 # if you want to add other percentiles you can replace this with something like "ptl <- c(.10,.50,.90,.95)"
CI_95_outcomes_male <- apply(outcomes_male_dt,2,quantile,probs=ptl)
# I'm going to assume that mean_outcomes_male is a vector of means for all the variables in outcomes_male_dt
mean_outcomes_male <- colMeans(outcomes_male_dt)
# You want to end up with a 2x4 matrix - I'm assuming you meant row 1 will be the means, and row 2 will be the 95th percentiles, and the columns will be the variables
want <- rbind(mean_outcomes_male, CI_95_outcomes_male)
colnames(want) <- colnames(outcomes_male_dt)
row.names(want) <- c('Mean',paste0("p",ptl*100)) # paste0("p",ptl*100) is equivalent to paste("p",ptl*100,sep="")
want # Resulting matrix