dplyr按行名进行突变

时间:2018-08-17 15:27:41

标签: r dplyr

我有一些数据,我试图基于行名运行一些非常基本的计算。我基本上想使用dplyr将行名中的Total Cash Flow From Operating Activities + Capital Expenditures加在一起,并将它们存储为新行。下面的代码最终实现了我想要的功能,但是它相当庞大,我试图在一个管道函数(而不是两个)中创建它。

CFt <- as.data.frame(t(CF))
 library(dplyr)
 FCF <- NULL
 FCF <- CFt %>%
  select(`Total Cash Flow From Operating Activities`, `Capital Expenditures`)

 FCF <- FCF %>% 
   rownames_to_column('Firms') %>%
   mutate(FCF = `Total Cash Flow From Operating Activities` + `Capital Expenditures`) %>% 
   column_to_rownames('Firms') %>%
   t()

数据:

CF <- structure(list(GE2017 = c(-5786000, 7359000, 5568000, 1551000, 
987000, 747000, NA, 10426000, -7371000, NA, 5891000, 2322000, 
-8650000, NA, -8952000, 1006000, -19146000, 891000, -5507000), 
    GE2016 = c(8831000, 7139000, -19373000, 1514000, 3034000, 
    -1389000, NA, -244000, -7199000, NA, -10250000, 49202000, 
    -8806000, NA, -58411000, -485000, -89131000, -1146000, -41319000
    ), GE2015 = c(-6126000, 6509000, 21411000, -52000, -1537000, 
    -314000, NA, 19891000, -7309000, NA, -5316000, 59488000, 
    -9295000, NA, -57546000, -8114000, -76054000, -3464000, -138000
    ), GE2014 = c(15233000, 6423000, 8788000, -1913000, 50000, 
    -872000, NA, 27709000, -7134000, NA, 406000, -5034000, -8852000, 
    NA, -30190000, 23304000, -16956000, -3492000, 2224000), GOOG2017 = c(12662000, 
    6899000, 8284000, -3768000, 1121000, NA, 3682000, 37091000, 
    -13184000, -19448000, 1419000, -31401000, NA, NA, -86000, 
    -3366000, -8298000, 405000, -2203000), GOOG2016 = c(19478000, 
    6100000, 7158000, -2578000, 333000, NA, 2420000, 36036000, 
    -10212000, -18229000, -1978000, -31165000, NA, NA, -1335000, 
    -3304000, -8332000, -170000, -3631000), GOOG2015 = c(16348000, 
    5024000, 5609000, -2094000, 246000, NA, 1618000, 26572000, 
    -9950000, -13635000, 75000, -23711000, NA, NA, -23000, -2422000, 
    -4225000, -434000, -1798000), GOOG2014 = c(14136000, 4601000, 
    3615000, -1641000, 261000, NA, 1461000, 23024000, -11014000, 
    -6222000, 628000, -21055000, NA, NA, -18000, -2069000, -2087000, 
    -433000, -551000), NVDA2018 = c(3047000, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), 
    NVDA2017 = c(1666000, 187000, 473000, -321000, 184000, -375000, 
    -167000, 1672000, -176000, -619000, -5000, -793000, -261000, 
    NA, 1315000, -24000, 291000, NA, 1170000), NVDA2016 = c(614000, 
    197000, 386000, -32000, -11000, 66000, -74000, 1175000, -86000, 
    -339000, 18000, -4e+05, -213000, 120000, 1315000, 4000, -676000, 
    NA, 99000), NVDA2015 = c(631000, 220000, 230000, -49000, 
    -27000, -95000, -32000, 906000, -122000, -625000, -1000, 
    -727000, -186000, 154000, 1315000, 12000, -834000, NA, -655000
    )), .Names = c("GE2017", "GE2016", "GE2015", "GE2014", "GOOG2017", 
"GOOG2016", "GOOG2015", "GOOG2014", "NVDA2018", "NVDA2017", "NVDA2016", 
"NVDA2015"), row.names = c("Net Income", "Depreciation", "Adjustments To Net Income", 
"Changes In Accounts Receivables", "Changes In Liabilities", 
"Changes In Inventories", "Changes In Other Operating Activities", 
"Total Cash Flow From Operating Activities", "Capital Expenditures", 
"Investments", "Other Cash flows from Investing Activities", 
"Total Cash Flows From Investing Activities", "Dividends Paid", 
"Sale Purchase of Stock", "Net Borrowings", "Other Cash Flows from Financing Activities", 
"Total Cash Flows From Financing Activities", "Effect Of Exchange Rate Changes", 
"Change In Cash and Cash Equivalents"), class = "data.frame")

2 个答案:

答案 0 :(得分:3)

如果我们使用bind_rows,它将删除行名并分配默认行号。因此,我们正在使用rbind

library(tidyverse)
CF %>%
   rownames_to_column('rn') %>% 
   filter(rn %in% c( "Total Cash Flow From Oprating Activities" , 
         "Capital Expenditures" )) %>% 
   summarise_at(-1, sum, na.rm = TRUE) %>% 
   `row.names<-`(., 'TotalCashFlowOPAc+CapitalExpend') %>% 
   rbind(CF, .)

答案 1 :(得分:1)

与@akrun的解决方案略有不同

library(tibble)
library(dplyr)

CF %>% 
  rownames_to_column('rn') %>%
  filter(rn %in% c('Total Cash Flow From Operating Activities',
                   'Capital Expenditures')) %>%
  group_by(rn) %>%
  summarize_all(sum) %>%
  as.data.frame() %>%
  column_to_rownames('rn') %>%
  rbind(., FCF = colSums(.))