0
$\begingroup$

I am trying to build a for loop in R. but facing the error as the loop is not creating the exact variable and may be double counting. What i trying to do is

  1. The code has to go through every row and select all rows which are less than the 6 months of the date_created variable) and create a count variable. For eg. it will take first row and select rows in the datasets which satisify filter condition and less than 6 months of the date_created variable. Then it will go to the next row and select rows in the datasets which satisfy filter condition and less than 6 months of the date_created variable for that row.

  2. enter image description hereThen it has to create the second variable which is the sum of all the values in column "nos" which satisfy the above filter conditions and less than 6 months of the date_Created variable.

Please find the answers to the questions.

Code : datanew1$count <- 0 # initialise counter i <- 1 # for testing for (i in 1:nrow(datanew1)){ # loop through rows # compare all rows to row i compare <- (datanew1location==datanew1location[i]) & #(datanew1department==datanew1department[i]) & (datanew1grade==datanew1grade[i]) & (datanew1DateCreated>=datanew1less6month[i])& (datanew1DateCreated<=datanew1Date_Created[i]) # count number of TRUE results (subtract 1 for row i) datanew1$count[i] <- sum(compare) }

Results enter image description here

Final outcome enter image description here

$\endgroup$
  • $\begingroup$ Please post 1) the code you have tried 2) the results 3) the desired results $\endgroup$ – desertnaut Jun 26 '19 at 0:19
  • $\begingroup$ Thanks. Please find below. $\endgroup$ – anonymous Jun 26 '19 at 12:34
  • $\begingroup$ datanew1$count <- 0 # initialise counter i <- 1 # for testing for (i in 1:nrow(datanew1)){ # loop through rows # compare all rows to row i compare <- (datanew1$location==datanew1$location[i]) & #(datanew1$department==datanew1$department[i]) & (datanew1$grade==datanew1$grade[i]) & (datanew1$Date_Created >=datanew1$less6month[i])& (datanew1$Date_Created <= datanew1$Date_Created[i]) # count number of TRUE results (subtract 1 for row i) datanew1$count[i] <- sum(compare) } $\endgroup$ – anonymous Jun 26 '19 at 12:34
  • $\begingroup$ 2. The resultsId nos location grade Date_Created less6month count 20095BR 4 Netherlands 3 3/1/2016 9/1/2015 1 21096BR 2 Netherlands 3 3/15/2016 9/15/2015 2 21406BR 3 Netherlands 3 3/21/2016 9/21/2015 3 $\endgroup$ – anonymous Jun 26 '19 at 12:36
  • $\begingroup$ 4. I have already intended results in my first post. $\endgroup$ – anonymous Jun 26 '19 at 12:36
0
$\begingroup$
  1. For the first question the codes is datanew1$count <- 0 # initialise counter i <- 1 # for testing for (i in 1:nrow(datanew1)){ # loop through rows # compare all rows to row i compare <- (datanew1location==datanew1location[i]) & #(datanew1department==datanew1department[i]) & (datanew1grade==datanew1grade[i]) & (datanew1DateCreated>=datanew1less6month[i])& (datanew1DateCreated<=datanew1Date_Created[i]) # count number of TRUE results (subtract 1 for row i) datanew1$count[i] <- sum(compare) }
$\endgroup$
0
$\begingroup$
  1. The final output should look like enter image description here
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.