0

I have to prepare charts with R for a case study. I have a dataset containing tens of thousands of rows organized as follows:

Platform | Profits

Desktop  |  608.50

Desktop  |  591.54

Desktop  |   83.21

Mobile   |   27.13

Mobile   |  133.81

Mobile   |  201.13

There are tens of thousands more Desktop and Mobile rows and their profits than what I posted, but I would like to know if there is a way for me to combine all of the Desktop and Mobile profits so that the resulting table is as follows so I can graph the totals easier:

Platform | Profit

Desktop  |5839.01

Mobile   |4219.58

I tried subset, sumRows, sumCols, but I can't seem to make a table of this desired format. I am 100% okay with having to break this into parts.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
agasco3
  • 1
  • 1

1 Answers1

0

You can use the following code

library(tidyverse)

df %>% 
  group_by(Platform) %>% 
  summarise(sum_profit = sum(Profits))

Output

# A tibble: 2 x 2
  Platform sum_profit
  <chr>         <dbl>
1 Desktop       1283.
2 Mobile         362.

Data

df = structure(list(Platform = c("Desktop", "Desktop", "Desktop", 
"Mobile", "Mobile", "Mobile"), Profits = c(608.5, 591.54, 83.21, 
27.13, 133.81, 201.13)), class = "data.frame", row.names = c(NA, 
-6L))
UseR10085
  • 7,120
  • 3
  • 24
  • 54
  • Thank you! I tried this, however the console returns the following error > platformsValues %>% + group_by('Platform Type Name') %>% + summarise(sum = sum('Net Gross Booking Value USD')) Error: Problem with `summarise()` column `sum`. i `sum = sum("Net Gross Booking Value USD")`. x invalid 'type' (character) of argument i The error occurred in group 1: "Platform Type Name" = "Platform Type Name". – agasco3 Nov 24 '21 at 05:14
  • Don't keep space in the variable name e.g. `Platform Type Name` to `PlatformTypeName` or `Platform_Type_Name` and `Net Gross Booking Value USD` to `NetGrossBookingValueUSD` or else you can use shorter column names as you have given in the question. – UseR10085 Nov 24 '21 at 05:18
  • I had a feeling the spacing was an issue since I'd have to put it in quotation marks, but that's how it is in the excel sheet, should I just change the excel sheet? – agasco3 Nov 24 '21 at 05:20
  • As in like(platformsValues, 'Net Gross Booking Value USD') ? – agasco3 Nov 24 '21 at 05:49
  • No use backticks. – UseR10085 Nov 24 '21 at 06:31
  • If the answer helped you, you can [accept it](https://stackoverflow.com/help/someone-answers). – UseR10085 Nov 24 '21 at 07:11