[R] How to strip of a term in data.frame

ronggui ronggui.huang at gmail.com
Wed May 3 07:19:38 CEST 2006


2006/5/3, Guojun Zhu <shmilylemon at yahoo.com>:
> say I have 100 terms for data.frame a.  I want to do a
> linear regression of one term (say y) on other 33
> terms.  the only way to write this is "lm(y~.,
> data=a)". But there are some intermediate term on a
> and I need to take them off before I can run it.  I
> know you can use a[,-(3:4)], But can I specify it by
> name instead, I want to save everything in a script,
> something by name is much easier to read and easier to
> maintain later.

Here is one way:
suppose the variables you want to remove  are c("x","y","z"),then  you
can use match to get the posistion index.so the following code will
work.
a.new <- a[,-match(c("x","y","z"),names(a))]

> also, how to take of some rows from data.frame.  For
> example every row year==1995 or something like that?

_subset_ will do the job,for example
a.sub <- subset(a,year==1995)

> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>


--
»ÆÈÙ¹ó
Deparment of Sociology
Fudan University




More information about the R-help mailing list