I am trying to write a function in R but I'm struggling with a sequence problem:
vincent <- function(v,n, val_min){
# v = vector to vincentize n = number of bin +1
mean_vct <- array(0, n) # n = nb de bins; crée un vecteur de 5 zéros si n = 5
vsort <- sort(v)
vsort <- sort(subset(vsort, vsort>= val_min))
for (j in seq(1,n) ){
mean_vct[j] <- (val_inf(j,vsort,n) + val_inter(j,vsort,n) + val_sup(j,vsort,n))
mean_vct[j] <- mean_vct[j]/(length(vsort)/(n))
}
return (mean_vct)
}
When applying this code with a print of the sequence, I get :
1 2 3 4 5 6 7 8 9 0
instead of 0 1 2 3 4 5 6 7 8 9
And I need this sequence to begin with 0 because I'm converting a code from Python to R.
Thanks
Edit : An example for applying this function :
RT <- 1:100
vincent(RT, 10, 0)
There are several issues with your code.
vincent <- function(v,n){
# v = vector to vincentize n = number of bin
mean_vct <- array(0, n)
vsort <- sort(v) #Tri dans l'ordre les TR
for (j in seq(0,9)){ #pour chaque bin
mean_vct[j] <- j
}
return (mean_vct)
}
for (j in seq(0, 9))
0:n
creates a sequence just like seq(0,9) (you are probably used to the range method in python)mean_vct[j] <- j
This will not work because R starts indexing at one.
You have several options:
Since I started writing this answer you changed your code, but this should still explain your problem.