r - How to apply functions to a subset of the data in a vectorized manner -
this question has reply here:
r grouping functions: sapply vs. lapply vs. apply. vs. tapply vs. vs. aggregate 7 answersquestion:
how apply functions subset of info in vectorized manner.
example:
for info frame below:
x=c(1,2,1,2,1,2) y=c(3,4,5,4,3,2) df=data.frame(x,y)
i apply function (i.e. min()) y values each of x value, , collect in vector.
basically, have vectorized version of this:
nb = max(x); v = rep(0.0, nb) for(i in 1:nb){ v = df [ x == i, ]$y; v[i] <- min(v); } # here: # v[1] = min( df$y x=1) # v[2] = min( df$y x=2)
the function tapply
designed such problems:
with(df,tapply(y,x,fun=min)) #1 2 #3 2
if want add together results info frame, can utilize function ave
:
df$group.min <- with(df,ave(y,x,fun=min)) # x y group.min # 1 1 3 3 # 2 2 4 2 # 3 1 5 3 # 4 2 4 2 # 5 1 3 3 # 6 2 2 2
r
No comments:
Post a Comment