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
anda
.df[1,3]
does not exist. When doesdf[1,3]<-100
immediately before setting the function, you are creating a new column with all other valuesNA
. You can post the expected output of this code with the question data?– Rui Barradas
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
– Vinicius Soares