0

I have a data set I imported into R, in which there are certain parts of the data set that have two rows of the same time period, but with different values in their deliveries section. I have attached the data set: https://docs.google.com/spreadsheets/d/1n4STItqn_HFwrs_AiwrcIyTwEQYMdpGq/edit?usp=sharing&ouid=109726100465728977286&rtpof=true&sd=true

Is there a way to combine the deliveries values that are of the same year and countrycode?

I have currently tried to use the unique function such as:


ArmsSales <- read_excel("USArmsSales.xlsx")
ArmsSales <- unique(ArmsSales[, Deliveries = sum(Deliveries), by = c("CountryCode", "Year", "Country")])


Also tried to use an aggregate function, but failed terribly. I have seen people recommend code that creates a data frame, but when I tried to do that, or something similar it did not work for me. Any one have advice or a solution?

  • "Combine" could mean many things, do you mean "sum(Deliveries) by CountryCode,Year"? This is a [dupe](https://stackoverflow.com/q/11562656/3358272), likely just `ArmsSales %>% group_by(Country, CountryCode, Year) %>% summarize(Deliveries = sum(Deliveries))`. – r2evans Nov 18 '22 at 02:07
  • If that doesn't work for you, then please [edit] your question and put in real data, not an image of it, likely the easiest is just paste the output from `dput(head(ArmsSales))` (assuring that you have a dupe in that sample). Also, please explain why the suggested code and/or link do not work for this data. When you do that, please @ping me and we can work to resolve the issue. Thanks! – r2evans Nov 18 '22 at 02:08
  • I mean that I would like to try to add the values of duplicate countrycode and year together so they are a singular value for that year, if that is possible? – Kenzie DeKeyser Nov 18 '22 at 02:12
  • Did you try my code? (Assuming `library(dplyr)` or `library(tidyverse)`, I inferred this was safe by your use of `read_excel`.) – r2evans Nov 18 '22 at 02:15
  • @r2evans sorry was trying to show error code but that didn't work – Kenzie DeKeyser Nov 18 '22 at 02:24
  • I also tried to attach my data set, hopefully this hyperlink works, I can't seem to make a table work at the moment. Maybe I will have to try the dyplyr libary. I do have tidyverse inputed – Kenzie DeKeyser Nov 18 '22 at 02:26
  • When using the group_by function code, the error it gives me is: Error in 'vec_as_location' '...' must be empty I don't know if that helps at all? – Kenzie DeKeyser Nov 18 '22 at 02:29
  • I looked at your data. Your `Deliveries` column is `character`. R (and many programming languages, in general) do not automatically try to parse a string to see if it could be a number and then calculate things based on that. It's a string, so it doesn't "add". You need to first convert it to a number. `ArmsSales %>% mutate(Deliveries = readr::parse_number(Deliveries)) %>% group_by(Country, CountryCode, Year) %>% summarize(Deliveries = sum(Deliveries), .groups = "drop")` works for me. I cannot find how that data would produce your `vec_as_location` error, so you're doing something else. – r2evans Nov 18 '22 at 12:48

0 Answers0