简体   繁体   中英

Create a new dataframe showing the sum of each column

I have a dataframe that looks like this

Date Food     Utility Travel 
01   1.2      12.00    0
02   10.52    0        12.50
03   9.24     0        2.7
04   3.25     0        2.7

I want to create a new dataframe that shows in the first column the type of spending (eg food, utility) and then have the sum in another column. I do not need the date column in the new frame but don't want to omit it from the original.

I hope to have the below output.

Category    Total
Utility     12.00
Food        24.21
Transport   17.9 

I have tried creating a new value for each category, and then trying to pull them together in a dataframe but it has the transposed version, and seems a little long winded if I was to have lots of categories.

You could do this:

library(tidyverse)


test_data <- read_table2("Date Food     Utility Travel
01   1.2      12.00    0
02   10.52    0        12.50
03   9.24     0        2.7
04   3.25     0        2.7") 

test_data%>%
  select(Food:Travel) %>%
  pivot_longer(cols = everything(), names_to = "Category", values_to = "val") %>%
  group_by(Category) %>%
  summarise(Total = sum(val))
#> # A tibble: 3 x 2
#>   Category Total
#>   <chr>    <dbl>
#> 1 Food      24.2
#> 2 Travel    17.9
#> 3 Utility   12

First select the rows you want, then go long, then summarize the categories by sum.

With base R , we can stack the columns except the first to a two column data.frame, and then do a group by sum with aggregate

aggregate(values ~ ind, stack(dat[-1]), sum)
#     ind values
#1    Food  24.21
#2 Utility  12.00
#3  Travel  17.90

Or do colSums on the subset of columns and stack it

stack(colSums(dat[-1]))[2:1]

data

dat <- structure(list(Date = 1:4, Food = c(1.2, 10.52, 9.24, 3.25), 
    Utility = c(12, 0, 0, 0), Travel = c(0, 12.5, 2.7, 2.7)), 
    class = "data.frame", row.names = c(NA, 
-4L))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM