Reputation: 21
R Newbie has a simple data table (DT) that has the number of households (NumHH) in several United States (Residences):
NumHH Residence
6 AK
4 AL
7 AR
6 AZ
1 CA
2 CO
2 CT
1 AK
4 AL
6 AR
3 AZ
1 CA
6 CO
3 CT
5 AL
By using with(),
with(DT, table(NumHH, Residence))
I can get a table that's close to what I want:
Residence
NumHH AK AL AR AZ CA CO CT
1 1 0 0 0 2 0 0
2 0 0 0 0 0 1 1
3 0 0 0 1 0 0 1
4 0 2 0 0 0 0 0
5 0 1 0 0 0 0 0
6 1 0 1 1 0 1 0
7 0 0 1 0 0 0 0
but I need a table that provides the frequency of several ranges per residence. The frequencies are calculated this way:
##Frequency of ranges per State
One <- DT$NumHH <=1 ##Only 1 person/household
Two_Four <- ((DT$NumHH <=4) - (DT$NumHH <=1)) ##2 to 4 people in Household
OverFour <- DT$NumHH >4 ##More than 4 people in HH
Ideally, the result would look like this:
Residence
NumHH AK AL AR AZ CA CO CT
One 1 0 0 0 2 0 0
Two_Four 0 2 0 1 0 1 2
OverFour 1 1 2 1 0 1 0
I've tried:
with()
- I am only able to do one range at a time with "with()", such as:
with(DT, table (One, Residence))
- and that gives me a FALSE row and a TRUE row by state.
data.frames
asks me to name each state ("AK", "AL", "AR", etc.), but with()
already knows.
I have also tried ddply
, but got a list of each calculation's (150 unlabeled rows in 4 columns - not the desired 3 labeled rows in 50 columns for each state), so I'm obviously not doing it right.
Any assistance is greatly appreciated.
Upvotes: 2
Views: 87
Reputation: 93803
Use ?cut
to establish your groups before using table
:
with(dat, table( NumHH=cut(NumHH, c(0,1,4,Inf), labels=c("1","2-4",">4")), Residence))
# Residence
#NumHH AK AL AR AZ CA CO CT
# 1 1 0 0 0 2 0 0
# 2-4 0 2 0 1 0 1 2
# >4 1 1 2 1 0 1 0
Upvotes: 3