Home > front end >  Adding categorical outputs together in prop.table()
Adding categorical outputs together in prop.table()

Time:12-07

Noob - sorry. I have bee applying prop.table() to data and I have a very basic question. I want to automatically output the combined occasional AND Regular smokers (0.04 0.34 = 0.38) from a data frame of categorical data, how would I do it?

I really don't mind step by step processes that allow me to understand what is going on here. I can take that and apply it elsewhere then - Thank you

edit: The only process I can think of is to try to remove not smokers and count the remain categorical variables and use that count to get a proportion through basic maths. But I think I'm way off with that approach

Edit - adding in prop.table code

OccSmoker <- table(Student1995$smoke)
prop.table(OccSmoker)
$smoke
       Not Occasional    Regular 
      0.62       0.04       0.34 

The dataframe is here

> dput(head(Student1995,5))
structure(list(alcohol = structure(c(3L, 2L, 2L, 2L, 3L), .Label = c("Not", 
"Once or Twice a week", "Once a month", "Once a week", "More than once a week"
), class = "factor"), drugs = structure(c(1L, 2L, 1L, 1L, 1L), .Label = c("Not", 
"Tried once", "Occasional", "Regular"), class = "factor"), smoke = structure(c(2L, 
3L, 1L, 1L, 1L), .Label = c("Not", "Occasional", "Regular"), class = "factor"), 
    sport = structure(c(2L, 1L, 1L, 2L, 2L), .Label = c("Not regular", 
    "Regular"), class = "factor")), row.names = c(NA, 5L), class = "data.frame")

The Summary data if it helps - edit

> summary(Student1995)
                  alcohol          drugs           smoke            sport   
 Not                  : 5   Not       :36   Not       :38   Not regular:13  
 Once or Twice a week :16   Tried once: 6   Occasional: 5   Regular    :37  
 Once a month         :12   Occasional: 7   Regular   : 7                   
 Once a week          :14   Regular   : 1                                   
 More than once a week: 3 

CodePudding user response:

You could define a new variable smoke_class like so and then plug that into prop.table()

Student1995$smoke_class <- ifelse(
    Student1995$smoke %in% c("Occasional", "Regular"), # condition
    "Occasional   Regular", # value if condition == TRUE
    Student1995$smoke) # value if condition == FALSE


OccSmoker <- table(Student1995$smoke_class)
prop.table(OccSmoker)

# Not Occasional   Regular 
# 0.6                  0.4 
  • Related