lapply - Using apply functions instead of for and branching statements in R -
i using r , stop using branching , statements take advantage of apply functions. being said, have list, x:
x <- c(5,12,19,26,2,9,16,23)
i corresponding list follows:
for in x if (i<=7) 1 else if (i<=14) 2 else if (i<=21) 3 else if (i<=28) 4 else 5
the final new list be: 1,2,3,4,1,2,3,4
how can 1 of apply statements? every time try , write 1 end scratching head hour , post question here.
thank you.
simply use cut
:
x <- c(5, 12, 21, 35) as.integer(cut(x, c(-inf, 7, 14, 21, 28, inf))) #[1] 1 2 3 5
Comments
Post a Comment