Skip to content

two easy questions...

2 messages · stefano iacus, Peter Dalgaard

#
Hi all.

1) If I have a dataframe with variable names as follow: PC1 PC2 ... PCn and I want to pass only some of them to a function, e.g.
glm(resp~from PC1 to PC10, PC15, etc.,...)

is there a faster way than simply writing each variable name in the formula?

2) Again, I have a dataframe, say ali.df, with tha following variables: ali1, ali2, ...ali78
I want to sum, for example, ali1+al2+ali7+f rom ali10 to ali20 etc., then from ali4 to ali8, etc. and then put everything in a new dataframe

I'd do somthing like:
 
new.df <- cbind(apply(ali.df[c(1:2,7,10:20)],1,sum), apply(ali.df[4:8],1,sum))   and so on.
Is it the right and only way?


Thanks in advance (and forgive my orrible english...)

Stefano

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Stefano <stecalza at tiscalinet.it> writes:
There are some tricks involving the "." term and
subset(...,select=...)

E.g.

data(airquality)
names(airquality)
lm(Ozone~.,data=subset(airquality,select=Ozone:Month))
lm(Ozone~.,data=subset(airquality,select=c(Ozone:Wind,Month)))
lm(Ozone~.-Temp,data=subset(airquality,select=Ozone:Month))
You can't really do all that much better. You could use the subset
mechanism rather than ali.df[c(....)] which isn't shorter, but does
help you to keep track of variable names (which might have more
meaningful names in other cases!). 

You probably want data.frame() rather than cbind() though or new.df is
a matrix, not a data frame....

If it is a sum in each case, you could also try stuff like

as.data.frame(lapply(list(n1=c(1:2,7,10:20), 
                          n2=4:8),
                     function(l) apply(ali.df[l],1,sum) ))