0

I have a for loop that works on its own, but when I turn it into a function I do not get the output. The for loop populates a new column of a dataframe based on date ranges. I put in a data frame x and I want a new column with the new variable ,ay, populated.

Here is a data frame:

score <- c(86, 95, 48, 99)
date <- ymd(c('2013-07-12','2014-08-18', '2015-05-24', '2016-08-02'))
test <- data.frame(score,date)

Here is the for loop:

for (i in seq_along(x)) {
if (year(x$date[i]) == 2012 & month(x$date[i]) %in% c(8:12) | year(x$date[i]) == 2013 &   month(x$date[i]) %in% c(1:7)) {
x$ay[i] <- 1213
}
else if (year(x$date[i]) == 2013 & month(x$date[i]) %in% c(8:12) | year(x$date[i]) == 2014 & month(x$date[i]) %in% c(1:7)) {
x$ay[i] <- 1314
}
}

However when I try to turn this for loop into a function it does not populate the new variable. I am missing something fundamental here. Do I need some sort of return?

my_fun <- function(x) { for (i in seq_along(x)) {
      if (year(x$date[i]) == 2012 & month(x$date[i]) %in% c(8:12) | year(x$date[i]) == 2013 &   month(x$date[i]) %in% c(1:7)) {
x$ay[i] <- 1213
}
else if (year(x$date[i]) == 2013 & month(x$date[i]) %in% c(8:12) | year(x$date[i]) == 2014 & month(x$date[i]) %in% c(1:7)) {
x$ay[i] <- 1314
}
 }
 return(x)
 } 
Mike
  • 31
  • 1
  • 7
  • 1
    The `x` you are passing into the function environment does not contain `neuro_shelf`. – Konrad Jan 16 '17 at 17:42
  • oops, neuro_shelf was not supposed to be there. The issue i am having is still the same – Mike Jan 16 '17 at 18:03
  • Your function should `return(test)` at the end. (And it should probably either take `test` as an argument as well or initialize it to make sure it is created from scratch inside the function. As-is, your `test` vector gets coerced to a list the first time you do `test$ay[i] <-`, which seems pretty poor.) – Gregor Thomas Jan 16 '17 at 18:05
  • i just edited my code, when i generalized it i made a mistake test should be the data frame that is the function input – Mike Jan 16 '17 at 18:10
  • You use `x` as the only input to your function. This means that inside your function, you should only use `x`. No `test` inside the function unless you also have an input argument named `test`. And then the last line of your function should be `return(x)`. – Gregor Thomas Jan 16 '17 at 18:15
  • The ultimate question is how can i make my function populate the variable in the data frame. There is something missing when i turn the for loop into a function. – Mike Jan 16 '17 at 18:16
  • Please listen and try out the comments. We are trying to help you do that. Right now your function is working on two data frames, when it needs to be working on one. And it doesn't `return()` anything, which means that all of the work it does is local only to the function - nothing is output. If you make the changes I have recommended, your problem will be solved. – Gregor Thomas Jan 16 '17 at 18:24
  • I updated the code to reflect the suggestions....it think. The function will print the values but to save it in the df i have to assign test$ay <-my_fun(x). How can i incorporate this into the function itself? I really appreciate the help. This has been driving me crazy – Mike Jan 16 '17 at 18:54
  • In that case, it is indeed a duplicate as user2100 suggested. But you are writing an R function in a very unlike way in that case. Probably every other R function you've ever used has the property "to save the result I have to assign it". – Gregor Thomas Jan 16 '17 at 19:18

0 Answers0