How to add rows together


I'm trying to graph quarterly job losses in a number of different fields, but I want to add up the number of job losses for all the fields together for each quarter, that way i can graph yearly quarters versus jobs lost.

The first column of my table is Quarters (with 17 total rows), but then the rest of the columns are number of job losses. How do I add the rest of the columns together for each quarter? That way I have clear X and Y variables.

Is this the sort of thing you are trying to do?

#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>     filter, lag
#> The following objects are masked from 'package:base':
#>     intersect, setdiff, setequal, union
DF <- data.frame(Quarter=c("Q1","Q2","Q3"),
#>   Quarter  V1  V2 V3
#> 1      Q1 123 495 45
#> 2      Q2 213 372 72
#> 3      Q3 333 528 44
#Sum across rows
DF <- DF %>% rowwise() %>% mutate(Total=sum(c_across(V1:V3)))
#> # A tibble: 3 x 5
#> # Rowwise: 
#>   Quarter    V1    V2    V3 Total
#>   <chr>   <dbl> <dbl> <dbl> <dbl>
#> 1 Q1        123   495    45   663
#> 2 Q2        213   372    72   657
#> 3 Q3        333   528    44   905

Created on 2020-12-06 by the reprex package (v0.3.0)

Yeah, exactly. When I try to use that code, it gives me this error:

Error: Problem with mutate() input Total.
x Can't combine Quarter and 3 .
:information_source: Input Total is sum(c_across(1:16)).
:information_source: The error occurred in row 1.

If the first column is the quarter you should not include it int the sum. Do you mean

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.