0

I have count data on utility disconnections in a set of about 100 zip codes. I also have data on the total number of utility customers in those zip codes. I merged the two together based on the year/month and zip code using:

new<- merge(disconnect_total,totalres, by=c("YR_MO","ZIP"))

Then I checked to see the column names and got the following:

head(new)

   YR_MO   ZIP TOTAL.x Non.CARE.x CARE.x FERA.x MBL.x  TOTAL.y Non.CARE.y
1 201001 91901      15         12      3      0     0   6,516      5,675 
2 201001 91902      11          9      2      0     0   6,275      5,616 
3 201001 91905       1          1      0      0     0     769        595 
4 201001 91906       3          2      1      0     0   1,298        929 
5 201001 91910      42         22     20      0     0  25,645     17,180 
6 201001 91911      51         21     30      0     0  23,843     13,518 
    CARE.y FERA.y MBL.y
1     825      16   146
2     642      17   110
3     174       0    23
4     361       8    37
5   8,268     197   400
6  10,032     293   429

In order to create a new variable for the percent of disconnections in each zip code in each year/month, I need to divide TOTAL.y (the total number of customers) by TOTAL.x (the total number of disconnections). I tried to do this and failed using:

percentdisconnect <- (new$TOTAL.x/new$TOTAL.y)

Can anyone help me?

GordonShumway
  • 1,980
  • 13
  • 19
ATinnin
  • 1
  • 1
  • How did `(new$TOTAL.x/new$TOTAL.y)` fail? It looks fine. Was there an error message? What did it say? Or does the result seem wrong? What seems wrong about it? – Gregor Thomas Jan 25 '22 at 21:42
  • 1
    Mmm, I see commas in your `TOTAL.y` column, which means it's string, not numeric. From this [FAQ](https://stackoverflow.com/a/1523177/903061), you should try converting with `new$TOTAL.y <- as.numeric(gsub(",", "", new$TOTAL.y))` first. – Gregor Thomas Jan 25 '22 at 21:44
  • Thank you Gregor! That was exactly my problem! Your code did the trick - thank you very much for your help! – ATinnin Jan 25 '22 at 21:58

0 Answers0