简体   繁体   English

创建一个新的 dataframe 显示每列的总和

[英]Create a new dataframe showing the sum of each column

I have a dataframe that looks like this我有一个看起来像这样的 dataframe

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.我想创建一个新的 dataframe,它在第一列中显示支出类型(例如食物、公用事业),然后在另一列中显示总和。 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.我希望有下面的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.我尝试为每个类别创建一个新值,然后尝试将它们组合到 dataframe 中,但它具有转置版本,如果我有很多类别,似乎有点啰嗦。

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.首先 select 你想要的行,然后 go 长,然后按总和汇总类别。

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使用base R ,我们可以将除第一列之外的列stack到两列 data.frame 中,然后通过汇总sum 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或者对列的子集执行colSums并将其stack

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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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