how to use the 'group' argument in function rowsum

Zi Xin Lee picture Zi Xin Lee · May 21, 2015 · Viewed 9.5k times · Source
               M.5    M5.9  M10.14 M14.17     F.5    F5.9  F10.14 F14.17
Alabama     154902  158765  163731  97673  146906  154067  157592  91339
Alaska       27593   27033   26425  15899   26341   25172   24487  14315
Arizona     223705  236024  230633 138579  214067  218351  227456 130770
Arkansas     99007  105750  103149  59618   92649   99763   94845  56848
California 1296287 1296812 1306123 825862 1240523 1239678 1245997 788024
Colorado    172562  187549  168057 100405  164433  172087  170502  95712

I am compiling data from the American Community Survey using the package acs. This is the head of the data frame I have about number of males and females in each state from ages under 5, 5-9,10-14 etc. The dataframe name is N0018_12_4. I want to find out the total population of kids under 18 in each state. When I try rowsum like this, I get an error message saying "argument "group" is missing, with no default". I tried reading the help page on R and searching for examples of how to use the argument 'group' on cran/ elsewhere but I still don't get what I'm supposed to do with that argument.

Any help will be greatly appreciated! :)

Answer

chappers picture chappers · May 23, 2015

I think the function you are looking for is ?rowSums.

Here I've assumed your data looks like this, which I've called "country.csv":

M.5,M5.9,M10.14,M14.17,F.5,F5.9,F10.14,F14.17
Alabama,154902,158765,163731,97673,146906,154067,157592,91339
Alaska,27593,27033,26425,15899,26341,25172,24487,14315
Arizona,223705,236024,230633,138579,214067,218351,227456,130770
Arkansas,99007,105750,103149,59618,92649,99763,94845,56848
California,1296287,1296812,1306123,825862,1240523,1239678,1245997,788024
Colorado,172562,187549,168057,100405,164433,172087,170502,95712

So then if you do:

dat <- read.csv("country.csv")
rowSums(dat)

You will get the output:

Alabama     Alaska    Arizona   Arkansas California   Colorado 
1124975     187265    1619585     711629    9239306    1231307 

If you want to attach it to your data frame you can do:

dat$total <- with(dat, rowSums(dat))

Which will add the column to your data as expected:

               M.5    M5.9  M10.14 M14.17     F.5    F5.9  F10.14 F14.17   total
Alabama     154902  158765  163731  97673  146906  154067  157592  91339 1124975
Alaska       27593   27033   26425  15899   26341   25172   24487  14315  187265
Arizona     223705  236024  230633 138579  214067  218351  227456 130770 1619585
Arkansas     99007  105750  103149  59618   92649   99763   94845  56848  711629
California 1296287 1296812 1306123 825862 1240523 1239678 1245997 788024 9239306
Colorado    172562  187549  168057 100405  164433  172087  170502  95712 1231307