使用分位数函数后组合数据帧的行向量

时间:2018-03-13 06:21:24

标签: r dataframe quantile

新手问题。我运行了以下命令:

CI_95_outcomes_male< - data.frame(do.call(cbind,lapply(1:ncol(outcome_male_dt),function(r)quantile(outcome_male_dt [,r],c(.95)))))

并以此输出结束:

  

CI_95_outcomes_male               X1 X2 X3 X4   95%9629902039 0 2.968924e + 15 2.968924e + 15

我想将此向量与后续向量组合以最终得到2X4矩阵:

  

mean_outcomes_male

     

ylg_smoking_simS deaths_averted total_cig total_tax_            9.62990 0.0000 2.78248 2.782480

我试过了:

  

CI_95_outcomes_male< -colnames(mean_outcomes_male)   data.frame(mean_outcomes_male,CI_95_outcomes_male)   data.frame中的错误(mean_outcomes_male,CI_95_outcomes_male):     参数意味着不同的行数:4,0

感谢任何指导,谢谢!

1 个答案:

答案 0 :(得分:0)

  

CI_95_outcomes_male< -colnames(mean_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