Problem with applying function to a dataframe

Probably basic problem but I have no idea what is happening... When I apply a function to the whole data frame I get a different value than when applying to a specific row...

# I apply a function to variables within dataframe 'data'

> HEX <- data$HEX.rain_pos*cos(asin(min(pmax(data$FAM.norm,0)/data$HEX.rain_pos, pi/4)))

# I check row 1499657:
> HEX[1499657]
[1] 1088.853

# I apply the same function to row 1499657 of the dataframe. Now I get what I expect... 
>data$HEX.rain_pos[1499657]*cos(asin(min(pmax(data$FAM.norm[1499657],0)/data$HEX.rain_pos[1499657], pi/4)))
[1] 673.9898

What are the 1499657th values of data$HEX.rain_pos and data$FAM.norm?

Anyway you can turn this error into a reproducible example?

Solved, and can be closed. I didn't know I need to vectorize the trigonometric functions (or use 'rowwise()'). R seems to be a little inconsistent in that some operations are already vectorized and others not.

I think the trig-functions are vectorized. Likely the visible difference is from min() that is computing one global min and not one per row. You could wrap up your whole expression into a function and apply Vectorize() to good effect.

1 Like