How to transpose this matrix (or dataframe) ?

Hi, I would like to kindly ask how to transpose this dataframe:
from this:
obraz
to this:
obraz

tab <- matrix(c(10,3,3,1), nrow=2,ncol=2)
colnames(tab) <- c("No_Therapy","Therapy")
row.names(tab) <- c("No_Prozac","Prozac")

I tried t() but it did not work here.

t(tab) did as it was asked, which was to pivot the object

tab <- matrix(c(10,3,3,1), nrow=2,ncol=2)
colnames(tab) <- c("No_Therapy","Therapy")
row.names(tab) <- c("No_Prozac","Prozac")
tab
#>           No_Therapy Therapy
#> No_Prozac         10       3
#> Prozac             3       1
t(tab)
#>            No_Prozac Prozac
#> No_Therapy        10      3
#> Therapy            3      1

which was, however, not what you wanted, which is to swap the positions of the columns and rows.

tab <- matrix(c(10,3,3,1), nrow=2,ncol=2)
colnames(tab) <- c("No_Therapy","Therapy")
row.names(tab) <- c("No_Prozac","Prozac")
tab[c("Prozac","No_Prozac"),c("Therapy","No_Therapy")]
#>           Therapy No_Therapy
#> Prozac          1          3
#> No_Prozac       3         10

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.