0
votes

Novice problem. I ran following command:

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

and end up with this output:

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

I would like to combine this vector with following vector to end up with 2X4 matrix:

#

mean_outcomes_male

ylg_smoking_simS deaths_averted total_cig total_tax_ 9.62990 0.0000 2.78248 2.782480

I tried:

CI_95_outcomes_male<-colnames(mean_outcomes_male) data.frame(mean_outcomes_male,CI_95_outcomes_male) Error in data.frame(mean_outcomes_male, CI_95_outcomes_male) : arguments imply differing number of rows: 4, 0

Any guidance appreciated, thanks!

1

1 Answers

0
votes

CI_95_outcomes_male<-colnames(mean_outcomes_male)

I think you forgot to put colnames around CI_95_outcomes_male. But there's another problem here. I'm assuming that mean_outcomes_male is a vector, in which case colnames(mean_outcomes_male) is NULL.

data.frame(mean_outcomes_male,CI_95_outcomes_male)

Even if CI_95_outcomes_male was correct, the above command will result in a 4x5 data frame, with the first column being the mean_outcomes_male vector, second column being the CI_95_outcomes_male value for your first variable (repeated for each row),...,and the fifth column being the CI_95_outcomes_male value for your fourth variable (repeated for each row).

You need to do something like this:

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