Search code examples
rdataframerowsum

Adding new column to dataframe (R) with a rowsum


Edit: The lecturer suggested using Stack Overflow to seek out community supports and leverage the community when we move into data analytics. I'm a mature student and this is a career pivot attempt. My goal is to learn and folks are great - thank you for your help this is my first assignment, I am trying to work through it myself - but I hit a roadblock

I have a data frame CrimeDataTheft. It has columns of mostly numeric and one character. I am trying to create a new column called Country Totals that will be the sum of the rows numeric data (named below). I did not include the column Country as it is a character column.

I am placing the old and new columns with the sumed variable in a dataframe called SumCrimeData

SumCrimeData <- CrimeDataTheft$Country.Totals = rowsum(CrimeDataTheft[,c(Intentional.homicide, 
                   Attempted.intentional.homicide, Assault, Kidnapping, Sexual.violence
                   , Robbery, Unlawful.acts.involving.controlled.drugs.or.precursors)], na.rm = TRUE)

The error I am getting is

Error in `[.data.frame`(CrimeDataTheft, , c(Intentional.homicide, Attempted.intentional.homicide,  : 
  object 'Intentional.homicide' not found

I am pulling the data from a csv and the name is copied exactly. Can someone please point out where I'm going wrong? Thank you!!

dput(head(CrimeDataTheft, 5)) 
structure(list(Country = c("Albania", "Austria", "Belgium", "Bosnia and Herzegovina", 
"Bulgaria"), Intentional.homicide = c(2.03, 0.84, 1.27, NA, 1.14
), Attempted.intentional.homicide = c(3.25, 1.93, 8.87, NA, 0.54
), Assault = c(5.52, 43.29, 556.36, NA, 39.54), Kidnapping = c(0.14, 
0.07, NA, NA, 1.03), Sexual.violence = c(5.38, 50.9, 77.45, NA, 
8.64), Robbery = c(3.42, 29.67, 140.14, NA, 16.9), Unlawful.acts.involving.controlled.drugs.or.precursors = c(70.26, 
494.05, 547.74, NA, 78.14), Country.Totals = c(90, 620.75, 1331.83, 
0, 145.93), Country.Totals.per.000s = c(90, 620.75, 1331.83, 
0, 145.93)), row.names = c(NA, 5L), class = "data.frame")

Solution

  • Based on what you mentioned above in your comment, it does not look like you already have a SumCrimeData dataframe. If you decide to use rowSums instead of rowsum you will need to create the SumCrimeData dataframe.

    The should sum the rows that you selected and create a new column called Country.Totals

    CrimeDataTheft$Country.Totals <- rowSums(CrimeDataTheft[,c("Intentional.homicide","Attempted.intentional.homicide", "Assault", "Kidnapping", "Sexual.violence","Robbery", "Unlawful.acts.involving.controlled.drugs.or.precursors")], na.rm = TRUE)
    

    You can then create the new dataframe by doing the following

    SumCrimeData <- as.data.frame(CrimeDataTheft$Country.Totals)
    

    or if you don't want it as a dataframe you can do this

    SumCrimeData <- CrimeDataTheft$Country.Totals
    

    I am not sure you can create a new column and a new dataframe in the same step like you showed in your question i.e. SumCrimeData <- CrimeDataTheft$Country.Totals = rowsum(...)