我目前正在导入两个表(以最基本的形式)
Table 1
State Month Account Value
NY Jan Expected Sales 1.04
NY Jan Expected Expenses 1.02
Table 2
State Month Account Value
NY Jan Sales 1,000
NY Jan Customers 500
NY Jan F Expenses 1,000
NY Jan V Expenses 100
我的最终目标是创建一个包含前两行值的第3个数据框,并根据函数计算出第4列
NextYearExpenses = (t2 F Expenses + t2 V Expenses)* t1 Expected Expenses
NextYearSales = (t2 sales) * t1 Expected Sales
所以我想要的输出如下
State Month New Account Value
NY Jan Sales 1,040
NY Jan Expenses 1,122
我对R比较陌生,我认为ifelse语句可能是我最好的选择。我尝试合并表并使用简单的列函数进行计算,但没有实际进展。
有什么建议吗?
答案 0 :(得分:2)
您可能需要处理一些数据,但没有异常
require(dplyr)
Table1<-tibble(State=c("NY","NY"), Month=c("Jan","Jan"), Account=c("Expected Sales", "Expected Expenses"), Value=c(1.04,1.02))
Table2<-tibble(State=c("NY","NY","NY","NY"), Month=c("Jan","Jan","Jan","Jan"), Account=c("Sales", "Customers", "F Expenses","V Expenses"), Value=c(1000,500,1000,100))
我要做的第一件事是将帐户重命名为通用名称,即费用,这将有助于我稍后合并到表1
Table2$Account[Table2$Account=="F Expenses"]<-"Expenses"
Table2$Account[Table2$Account=="V Expenses"]<-"Expenses"
然后我使用group_by函数并按州,月份和帐户分组并进行总和
Table2 <- Table2 %>% group_by(State, Month,Account) %>%
summarise(Tot_Value=sum(Value)) %>% ungroup()
head(Table2)
## State Month Account Tot_Value
## <chr> <chr> <chr> <dbl>
## 1 NY Jan Customers 500
## 2 NY Jan Expenses 1100
## 3 NY Jan Sales 1000
然后类似于表1中的帐户重命名
Table1$Account[Table1$Account=="Expected Sales"]<-"Sales"
Table1$Account[Table1$Account=="Expected Expenses"]<-"Expenses"
合并到第三张表Table 3
Table3<- left_join(Table1,Table2)
使用mutate进行所需的操作
Table3 <- Table3 %>% mutate(Value2=Value*Tot_Value)
head(Table3)
## # A tibble: 2 x 6
## State Month Account Value Tot_Value Value2
## <chr> <chr> <chr> <dbl> <dbl> <dbl>
## 1 NY Jan Sales 1.04 1000 1040
## 2 NY Jan Expenses 1.02 1100 1122
答案 1 :(得分:1)
这就是我对dplyr
和tidyr
所做的事情。
首先,我将您的初始表与rbind
合并为一个长格式表。由于您具有每个帐户值的唯一标识符,因此这些标识符不必是单独的表。接下来,我group_by
将状态和月份归为一组,假设最终您将具有各种状态/月份。接下来,我根据您指定的“帐户”的值summarise
,并创建了两个新列。最后,将其转换为所需的长格式,我使用了gather
中的tidyr
从宽格式转换为长格式。您可以通过在%>%
之后删除来将这些命令分成较小的块,以更好地了解每个步骤的作用。
library(dplyr)
library(tidyr)
rbind(df,df2) %>%
group_by(State,Month) %>%
summarise(Expenses = (Value[which(Account == "F Expenses")] + Value[which(Account == "V Expenses")]) * Value[which(Account == "Expected Expenses")],
Sales = Value[which(Account == "Sales")] * Value[which(Account == "Expected Sales")]) %>%
gather(New_Account,Value, c(Expenses,Sales))
# A tibble: 2 x 4
# Groups: State [1]
# State Month New_Account Value
# <chr> <chr> <chr> <dbl>
#1 NY Jan Expenses 1122
#2 NY Jan Sales 1040
答案 2 :(得分:1)
我建议您检出the concept of "tidy data",因为使用当前具有的结构来处理数据存在一些实际挑战。例如。创建t3只需要2-3行代码,所有这些只是为了解决您的数据体系结构:
library(tidyverse)
t1 <- data.frame(State = rep("NY", 2),
Month = rep(as.Date("2018-01-01"), 2),
Account = c("Expected Sales", "Expected Expenses"),
Value = c(1.04, 1.02),
stringsAsFactors = FALSE)
t2 <- data.frame(State = rep("NY", 4),
Month = rep(as.Date("2018-01-01"), 4),
Account = c("Sales", "Customers", "F Expenses", "V Expenses"),
Value = c(1000, 500, 1000, 100),
stringsAsFactors = FALSE)
t3 <- t2 %>%
spread(Account, Value) %>%
inner_join({
t1 %>%
spread(Account, Value)
}, by = c("State" = "State", "Month" = "Month")) %>%
mutate(NewExpenses = (`F Expenses` + `V Expenses`) * `Expected Expenses`,
NewSales = Sales * `Expected Sales`) %>%
select(State, Month, Sales = NewSales, Expenses = NewExpenses) %>%
gather(Sales, Expenses, key = `New Account`, value = Value)