lundi 17 août 2020

how to write a conditional function

I am trying to write a function to calculate h-point. the function is defined over a rank frequency data frame. so far, I`ve managed to sort and rank the frequencies using quanteda package.

DATA <-textstat_frequency(dfm, groups = dfm@docvars$docname_, ties_method = "first")

and the formula for h-point is :

if {there is an r = f(r), h-point = r } else { h-point = f(i)j-f(j)i / i-j+f(i)-f(j) } where f(i) and f(j) are corresponding frequencies for ith and jth ranks and i and j are adjacent ranks that i<f(i) and j>f(j).

NOW, i have tried the following codes :

fr <-function(x){d <-DATA$frequency[x]
return(d)}

for (i in 1:length(DATA$rank)) {
j <- i+1
if (i==fr(i))
return(i)
else(i<fr(i) && j>fr(j)) {
s <-fr(i)*j-fr(j)*i/j-i+fr(i)-fr(j)
return(s)
}

and they do not work. can you please tell me what I`m doing wrong here?

Aucun commentaire:

Enregistrer un commentaire