Hey @williaml, thanks for giving me the tip, I think that will be easier now, here's the example:
df1 that is a huge data base that I alredy have:
df1 <- data.frame(stringsAsFactors = TRUE,
State = c("RJ","RJ","RJ","RJ","SP","SP","SP","SP"),
Account = c("revenue","rev_transfers","contribution_rev",
"expenses_transfers","revenue", "rev_transfers",
"contribution_rev","expenses_transfers"),
value_t1 = c(5, 1, 4, 9, 4, 7, 4, 6),
value_t2 = c(6, 3, 5, 7, 1, 4, 2, 6),
value_t3 = c(9, 8, 0, 1, 2, 7, 8, 3),
value_t4 = c(7, 2, 2, 8, 9, 4, 5, 1),
sum_t = c(sum_t <- df1$value_t1 + df1$value_t2 + df1$value_t3 + df1$value_t4)
)
df2 its another data base that I alredy have:
df2 <- data.frame(stringsAsFactors = TRUE,
State = c("RJ","RJ","RJ","RJ","SP","SP","SP","SP"),
Account = c("result_t1","result_t2","result_t3",
"result_t4","result_t1", "result_t2",
"result_t3","result_t4"),
result_df2 = c(-3,-8,1,7,9,2,-1,-2)
)
I'll separate the problems in 2 parts to try to make it easier:
PART 1
- What I need to do is to create one formula that will do basic operations, like:
result_df3 <- df1$value_t1 - df1$value_t2 - df1$value_t3 + df1$value_t4
But the my problem is that I need to make R do this operations calling the rows by its names, because of the size of the data base, like:
result_df3 <- df1$revenue - df1$rev_transfers - df1$contribution_rev + df1$expenses_transfers
And I don't know how to do this operations calling by the row's name, only by the column's name.
PART 2
2. With this formula, I need to create a new df (df3), and compare with the results of df2, using some kind of if loop code to say if they're equal or not, like:
df3 <- data.frame(stringsAsFactors = FALSE,
State = c("RJ","RJ","RJ","RJ","SP","SP","SP","SP"),
Account = c("result_t1","result_t2","result_t3",
"result_t4","result_t1", "result_t2",
"result_t3","result_t4"),
result_df2,
result_df3 = c(result_df3 <- df1$value_t1 - df1$value_t2 - df1$value_t3 + df1$value_t4),
equal = c(
for(State in df3){
if (result_df2 == result_df3){
print("yes")
} else {
print("no")
}})
)
And my if is returning me this:
1: In if (result_df2 == result_df3) {:
the condition has length> 1 and only the first element will be used
Do you or anyone know how to solve this?