[R] How to do it through 1 step?

arun smartpink111 at yahoo.com
Mon Dec 24 10:01:03 CET 2012


HI,

Not sure if this works for you.
set.seed(5)
dat<-data.frame(x=sample(1:20,6,replace=TRUE),a=sample(20:40,6,replace=TRUE))
 dat1<-transform(dat,f=a/median(a),x_new=x*(a/median(a)))
 head(dat1,2)
#   x  a        f     x_new
#1  5 31 1.016393  5.081967
#2 14 36 1.180328 16.524590
A.K.




----- Original Message -----
From: meng <laomeng_3 at 163.com>
To: r-help at r-project.org
Cc: 
Sent: Monday, December 24, 2012 12:55 AM
Subject: [R] How to do it through 1 step?

A data set(dat),has 2 variables: x and a, and 100 rows.

I wanna add 2 variables,and call the new data set dat1:
var1:f = a/median(a)
var2:x_new = x*f

My solution:
dat1<-transform(dat,f = a/median(a),x_new = x*f)
But gets error reply which says that "f" is not exits since dat has no variables called "f".

So I have to do through 2 steps:
dat0<-transform(dat,f=a/median(a))
dat1<-transform(dat0,x_new=x*f)

How to do it through 1 step?

Many thanks!


    [[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.





More information about the R-help mailing list