[R] Filtering a dataset's columns by another dataset's column names

Daniel Malter daniel at umd.edu
Fri Feb 27 18:47:45 CET 2009


Hi Josh B,

this looks like homework to me. Please obey the posting rules. I.e., provide
self-contained code/examples and show what the point is at which you are
stuck. 

To solve your problem, you need the "which" and the "names" function as well
as the %in%  operator. It is then easy to rbind the two datasets once you
have figured out what the common column names are. Please try on your own
first and report back if and where you are stuck along with the
self-contained code. If this is indeed homework, please ask your professor
or teacher.

Example for two simulated datasets:

x=rnorm(30)
dim(x)=c(5,6)
x=data.frame(x)
names(x)=c("a","b","c","x","y","z")

y=rnorm(30)
dim(y)=c(5,6)
y=data.frame(y)
names(y)=c("a","b","d","v","w","x")

Daniel


-------------------------
cuncta stricte discussurus
-------------------------

-----Ursprüngliche Nachricht-----
Von: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Im
Auftrag von Josh B
Gesendet: Friday, February 27, 2009 12:28 PM
An: R Help
Betreff: [R] Filtering a dataset's columns by another dataset's column names

Hello all,

I hope some of you can come to my rescue, yet again.

I have two genetic datasets, and I want one of the datasets to have only the
columns that are in common with the other dataset. 
Here is a toy example (my real datasets have hundreds of columns):

Dataset 1:

Individual    SNP1    SNP2    SNP3    SNP4    SNP5
1    A    G    T    C    A
2    T    C    A    G    T
3    A    C    T    C    A

Dataset 2:

Individual    SNP1    SNP3    SNP5    SNP6    SNP7
4    A    T    T    G    C
5    T    A    A    G    G
6    A    A    T    C    G

I want Dataset1 to have only columns that are also represented in Dataset 2,
i.e., I want to generate a new Dataset 3 that looks like this:

Individual    SNP1    SNP3    SNP5
1    A    T    A
2    T    A    T
3    A    T    A

Does anyone know how I could do this? Keep in mind that this is not a simple
merge, as in the "merge" function.

Thanks very much for your help everyone.
Josh B.



      
	[[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list