do.call(rbind, results) wont keep rownames

Dear all,
I am running a function "results", which is basically al loop over 10000 genes and it fetches the results of two parameter (coefficient and z value) for each gene.

I then combine all results for all genes with allResults <- do.call(rbind, results).

Now my issue is that I would like to keep the gene name as well (not only the coefficient and zvalue).

I am not quite sure how to fix this.

This is the complete code (currently set to 5 genes):

results <- lapply(1:5, function(i){
  expr<-as.vector(predicted_gene[,i] )
  resi <- multinom(tgroup2 ~ sex + age + Site_Category+ expr, data = pheno)
  print(i)
  coef<-c(summary(resi)$coefficients[1,6],
           summary(resi)$coefficients[1,6]/summary(resi)$standard.errors[1,6])
  names(coef)<-colnames(predicted_gene[,i])
}
)
names(coef)
allResults <- do.call(rbind, results)
View(allResults)

The result is only this:
image

I would like it to have the gene name instead of 1-5 in the first column.

Thank you for any hint!

Could you make a reprex ? its hard to follow.


#suprised that your code whose lappy ends with a names statement 
# doesnt give  you just the names in its output
(results <-lapply(1:4, function(i){
  coeff <- c(1,i)
  names(coeff) <- letters[c(1,i+1)]
  # coeff
}))

do.call(rbind,results)

# has a different problem because the names differ between 
# iteration to iteration
(results <-lapply(1:4, function(i){
  coeff <- c(1,i)
  names(coeff) <- letters[c(1,i+1)]
  coeff
}))
# just has the column names of the first iteration
do.call(rbind,results)

# transpose and dataframe our results
(results <-lapply(1:4, function(i){
  coeff <- c(1,i)
  names(coeff) <- letters[c(1,i+1)]
  as.data.frame(t(coeff))
}))

#instead of rbind 
do.call(dplyr::bind_rows,results)
1 Like

Does the res1 object contain the gene names?

Thank you I figured it out.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.