简体   繁体   English

R中具有多个CALCULATED变量的频率表

[英]frequency table with several CALCULATED variables in R

R Newbie has a simple data table (DT) that has the number of households (NumHH) in several United States (Residences): R Newbie有一个简单的数据表(DT),其中包含几个美国的户数(NumHH)(住所):

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(),

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: 我试过了:

  1. 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. with() -我只能一次使用“ with()”来做一个范围,例如: with(DT, table (One, Residence)) -并且按状态给出了FALSE行和TRUE行。

  2. data.frames asks me to name each state ("AK", "AL", "AR", etc.), but with() already knows. data.frames要求我命名每个状态(“ AK”,“ AL”,“ AR”等),但是with()已经知道。

  3. 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. 我也尝试了ddply ,但是得到了每个计算的列表(4列中有150个未标记的行-每种状态都不需要50列中的3个标记的行),所以我显然做得不好。

Any assistance is greatly appreciated. 非常感谢您的协助。

Use ?cut to establish your groups before using table : 在使用table之前,使用?cut来建立您的组:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM