Home > Enterprise >  how to change column display order using table() in r
how to change column display order using table() in r

Time:07-28

I am using the table() function in r to create a contingency table. The table displays columns for 'No' and 'Yes' by default, however, I would like the columns to display results for 'Yes' and 'No'. Is there a way to achieve this? Thank you

My code:

data$skull_fractures_i<-ifelse(! (data$`Skull Fractures` %in% c("Not idenfitied",'Not identified','Heart only autopsy','Had craniotomy at hospital')), 'Yes', 'No')  
data$age_i<-ifelse(data$`Age Deceased`<=65, '<=65 years old', '>65 years old')
table(data$age_i, data$skull_fractures_i)

I have:

               No  Yes
<=65 years old 24   45
>65 years old  40   50

I want:

               Yes  No
<=65 years old 45   24
>65 years old  50   40

CodePudding user response:

Fix your factor levels before table:

data$skull_fractures_i <- factor(data$skull_fractures_i, levels = c("Yes", "No"))
  •  Tags:  
  • r
  • Related