2

frequency table of numbers

This is from Wikipedia.

I want to make similar barplot with data below.

audit<-structure(list(ID = c(1004641L, 1010229L, 1024587L, 1038288L, 
1044221L, 1047095L, 1047698L, 1053888L, 1061323L, 1062363L), 
Age = c(38L, 35L, 32L, 45L, 60L, 74L, 43L, 35L, 25L, 22L), 
Employment = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L), .Label = c("Consultant", "Private", "PSFederal", 
"PSLocal", "PSState", "SelfEmp", "Unemployed", "Volunteer"
), class = "factor"), Education = structure(c(3L, 1L, 5L, 
2L, 3L, 5L, 2L, 12L, 1L, 5L), .Label = c("Associate", "Bachelor", 
"College", "Doctorate", "HSgrad", "Master", "Preschool", 
"Professional", "Vocational", "Yr10", "Yr11", "Yr12", "Yr1t4", 
"Yr5t6", "Yr7t8", "Yr9"), class = "factor"), Marital = structure(c(5L, 
1L, 2L, 3L, 3L, 3L, 3L, 3L, 2L, 1L), .Label = c("Absent", 
"Divorced", "Married", "Married-spouse-absent", "Unmarried", 
"Widowed"), class = "factor"), Occupation = structure(c(12L, 
14L, 2L, 10L, 3L, 12L, 3L, 6L, 2L, 11L), .Label = c("Cleaner", 
"Clerical", "Executive", "Farming", "Home", "Machinist", 
"Military", "Professional", "Protective", "Repair", "Sales", 
"Service", "Support", "Transport"), class = "factor"), Income = c(8, 
7, 1, 2, 7, 3, 4, 5, 1, 5), Gender = structure(c(1L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L), .Label = c("Female", "Male"
), class = "factor"), Deductions = c(0, 0, 0, 0, 0, 0, 0, 
0, 0, 0), Hours = c(72L, 30L, 40L, 55L, 40L, 30L, 50L, 40L, 
40L, 37L), IGNORE_Accounts = structure(c(31L, 18L, 31L, 31L, 
31L, 31L, 31L, 31L, 31L, 31L), .Label = c("Canada", "China", 
"Columbia", "Cuba", "Ecuador", "England", "Fiji", "Germany", 
"Greece", "Guatemala", "Hong", "Hungary", "India", "Indonesia", 
"Iran", "Ireland", "Italy", "Jamaica", "Japan", "Malaysia", 
"Mexico", "NewZealand", "Nicaragua", "Philippines", "Poland", 
"Portugal", "Scotland", "Singapore", "Taiwan", "Thailand", 
"UnitedStates", "Vietnam", "Yugoslavia"), class = "factor"), 
RISK_Adjustment = c(0L, 0L, 0L, 7298L, 15024L, 0L, 22418L, 
0L, 0L, 0L), TARGET_Adjusted = c(0L, 0L, 0L, 1L, 1L, 0L, 
1L, 0L, 0L, 0L)), .Names = c("ID", "Age", "Employment", "Education", 
"Marital", "Occupation", "Income", "Gender", "Deductions", "Hours", 
"IGNORE_Accounts", "RISK_Adjustment", "TARGET_Adjusted"), row.names = c(NA, 
10L), class = "data.frame")

What I am trying to do is

1) make a barplot just like above: the frequency of audit$Income.

2) make two barplot devided by audit$TARGET_Adjusted.

I tried for the first one like below. And it didn't work.

fre <- tapply(audit$income, audit$TARGET_Adjusted, table)
barplot(fre)

Can you please let me know how to?

Doo Hyun Shin
  • 297
  • 3
  • 15
  • Try doing the `barplot` on the tabled data directly - ie on `table(audit$Income, audit$TARGET_Adjusted)`. You my want to look at the `beside` argument. – user20650 Oct 06 '14 at 17:44
  • @Doo Hyun Shin: can you clarify if you want two separate barplots (one for 0 and other for 1) or a single one with two bars (representing 0 and 1)? – Ujjwal Oct 06 '14 at 18:26
  • @ujjwal sorry for late reply. I want separate barplot – Doo Hyun Shin Oct 07 '14 at 00:13
  • @user20650 I tried but I got this message :"all arguments must have the same length." – Doo Hyun Shin Oct 07 '14 at 00:22
  • 1
    What did you try...did you try `barplot(table(audit$Income), beside=TRUE)` and also `barplot(table(audit$Income, audit$TARGET_Adjusted), beside=TRUE)` ?. [your error *may* suggest a typo in one of the variables in your code] – user20650 Oct 07 '14 at 00:36
  • fre<- table(audit$income, audit$TARGET_Adjusted) This is what I tried. And because of your comment now, my problem is solved! Thank you very much! – Doo Hyun Shin Oct 07 '14 at 00:40
  • Great stuff, you're welcome. [i think your error message described in comment above comes from using `income` instead of `Income`] – user20650 Oct 07 '14 at 00:41

0 Answers0