0

I have functions and would like to save from the IF function the variable "sick [i]" to take advantage of it

for(i in 1:licznik){
  print_func <- function(a, b)
  {
    if(a > b)
    {

      print('wspolczynnik jest wiekszy' )

      print(sick[i])

    } 

  }

  print_func(a[i], b[i])
}

How to do it ?

Nerson48
  • 1
  • 6
  • 1
    What exactly are you asking here? Where do you want to "save" it? When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Apr 25 '18 at 16:21
  • in order to check if I wrote the function correctly, I took the variable "sick" to print. I would like the result that meets the assumption (a> b) i.e. sick [i], to write in the new variable – Nerson48 Apr 25 '18 at 16:26
  • Independent of what you are trying to achieve, define the `print_function` outside of the for loop. Guessing what you want to do, use `return` to let the function actually *return* a value which you can use afterwards. – apitsch Apr 25 '18 at 16:27

1 Answers1

1

Try this from R for Data Science.

   out <- vector("list", length(licznik))
    for (i in seq_along(licznik)) {
      if(a[[i]] > b[[i]]) {
          print('wspolczynnik jest wiekszy')
          print(sick[[i]])
          out[[i]] <- sick[[i]]
      }
    }
    str(unlist(out))
bscout11
  • 66
  • 8
  • result: chr [1:5] NA NA NA "11" "41" Error in as.igraph.vs(graph, v) : Invalid vertex names how to remove "NA" from the vector ? – Nerson48 Apr 25 '18 at 18:23
  • maybe add an if statement before hand: `if(!is.na(sick[[i]]) out[[i]] <- sick[[i]]` – bscout11 May 02 '18 at 16:45