Home > OS >  Adding new column to dataframe (R) with a rowsum
Adding new column to dataframe (R) with a rowsum

Time:10-19

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")

CodePudding user response:

the rowsum() function is for a grouped calculation. Use instead the rowSums() function to do what you want, with quotes on the variable names to avoid your first error

  • Related