[R] How to choose columns in data.frame by parts of columns' names?
Gabor Grothendieck
ggrothendieck at gmail.com
Wed May 31 05:13:53 CEST 2006
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
On 5/30/06, Guo Wei-Wei <wwguocn at gmail.com> wrote:
> Thank you. I made a mistake in my previous email. What I mean is:
>
> data.new[,"G1"] <- (data.old[,"XG1"] + data.old[,"YG1"])/2
>
> data.old[, regexpr("G1", colnames(data.old)) > 0]
> is a nice way, but there are about 100 "X*"s and "Y*"s. Can I do some
> comparision on all those column names and get columns with similar
> parts?
>
> 2006/5/31, Gabor Grothendieck <ggrothendieck at gmail.com>:
> > On 5/30/06, Guo Wei-Wei <wwguocn at gmail.com> wrote:
> > > Dear all,
> > >
> > > I have a data.frame which has names as following.
> > > [1] "XG1" "YG1" "XEST" "YEST"
> > > [2] "XNOEMP1" "XNOEMP2" "YNOEMP1" "YNOEMP2"
> > > [3] "XBUS10" "XBUS10A" "XBUS10B" "XBUS10C"
> > > [4] "YBUS10" "YBUS10A" "YBUS10B" "YBUS10C"
> > > [5] "XOWNBUS" "XSELFEST" "YOWNBUS" "YSELFEST"
> > >
> > > Those columns have names beginning with "X" or "Y". Each "X" is paired
> > > by a "Y", e.g. "XG1" and "YG1", but they are not in the order of "X Y
> > > X Y ...". I want to combine "X*" and "Y*" like this:
> > >
> > > data.new[,"G1"] <- (data.old[,"XG1"] + endata.use[,"YG1"])/2
> > >
> > > How to choose columns by parts of names? For example, I can pick out
> > > XG1 and YG1 because they have the common part "G1".
> > >
> >
> >
> > This gives all columns whose column name contains G1:
> >
> > data.old[, regexpr("G1", colnames(data.old)) > 0]
> >
>
> ______________________________________________
> 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
>
More information about the R-help
mailing list