Hello! I have a data frame where the first column contains dates, the second one names and the other two earnings and costs. I want to make a for loop that will calculate the difference between earnings and costs for every name and every date. I have managed to do it for every name but when I try to expand it for every date I am not able to do it. In my loop I assume that i is the date and k is the name so the difference will be for example earnings [i,k] - costs [i,k] but I get the message that there is something wrong with the dimensions.
I wouldn't think to use a loop for this . r is vectorised language.
in base R to find the difference between Petal.Length and Petal.Width for each observation in iris
iris$diff <- iris$Petal.Length - iris$Petal.Width
with library(tidyverse)
iris <- mutate(iris, diff = Petal.Length - Petal.Width)
In future, please try to provide sample data in an easy-to-copy format (as I have done in the code below). This makes it much easier for others to help you.
As nirgrahamuk pointed out, R is vectorized so you don't need loops for these kind of operations. You can directly assign the result of the computation to a new variable.
df <- data.frame(stringsAsFactors = FALSE,
date = c(as.Date("2020-07-01"), as.Date("2020-07-02")),
name = c("John", "Mary"),
earnings = c(1500, 2000),
cost = c(1250, 1500))
df$difference <- df$earnings - df$cost
df
#> date name earnings cost difference
#> 1 2020-07-01 John 1500 1250 250
#> 2 2020-07-02 Mary 2000 1500 500
Created on 2020-07-31 by the reprex package (v0.3.0)
Thank you! My issue was that for a particular date I had many names which were the same so the difference could not be calculated that easily however now everything works ok!
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.