0

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")
iKcon
  • 71
  • 7
  • 1
    You need to put the column names within quotes – Cath Oct 18 '21 at 15:14
  • Thank you - you moved me forward - the error now is about "Groups" I don't understand groups at a basic level in relation to my data. The error now reads ``` Error in rowsum.data.frame(CrimeDataTheft[, c("Intentional.homicide", : argument "group" is missing, with no default ``` – iKcon Oct 18 '21 at 15:19
  • It might be easier if you give an example of your data using `dput` instead of posting the errors. This will allow us to test our suggestions before posting them – neuron Oct 18 '21 at 15:34
  • OK, going to look up dput and use it here - cheers – iKcon Oct 18 '21 at 15:35
  • please do not truncate, we need to see everything so we can recreate your dataset. Try `dput(head(df, 5))` to show make the output smaller. Change `df` to the name of your dataframe – neuron Oct 18 '21 at 15:41
  • I added it into the question text body - cheers – iKcon Oct 18 '21 at 15:47
  • P.S. I believe the reason you are getting a low response rate to your question is that it looks like this is a homework question. See [How do I ask and answer homework questions](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions) – neuron Oct 18 '21 at 16:15
  • Thank you - to be clear I'm declaring all Stack assistance as such in the code comments. – iKcon Oct 18 '21 at 16:23

2 Answers2

1

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

neuron
  • 1,949
  • 1
  • 15
  • 30
0

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

BPeif
  • 191
  • 6
  • Brilliant!! Thank you. Really sorry about this but it is telling me now that the new dataframe SumCrimeData is not found. Is it not possible to create the data frame the way I've added it above? SumCrimeData <- CrimeDataTheft$Country.Totals = rowSums(.... – iKcon Oct 18 '21 at 15:30