简体   繁体   English

如何根据 R 中的特定分类列值创建两个金额列

[英]How to Create Two Amount Columns Based on Specific Categorical Column Values in R

I'm relatively new to R and I have a dataframe that looks like this:我对 R 比较陌生,我有一个如下所示的数据框:

1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10
Name姓名 Max最大限度 Max最大限度 Max最大限度 Joey乔伊 Joey乔伊 Nancy南希 Nancy南希 Nancy南希 Linda琳达 Linda琳达
Amount_Type金额类型 InternetBill互联网账单 Groceries杂货 WaterBill水单 InternetBill互联网账单 Groceries杂货 WaterBill水单 Groceries杂货 InternetBill互联网账单 WaterBill水单 Groceries杂货
Amount数量 $75 75 美元 $230.66 230.66 美元 $40 40 美元 $70 70 美元 $188.75 188.75 美元 $35 35 美元 $175.89 175.89 美元 $75 75 美元 $30 30 美元 $236.87 236.87 美元

I need to add 3 more rows and pivot the dataframe:我需要再添加 3 行并旋转数据框:

The dataframe needs to be grouped by name and outputs 3 totals columns:数据框需要按名称分组并输出 3 个总计列:

  1. Fixed_Cost which should include InternetBill and WaterBill amounts Fixed_Cost 应包括 InternetBill 和 WaterBill 金额
  2. Variable_Cost which should include Groceries Variable_Cost 应包括杂货
  3. Total_Cost which should be fixed + variable costs Total_Cost 应该是固定的 + 可变成本

So something like this:所以是这样的:

Name姓名 Fixed_Cost固定成本 Variable_Cost可变成本 Total_Cost总消耗
Max最大限度 $115 115 美元 $230.66 230.66 美元 $345.66 345.66 美元
Joey乔伊 $70 70 美元 $188.75 188.75 美元 $258.75 258.75 美元
Nancy南希 $110 110 美元 $175.89 175.89 美元 $285.89 285.89 美元
Linda琳达 $30 30 美元 $236.87 236.87 美元 $266.87 266.87 美元

Any advice on how to go about doing this?关于如何去做这件事的任何建议? Thanks!谢谢!

library(tidyverse)   

setNames(data.frame(t(df1[,-1])), df1[,1]) %>%
  pivot_wider(Name, names_from = Amount_Type, values_from = Amount,
              values_fn = parse_number, values_fill = 0) %>%
  mutate(Fixed_cost = InternetBill + WaterBill, variable_cost = Groceries,
         Total_Cost = Fixed_cost + variable_cost, .keep ='unused')

# A tibble: 4 x 4
  Name  Fixed_cost variable_cost Total_Cost
  <chr>      <dbl>         <dbl>      <dbl>
1 Max          115          231.       346.
2 Joey          70          189.       259.
3 Nancy        110          176.       286.
4 Linda         30          237.       267.

If we transpose the data, it becomes more easier to do a group by sum如果我们transpose数据,那么按sum进行分组会变得更容易

library(data.table)
data.table::transpose(setDT(df1), make.names = 1)[, 
  Amount := readr::parse_number(Amount)][, 
 .(Fixed_Cost = sum(Amount[Amount_Type %in% c("InternetBill", "WaterBill")]), 
 Variable_Cost =  sum(Amount[!Amount_Type %in% c("InternetBill", "WaterBill")])),
       by = Name][,
   Total_Cost := Fixed_Cost + Variable_Cost][]

-output -输出

     Name Fixed_Cost Variable_Cost Total_Cost
   <char>      <num>         <num>      <num>
1:    Max        115        230.66     345.66
2:   Joey         70        188.75     258.75
3:  Nancy        110        175.89     285.89
4:  Linda         30        236.87     266.87  

data数据

df1 <- structure(list(`0` = c("Name", "Amount_Type", "Amount"), `1` = c("Max", 
"InternetBill", "$75"), `2` = c("Max", "Groceries", "$230.66"
), `3` = c("Max", "WaterBill", "$40"), `4` = c("Joey", "InternetBill", 
"$70"), `5` = c("Joey", "Groceries", "$188.75"), `6` = c("Nancy", 
"WaterBill", "$35"), `7` = c("Nancy", "Groceries", "$175.89"), 
    `8` = c("Nancy", "InternetBill", "$75"), `9` = c("Linda", 
    "WaterBill", "$30"), `10` = c("Linda", "Groceries", "$236.87"
    )), class = "data.frame", row.names = c(NA, -3L))

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

相关问题 基于 R 中的其他两个列创建一个新的分类“比较检测”列(九个选项答案) - Create a new categorical “comparison detection” column based on two other columns in R (nine option answers) 根据R中数据帧中其他列中的值的条件创建一个新的分类列 - Create a new categorical column based on conditions of values in other columns in a dataframe in R 根据两列中的值在 R 中创建新列 - Create new column in R based upon values in two columns 基于 data.table R 中的两列创建一个分类变量 - Create a categorical variable based on two columns in data.table R 如何创建一个逻辑向量来指示两列中的值在 R 中的分类因子中是否相同? - How to create a logical vector that indicates whether the values in two columns are the same across categorical factors in R? R 根据特定列创建列 - R create column based on specific columns 如何根据 R 中的两个分类值创建新变量? - How do I create a new variable based on two categorical values in R? 如何根据r中的两列创建序列数值列? - How to create a sequence numerical column based on two columns in r? 如何根据R中另外两列的分组来标准化列中的值? - How to standardize values in a column based on grouping by two other columns in R? 根据其他列中的特定值创建列 - Create column based on specific values in other columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM