Skip to content
Prev 10332 / 398503 Next

two easy questions...

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) ))