Skip to content
Prev 314146 / 398506 Next

How to do it through 1 step?

Hi,
How about this?

set.seed(5)
dat<-data.frame(x=sample(1:20,6,replace=TRUE),a=sample(20:40,6,replace=TRUE))
?dat1<-within(dat,{f=a/median(a);x_new=x*f})
?dat1
#?? x? a???? x_new???????? f
#1? 5 31? 5.081967 1.0163934
#2 14 36 16.524590 1.1803279
#3 19 40 24.918033 1.3114754
#4? 6 22? 4.327869 0.7213115
#5? 3 25? 2.459016 0.8196721
#6 15 30 14.754098 0.9836066

#or
?res<-do.call(data.frame,lapply(lapply(1,function(y) dat),function(y) {y$f<-y$a/median(y$a);y$x_new<-y$f*y$x;y}))
A.K.