Re: [R] How to choose columns in data.frame by parts of columns' names?
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 <[EMAIL PROTECTED]> 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 <[EMAIL PROTECTED]>: > > 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
Re: [R] How to choose columns in data.frame by parts of columns' names?
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 <[EMAIL PROTECTED]>: > 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
Re: [R] How to choose columns in data.frame by parts of columns' names?
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 <[EMAIL PROTECTED]> 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 <[EMAIL PROTECTED]>: > > On 5/30/06, Guo Wei-Wei <[EMAIL PROTECTED]> 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@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
Re: [R] How to choose columns in data.frame by parts of columns' names?
Peter, Thank you, I made a mistake in my previous email. What I mean is: data.new[,"G1"] <- (data.old[,"XG1"] + data.old[,"YG1"])/2 Does your way have effects on data? or only have effects on those column names? I tried on my data and get a list of numbers. Can I rearrange the order of columns of data.frame by your way? 2006/5/31, Peter Alspach <[EMAIL PROTECTED]>: > > Wei-wei > > yourNames > [1] "XG1" "YG1" "XEST" "YEST" "XNOEMP1" "XNOEMP2" > [7] "YNOEMP1" "YNOEMP2" "XBUS10" "XBUS10A" "XBUS10B" "XBUS10C" > [13] "YBUS10" "YBUS10A" "YBUS10B" "YBUS10C" "XOWNBUS" "XSELFEST" > [19] "YOWNBUS" "YSELFEST" > yourNames[order(substring(yourNames,2), substring(yourNames, 1,1))] > [1] "XBUS10" "YBUS10" "XBUS10A" "YBUS10A" "XBUS10B" "YBUS10B" > [7] "XBUS10C" "YBUS10C" "XEST" "YEST" "XG1" "YG1" > [13] "XNOEMP1" "YNOEMP1" "XNOEMP2" "YNOEMP2" "XOWNBUS" "YOWNBUS" > [19] "XSELFEST" "YSELFEST" > > gives an idea of what I mean ... > > Peter Alspach > > > > __ > > The contents of this e-mail are privileged and/or confidential to the > named recipient and are not to be used by any other person and/or > organisation. If you have received this e-mail in error, please notify > the sender and delete all material pertaining to this e-mail. > __ > __ 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
Re: [R] How to choose columns in data.frame by parts of columns' names?
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 <[EMAIL PROTECTED]>: > On 5/30/06, Guo Wei-Wei <[EMAIL PROTECTED]> 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@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
Re: [R] How to choose columns in data.frame by parts of columns' names?
Wei-wei > 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". Not entirely sure what you mean but one approach might be to re-order the columns so that they are in order. yourNames [1] "XG1" "YG1" "XEST" "YEST" "XNOEMP1" "XNOEMP2" [7] "YNOEMP1" "YNOEMP2" "XBUS10" "XBUS10A" "XBUS10B" "XBUS10C" [13] "YBUS10" "YBUS10A" "YBUS10B" "YBUS10C" "XOWNBUS" "XSELFEST" [19] "YOWNBUS" "YSELFEST" yourNames[order(substring(yourNames,2), substring(yourNames, 1,1))] [1] "XBUS10" "YBUS10" "XBUS10A" "YBUS10A" "XBUS10B" "YBUS10B" [7] "XBUS10C" "YBUS10C" "XEST" "YEST" "XG1" "YG1" [13] "XNOEMP1" "YNOEMP1" "XNOEMP2" "YNOEMP2" "XOWNBUS" "YOWNBUS" [19] "XSELFEST" "YSELFEST" gives an idea of what I mean ... Peter Alspach __ The contents of this e-mail are privileged and/or confidenti...{{dropped}} __ 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
Re: [R] How to choose columns in data.frame by parts of columns' names?
On 5/30/06, Guo Wei-Wei <[EMAIL PROTECTED]> 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@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] How to choose columns in data.frame by parts of columns' names?
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". Thank you. Wei-Wei __ 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