Conditional Sum on each R line?

Asked

Viewed 518 times

0

I’m trying to create a function that takes the fixed value of a row df[1.3] and subtracts it by the value of another row of a dataframe df[,1] and the result of that subtraction is also subtracted by the bottom line of the same column df[,1](and so on)and at the same time run a ifelse that if the resulting value of these substations is less than a certain value, add the result of the subtractions by another objectB. Does anyone have any idea how I do it?

my script:

objetoA<- 100

valor <- 20

objetoB<-40
a<-c(1,2,3,4,5)
b<-c(10,10,10,10,10)
df <- data.frame(b,a)
df[1,3]<-100
funcao<- function(x) {df[1,3] - df[,1] + (ifelse(df[1,3] - df[,1] <= valor,objetoB+df[1,3] - df[,1],0))}

print((last(map(objetoA, funcao))))

So that it is in the Script subtracts df[1,3]for each row separately, row by row, and returns me the result of each row as if I took the column and did this: df[1,3]- df[,1] only or is not subtract the result of the substations by the row below..

But I want him to take the value of the first subtraction and subtract it by the bottom line and so on as he checks the condition of the... Does anyone have any idea how to do that?

The result in this case would be only 100-10-10-10-10-10=50

  • df only has two columns, b and a. df[1,3] does not exist. When does df[1,3]<-100 immediately before setting the function, you are creating a new column with all other values NA. You can post the expected output of this code with the question data?

  • Thanks for the reply Rui, when I give View(df) after doing df[1,3]<-100, I see that a value of 100 was created for the first row of the third column. I would like the exit to this code to be 50 because the condition of the ifelse will not be triggered because the result of the subtractions does not reach less than or equal to the value 20. The account the function would make would be 100-10-10-10-10-10= 50

3 answers

0

I’ve managed to get the code for whoever wants it:

objetoA <- 100
valor <- 20
objetoB <- 40

a <- c(1,2,3,4,5)
b <- c(10,10,10,10,10)

df <- data.frame(b,a)
df[1,3] <- 100

subtrair <- append(df[1,3], df$b)

resultado <- reduce(subtrair, function(a,b) {a-b})

while (resultado <= valor) { 
  resultado <- resultado + valor
}

print(resultado)  

>50

0

When you are dealing with some repetition strategy, it is worth checking if it is not a loop (loop) that you are looking for. No snippet below I used a for.

objetoA<- 100
valor <- 20
objetoB<-40
a<-c(1,2,3,4,5)
b<-c(10,10,10,10,10)
df <- data.frame(b,a)
df[1,3]<-100

> df
   b a  V3
1 10 1 100
2 10 2  NA
3 10 3  NA
4 10 4  NA
5 10 5  NA

for (i in seq(nrow(df))) {
  res <- (df[1,3] - df[i, 1])
  if (res < valor) {
    res + objetoB
  }
}

> res
[1] 90
  • Thank you very much for the answer, but I came upon a doubt in his code, he subtracted the 100 by 10 just once... my intention was to take the result of that subtraction and subtract by the bottom line until the condition of the ifelse be true to add with another object, and after that continue subtracting. In the df in question the res should result in 50 as the 100 would be subtracted by 10 five times and the ifelse condition would not be true at any time in this df

0

I believe that the following function does what the question asks, with the difference that the final result is 60 and not 50, since if the first value of df[1, 3] should then be maintained the function only makes 4 subtractions and not 5.

funcao2 <- function(x, val, obj){
  for(i in seq_len(nrow(x))[-1]){
    x[i, 3] <- x[i - 1, 3] - x[i, 'b']
    if(x[i, 3] <= val) x[i, 3] <- x[i, 3] + obj
  }
  x
}

funcao2(df, valor, objetoB)
#   b a  V3
#1 10 1 100
#2 10 2  90
#3 10 3  80
#4 10 4  70
#5 10 5  60

Browser other questions tagged

You are not signed in. Login or sign up in order to post.