如何将超过某一点的列集合成行[重复]。

问题描述 投票:0回答:1

我有一个数据框架。df这看起来像下面的样子(实际上,我有更多的列,一直到2020_Actual和2020_Budget)。

Company     Code                        2013_Actual 2013_Budget 2014_Actual 2014_Budget
CompanyX    100 - Salary & Wages        1500        1601        1620        1680
CompanyX    102 - Bonus & Incentives    3000        3500        3150        3300        
CompanyX    104 - Overtime              60          70          78          82
CompanyX    110 - Temporary Help        35          55          48          56  
CompanyX    112 - Taxes & Benefits      800         840         880         900

我想把所有的列都折叠起来 Code 变成 Actual, BudgetYear 这样的列子。

Company     Code                        Actual  Budget  Year
CompanyX    100 - Salary & Wages        1500    1601    2013
CompanyX    102 - Bonus & Incentives    3000    3500    2013
CompanyX    104 - Overtime              60      70      2013
CompanyX    110 - Temporary Help        35      55      2013
CompanyX    112 - Taxes & Benefits      800     840     2013
CompanyX    100 - Salary & Wages        1620    1680    2014
CompanyX    102 - Bonus & Incentives    3150    3300    2014
CompanyX    104 - Overtime              78      82      2014
CompanyX    110 - Temporary Help        48      56      2014
CompanyX    112 - Taxes & Benefits      880     900     2014

这是我目前的做法,但没有得到正确的结果。如果不需要指定每一个我想收集的列的名称,那也会很好,因为我的输入表的列名可能会改变。

library(dplyr)
library(tidyr)

# Read in data
df <- read.csv("inputTable.csv", header=T, stringsAsFactors=F)

# Collapse columns
actuals<- gather(df, 
                       key = "Year",
                       value = "Actuals",
                       2013_Actual,2014_Actual,2015_Actual,2016_Actual,2017_Actual,2018_Actual,2019_Actual,2020_Actual)

budget <- gather(df, 
                       key = "Year",
                       value = "Budget",
                       2013_Budget,2014_Budget,2015_Budget,2016_Budget,2017_Budget,2018_Budget,2019_Budget,2020_Budget)

# grab only relevant columns
df_actuals_final <- select(df_actuals, Company, Code, Year, Actuals)
df_budget_final <- select(df_budget, Company, Code, Year, Budget)

# merge 
m <- merge(df_actuals_final, df_budget_final, by=c("Company","Code", "Year"))

write.csv(m, "inputTable.mod.csv", quote=F, row.names=F)
r tidyr
1个回答
0
投票

gather 已被淘汰,取而代之的是 pivot_longer 这使得这样的转换变得非常简单了。

tidyr::pivot_longer(df, cols = -c(Company, Code), 
                        names_to = c('Year', '.value'), 
                        names_sep = '_')


# A tibble: 10 x 5
#   Company  Code                 Year  Actual Budget
#   <chr>    <chr>                <chr>  <int>  <int>
# 1 CompanyX 100-Salary&Wages     2013    1500   1601
# 2 CompanyX 100-Salary&Wages     2014    1620   1680
# 3 CompanyX 102-Bonus&Incentives 2013    3000   3500
# 4 CompanyX 102-Bonus&Incentives 2014    3150   3300
# 5 CompanyX 104-Overtime         2013      60     70
# 6 CompanyX 104-Overtime         2014      78     82
# 7 CompanyX 110-TemporaryHelp    2013      35     55
# 8 CompanyX 110-TemporaryHelp    2014      48     56
# 9 CompanyX 112-Taxes&Benefits   2013     800    840
#10 CompanyX 112-Taxes&Benefits   2014     880    900

数据

df <- structure(list(Company = c("CompanyX", "CompanyX", "CompanyX", 
"CompanyX", "CompanyX"), Code = c("100-Salary&Wages", "102-Bonus&Incentives", 
"104-Overtime", "110-TemporaryHelp", "112-Taxes&Benefits"), 
`2013_Actual` = c(1500L, 3000L, 60L, 35L, 800L), `2013_Budget` = c(1601L, 
3500L, 70L, 55L, 840L), `2014_Actual` = c(1620L, 3150L, 78L, 48L, 880L), 
`2014_Budget` = c(1680L, 3300L, 82L, 56L, 900L)), 
class = "data.frame", row.names = c(NA, -5L))
热门问题
推荐问题
最新问题