Re: [R] How to choose columns in data.frame by parts of columns' names?

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Wed 31 May 2006 - 22:01:31 EST

Use rowMeans:

rowMeans(iris[, regexpr("Sepal", colnames(iris)) > 0])

gives the means of each row of iris using only the columns whose names contain "Sepal".

On 5/31/06, Guo Wei-Wei <wwguocn@gmail.com> wrote:
> Gabor and Peter,
>
> Thank you. Both of you give me excellent ways. I have a further problem.
>
> How can I get the common parts of column names as column names in a
> new data.frame? For example, I combines data of XG1 and YG1 in
> data.old and get a new column in data.new named G1. Can It be done
> automaticlly?
>
> data.new[,"G1"] <- (data.old[,"XG1"] + data.old[,"YG1"])/2
>
>
> 2006/5/31, Gabor Grothendieck <ggrothendieck@gmail.com>:
> > This is not restricted to single matches:
> >
> > > colnames(iris)
> > [1] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width" "Species"
> > > regexpr("Sepal", colnames(iris)) > 0
> > [1] TRUE TRUE FALSE FALSE FALSE
> >
>
> ______________________________________________
> R-help@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
>



R-help@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 Received on Wed May 31 22:16:16 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Thu 01 Jun 2006 - 00:10:27 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.