I understand this is an easy question, and have been playing around with grep
and the match function, but was hoping for a little incite:
I have one .csv with the following data:
names values
A 1
B 2
C 3
D 4
The second .csv is:
names
A
C
I am hoping to match all of the rows that appear in the second .csv, making
a new file that would look like this:
names values
A 1
C 3
Here is what I have so far:
my.data <- read.csv("rows.csv",sep=",")
my.selection <- read.csv("select.csv",sep=",")
matched <- match(my.data[,1], my.selection[,1])
my.data <- my.data[matched]
write.table(as.matrix(my.data), "select_RESULTS.txt")
Unfortunately, this is throwing errors in row numbers...
--
View this message in context:
http://www.nabble.com/Matching-tp21130173p21130173.html
Sent from the R help mailing list archive at Nabble.com.
Try this:> Lines1 <- "names,values+ A,1 + B,2 + C,3 + D,4"> > Lines2 <- "names+ A + C"> > DF1 <- read.csv(textConnection(Lines1)) > DF2 <- read.csv(textConnection(Lines2)) > merge(DF1, DF2)names values 1 A 1 2 C 3 On Mon, Dec 22, 2008 at 11:03 AM, vpas <vic.pascow at gmail.com> wrote:> > I understand this is an easy question, and have been playing around with grep > and the match function, but was hoping for a little incite: > > I have one .csv with the following data: > > names values > A 1 > B 2 > C 3 > D 4 > > > The second .csv is: > > names > A > C > > > I am hoping to match all of the rows that appear in the second .csv, making > a new file that would look like this: > > names values > A 1 > C 3 > > > Here is what I have so far: > > my.data <- read.csv("rows.csv",sep=",") > my.selection <- read.csv("select.csv",sep=",") > matched <- match(my.data[,1], my.selection[,1]) > my.data <- my.data[matched] > write.table(as.matrix(my.data), "select_RESULTS.txt") > > Unfortunately, this is throwing errors in row numbers... > > -- > View this message in context: http://www.nabble.com/Matching-tp21130173p21130173.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >
You don't need grep for this. Use the merge() function and make sure the arguments all.x and all.y are considered depending on whether this is a left or right merge.> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of vpas > Sent: Monday, December 22, 2008 11:03 AM > To: r-help at r-project.org > Subject: [R] Matching > > > I understand this is an easy question, and have been playing > around with grep and the match function, but was hoping for a > little incite: > > I have one .csv with the following data: > > names values > A 1 > B 2 > C 3 > D 4 > > > The second .csv is: > > names > A > C > > > I am hoping to match all of the rows that appear in the > second .csv, making a new file that would look like this: > > names values > A 1 > C 3 > > > Here is what I have so far: > > my.data <- read.csv("rows.csv",sep=",") > my.selection <- read.csv("select.csv",sep=",") matched <- > match(my.data[,1], my.selection[,1]) my.data <- > my.data[matched] write.table(as.matrix(my.data), "select_RESULTS.txt") > > Unfortunately, this is throwing errors in row numbers... > > -- > View this message in context: > http://www.nabble.com/Matching-tp21130173p21130173.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >