crosstab - How can I count the frequency of an answer in R? -
i have db this:
id group drink 1 yes 2 no 3 na 4 b no 5 b no 6 b yes
and measure how many people of group drinks , how many people in group b drinks.
i using length()
, function returns 3
(na
being considered = yes). how can fix it?
table()
1 option:
db <- read.table(text = "id group drink 1 yes 2 no 3 na 4 b no 5 b no 6 b yes", header = true) with(db, table(drink)) with(db, table(group, drink)) > with(db, table(drink)) drink no yes 3 2 > with(db, table(group, drink)) drink group no yes 1 1 b 2 1
including na
class achieved usena
argument:
with(db, table(drink, usena = "ifany")) > with(db, table(drink, usena = "ifany")) drink no yes <na> 3 2 1
you can of course store objects returned table()
, access them other matrix/array:
tab <- with(db, table(drink, usena = "ifany")) tab[1] tab2 <- with(db, table(group, drink, usena = "ifany")) tab2[,1] tab2[1,] > tab <- with(db, table(drink, usena = "ifany")) > tab[1] no 3 > tab <- with(db, table(drink, usena = "ifany")) > tab[1] no 3 > tab2 <- with(db, table(group, drink, usena = "ifany")) > tab2[,1] b 1 2 > tab2[1,] no yes <na> 1 1 1
Comments
Post a Comment