0

I am trying to calculate the percent of production by source in 2018 for each continent

Here is my code :

df_tidy  %>%
  group_by (Country, tonnes) %>%
  select (Country, tonnes, year) %>%
  filter (Country %in% c("Africa,Americas,Asia,Europe,Oceania","tonnes")) %>%
  summarise (total.global.production = tonnes/sum(df_tidy$tonnes, na.rm=T)*100)

summarise() regrouping output by 'Country', 'tonnes' (override with .groups argument)". I am unsure how to fix my code since its run but I am not receiving a correct output.

  • I am still receiving the same output message "`summarise()` regrouping output by 'Country', 'tonnes' (override with `.groups` argument)". – Meleine Yao Mar 21 '21 at 15:19
  • That message is default behavior after `summarise()`. It is simple telling you that data were regrouped after the operation, and how you can set the default. It is not an error, warning, or anything else indicating a problem. We can guess where the issue is, but without a reproducible example it would be just that, guesswork. Can you include some sample data (in a way that is easy to copy/paste into R), and expected output? –  Mar 21 '21 at 16:01
  • so I changed my code to this df_tidy %>% group_by(Country,tonnes)%>% select(Country,tonnes,year)%>% filter(year==2018)%>% summarise(total.global.production = tonnes/sum(df_tidy$tonnes, na.rm=T)*100) df<-pivot_wider(data=df_tidy,names_from = c("Africa","Americas","Asia","Europe","Oceania"),values_from =tonnes/sum (df_tidy$tonnes, names_sep = "_")) – Meleine Yao Mar 21 '21 at 16:08
  • @ Adam im not sure how to send a sample of my data set to you – Meleine Yao Mar 21 '21 at 16:09
  • Try giving this a read on [reproducible examples](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Gives some hints on how to share some sample data and include it in the question. –  Mar 21 '21 at 16:12
  • try running : dput(df_tidy) – Meleine Yao Mar 21 '21 at 16:34

1 Answers1

0

Try this :

library(dplyr)

df_tidy  %>%
  filter(Country %in% c("Africa,Americas,Asia,Europe,Oceania","tonnes")) %>%
  group_by(Country, tonnes) %>%
  summarise(total.global.production = tonnes/sum(tonnes, na.rm=TRUE)*100)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213