-4

I want to sum data using Rstudio. For example in the table below, I want to sum the values of each color (only colors, not vehicle) for each person individually (Ana and Joe). I've tried multiple scripts, but I didn't manage to do so.

Thanks!

#Input
structure(list(
  Vehicle = structure(c(1L, 2L, 3L, 1L, 2L, 3L), 
                    .Label = c("Car","Tram", "Bike"), class = "factor"),
  Color = structure(c(1L, 1L, 1L, 2L, 2L, 3L), 
                    .Label = c("red","blue", "green"), class = "factor"),
  Ana = c(1, 1, 2, 5,5, 0), 
  Joe = c(9,6, 5, 6,4, 7)), 
   row.names = c(NA, 6L), class = "data.frame")

#table
Vehicle Color Ana Joe
1     Car   red   1   9
2    Tram   red   1   6
3    Bike   red   2   5
4     Car  blue   5   6
5    Tram  blue   5   4
6    Bike green   0   7     

Output

#Output
structure(list(
  Color = structure(c(1L, 2L, 3L), 
                    .Label = c("red","blue", "green"), class = "factor"),
  Ana = c(4, 10, 0), 
  Joe = c(20,10, 7)), 
  row.names = c(NA, 3L), class = "data.frame") 

Color Ana Joe
1   red   4  20
2  blue  10  10
3 green   0   7

my dataframe is quite large (+500 rows), so I don't want to type all values in manually

Script I've tried (I've tried a couple, but to be honest, I don't know where to begin)

names <- c("Ana", "Joe")

Heatmap %>%
  group_by(Color) %>%
  summarise(sum_name=(sum(names)))
Annelisa
  • 1
  • 1

1 Answers1

2

If you need to perform a sum across many columns, with the column names coming from a vector, you need to use the across() function:

names <- c("Ana", "Joe")

Heatmap %>%
  group_by(color) %>%
  summarise(across(any_of(names), ~sum(.x)))

However, if you just need to sum values from two columns, it's easier to just specify the column names:

Heatmap %>%
  group_by(color) %>%
  summarise(sum(Ana),
            sum(Joe))

Also, when asking questions it's best if you provide a reproducible example, so that users can quickly and easily reproduce your issue and help you better.