0

I wrote a function to do some calculations with frequencies (of values in the dataframe) and put them in a new column. Some calculations cannot be perform if the value is NA, so I changed them to 0. Subsequently, because a value cannot be divided by 0, I want the outcome to be the first frequency.

Now, the function stops after the IF-statement inside the function. Can you guys see what is wrong with it, without the data?

B <- function(frame1, frame2, col){
    Bframe <- merge(frame1, frame2, by = column, all = TRUE)
    Bframe$freq.x[is.na(Bframe$freq.x)] <- 0 
    Bframe$freq.y[is.na(Bframe$freq.y)] <- 0 
    if (Bframe$freq.y == 0) { 
      Bframe$result <- Bframe$freq.x
    }
    Bframe$result <- Bframe["freq.x"] - Bframe["freq.y"]
    Bframe$percentage <- (Bframe["result"]/Bframe["freq.y"])*100
    Bframe$percABS <- abs(Bframe$percentage)
    return(Bframe)
}

So, the function works up to Bframe$result. Is something in the structure wrong?

I also got an error: Warning message: In if (Bframe$freq.y == 0) {: the condition has length > 1 and only the first element will used.

If you do need some input data, please tell me.

MLnewby
  • 139
  • 1
  • 2
  • 8
  • Also, using the syntax highlighting as a guide, you can see that you are missing a `"` in `Bframe["freq.y]`. – Gregor Thomas Jun 27 '18 at 14:11
  • And I think the more canonical duplicate is [vectorized `if` statement in R](https://stackoverflow.com/q/4042413/903061). MLNewby - the problem is that `if` isn't vectorized. It only pays attention to the first element and ignores the rest. You want `ifelse` as a vectorized version of `if`. – Gregor Thomas Jun 27 '18 at 14:13
  • However, in this case, I would urge OP to not use an `if` (or `ifelse`) at all, and just use `Bframe$result[Bframe$freq.y == 0] <- Bframe$freq.x[Bframe$freq.y == 0]` – Gregor Thomas Jun 27 '18 at 14:15
  • @Gregor, excuse me for asking a duplicate question. However, your last suggestion won't work for me. The outcome is still `Inf`, and therefore, calculations are still being done with zero. I am not the smartest R-user, so I might have another issue in my code what doesn't do my calculations as I wish. – MLnewby Jun 27 '18 at 16:01
  • Well, I guess that makes sense. You divide by `Bframe$freq.y` regardless of whether or not it is 0 - updating the `result` column beforehand doesn't change that. But I find it hard to understand what you want to do without data. What is "the first frequency"? I'd be happy to re-open if you would add a few rows of sample data to show the problem, (5 or 6 rows with 1 or 2 missing/0 values is plenty!), along with the desired output for that input. – Gregor Thomas Jun 27 '18 at 16:08
  • Thank you @Gregor for your efforts. However, I already found the answer! – MLnewby Jun 27 '18 at 19:18

0 Answers0