nuwanma
nuwanma

Reputation: 81

Use loop function in r to calculate constant value for each day?

Hi have velocity (val) and depth (dep) data. So, using loop function in r I want to find a constant called 'K' (K=ODobbins(val,dep), 'ODobbins' is a function in StreamMetabolism package) for endless dates as data receive

structure(list(date = c("12/01/2019", "13/01/2019", "14/01/2019", 
"15/01/2019", "16/01/2019", "17/01/2019"), vel = c(0.6, 0.5, 
0.6, 0.2, 0.8, 0.1), dep = c(0.42, 0.21, 0.35, 0.24, 0.65, 0.12
)), class = "data.frame", row.names = c(NA, -6L))

Can anyone help with this?

Upvotes: 1

Views: 58

Answers (1)

akrun
akrun

Reputation: 887048

The 'ODobbins' is a vectorized function as

library(StreamMetabolism)
ODobbins
function (vel, dep) 
{
    (3.93 * (vel^0.5))/(dep^1.5)
}

Here, the / and ^ are vectorized, so we can pass the apply the function with arguments as the columns

with(df, ODobbins(vel, dep))
#[1] 11.183925 28.876770 14.701651 14.948261  6.707605 29.896523

Upvotes: 1

Related Questions