R : Problems in creating a seq(0,9) in a loop -
i trying write function in r i'm struggling sequence problem:
vincent <- function(v,n, val_min){ # v = vector 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)) (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 code print of sequence, : 1 2 3 4 5 6 7 8 9 0 instead of 0 1 2 3 4 5 6 7 8 9 , need sequence begin 0 because i'm converting code python r.
thanks
edit : example applying function :
rt <- 1:100 vincent(rt, 10, 0)
there several issues code.
vincent <- function(v,n){ # v = vector vincentize n = number of bin mean_vct <- array(0, n) vsort <- sort(v) #tri dans l'ordre les tr (j in seq(0,9)){ #pour chaque bin mean_vct[j] <- j } return (mean_vct) } for (j in seq(0, 9))
- you should go n , not until 9, using example crash loop
- there easier ways go 0 n ->
0:ncreates sequence seq(0,9) (you used range method in python)
mean_vct[j] <- j
this not work because r starts indexing @ one.
you have several options:
- loop 1:n instead of 0:(n-1) , incase need j computations use (j-1) in computations
- loop 0:(n-1) , use mean_vct[j+1]
since started writing answer changed code, should still explain problem.
Comments
Post a Comment