1

Ok a create 17 vectors (o1, o2, ..., o17) for a accounting exercise. Each vector has 3 elements account(character), T operation(character), and Value (numeric). I use rbind and data frame it. so I had this.

                           Account T operation  Value
o1                           Cash       Debit 100000
o2                Paid in Capital      Credit  10000
o3                           PP&E       Debit  10000
o4                           Cash      Credit  10000
o5                           Cash       Debit  48000
o6                           COGS       Debit  24000
o7         Revenue wardrobes 1stY      Credit  48000
o8       Inventory wardrobes 1stY      Credit  24000
o9                           Cash       Debit  20000
o10                          COGS       Debit  10000
o11        Revenue bookcases 1stY      Credit  20000
o12      Inventory bookcases 1stY      Credit  10000
o13            Operating expenses       Debit  30000
o14                          Cash      Credit  20000
o15              Accounts Payable      Credit  10000
o16          Depreciation Expense       Debit   1000
o17 Accumulated depreciation 1stY      Credit   1000

but I want to sum credits and debits with if and else. I will not describe my function because could have a lot of errors and for my experts' brothers, I suppose that some 3 lines of code I could get the answer.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Sounds like you may just not know the terminology to look up. There are multiple ways to do this (without loops). Please check out this other almost identical question with multiple possible methods: https://stackoverflow.com/questions/1660124/how-to-sum-a-variable-by-group – Adam Sampson Nov 14 '18 at 21:04

1 Answers1

0

I am not that I have understand what you want but I think you want this:

credits_indices <- as.character(data[["Credits"]]=="Credits" #indices to the credits
values <- values[["Value"]] #all the values
total <- sum(values) #total value
total_credits <- sum(values[credits_indices]) #total credits
total_debit <- total-total_credits # total debit

I hope this is what you want! If so please check this for answer to your question.

Manos Papadakis
  • 564
  • 5
  • 17