Kkt2
Kkt2

Reputation: 67

Making a frequency table from two columns based on a certain condition

I have a table with two columns titled "Age" and their preferred method of "Choice" (its just a dummy data, I have attached the picture on how it looks like). I want to make a frequency table in R out of it based on that should look something like

Choice       Age >= 40   Age < 40
Bio-metric   4          4
Manual       4          3

Any help is dearly appreciated. Thanks :)

Upvotes: 1

Views: 83

Answers (1)

akrun
akrun

Reputation: 886938

An option is table. Create a logical vector on the 'Age', recode it to numeric (TRUE/FALSE => 1/0, +1 => 2/1, pass a new vector c("Age < 40", "Age >=40") to make use of the index 1, 2 to change the values) and then apply the table along with 'Choice' column

with(df1, table(Choice, c('Age < 40', 'Age >=40')[1 +(Age >= 40)]))

Upvotes: 2

Related Questions