赞
踩
1、原始方式
- mydata <- data.frame(x1=c(1,2,3,4),x2=c(3,4,5,6))
-
- mydata$sum <-mydata$x1 + mydata$x2
-
- mydata$mean <- mydata$sum / 2
2、使用attach
- mydata <- data.frame(x1=c(1,2,3,4),x2=c(3,4,5,6))
-
- attach(mydata)
- mydata$sum <- x1 + x2
- mydata$mean <- sum
- detach(mydata)
- mydata
3、使用within
- mydata <- data.frame(x1=c(1,2,3,4),x2=c(3,4,5,6))
-
- mydata <- within(mydata,{
-
- sum <- x1+x2
-
- mean <- sum/2
-
- })
within 与 with 类似,区别是within允许你修改数据框。
4、使用transform
- mydata <- data.frame(x1=c(1,2,3,4),x2=c(3,4,5,6))
-
- mydata <- transform(mydata, sumx = x1 + x2, meanx = (x1 + x2)/2 )
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。