0

I'm new to R and I was trying to combine the columns (x axis) of a dataframe together in R to get the sum in a single column.

Tourism      Weekdays        Weekends
Profit       100             20
Visitors     250             75

How would I be able to combine Weekdays and Weekends into a single column called Week so it would look like the one below? As I understand it is possible to do a row addition if I wanted to combine Profit and Visitors so my thought was there must be something similar for columns.

Tourism      Week 
Profit       120  
Visitors     325  
Tony H
  • 53
  • 7

1 Answers1

1

Two possible approachs

Data

df <-
  data.frame(
    Tourism = c("Profit","Visitors"),
    Weekdays = c(100,250),
    Weekends = c(20,75)
  )

Base R

df$Week <- df$Weekdays + df$Weekends

Tidyverse

library(dplyr)

df %>% 
  mutate(Week = Weekdays + Weekends)

Result

# A tibble: 2 x 4
  Tourism  Weekdays Weekends  Week
  <chr>       <dbl>    <dbl> <dbl>
1 Profit        100       20   120
2 Visitors      250       75   325
Vinícius Félix
  • 8,448
  • 6
  • 16
  • 32