[R] set difference between two data frames
Hi, I have two data frames, say, x and y, where y is a subset of x. How can I find the set difference of these two data frames (i.e., x-y)? Thanks, -- Yasin Gocgun __ R-help@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.
Re: [R] set difference between two data frames
lapply() setdiff() by columns. Unless you have failed to tell us something, you almost certainly will not get a data frame (same number of rows/column) as your answer. -- Bert On Thu, Oct 31, 2013 at 12:58 PM, Yasin Gocgun entropy...@gmail.com wrote: Hi, I have two data frames, say, x and y, where y is a subset of x. How can I find the set difference of these two data frames (i.e., x-y)? Thanks, -- Yasin Gocgun __ R-help@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. -- Bert Gunter Genentech Nonclinical Biostatistics (650) 467-7374 __ R-help@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.
Re: [R] set difference between two data frames
Hello, Try the following. (I don't remember who wrote this function but I saw it in R-Help) setdiffDF - function(A, B){ f - function(A, B) A[!duplicated(rbind(B, A))[nrow(B) + 1:nrow(A)], ] df1 - f(A, B) df2 - f(B, A) rbind(df1, df2) } df1 - data.frame(A = 1:10, B = 11:20) df2 - data.frame(A = 1:5, B = 11:15) setdiffDF(df1, df2) Hope this helps, Rui Barradas Em 31-10-2013 19:58, Yasin Gocgun escreveu: Hi, I have two data frames, say, x and y, where y is a subset of x. How can I find the set difference of these two data frames (i.e., x-y)? Thanks, __ R-help@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.
Re: [R] set difference between two data frames
Thanks. Actually, I forgot to add that both have the same number of columns. On Thu, Oct 31, 2013 at 4:07 PM, Bert Gunter gunter.ber...@gene.com wrote: lapply() setdiff() by columns. Unless you have failed to tell us something, you almost certainly will not get a data frame (same number of rows/column) as your answer. -- Bert On Thu, Oct 31, 2013 at 12:58 PM, Yasin Gocgun entropy...@gmail.com wrote: Hi, I have two data frames, say, x and y, where y is a subset of x. How can I find the set difference of these two data frames (i.e., x-y)? Thanks, -- Yasin Gocgun __ R-help@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. -- Bert Gunter Genentech Nonclinical Biostatistics (650) 467-7374 -- Yasin Gocgun __ R-help@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.
Re: [R] set difference between two data frames
You could e.g. take the data.table package (every data.table is a data.frame) and make a join: dt.x - data.table(x) dt.y - data.table(y) merge.xy - x[y, nomatch = 0] diff.xy - x[!merge.xy] On 31 Oct 2013, at 21:41, Yasin Gocgun entropy...@gmail.com wrote: Thanks. Actually, I forgot to add that both have the same number of columns. On Thu, Oct 31, 2013 at 4:07 PM, Bert Gunter gunter.ber...@gene.com wrote: lapply() setdiff() by columns. Unless you have failed to tell us something, you almost certainly will not get a data frame (same number of rows/column) as your answer. -- Bert On Thu, Oct 31, 2013 at 12:58 PM, Yasin Gocgun entropy...@gmail.com wrote: Hi, I have two data frames, say, x and y, where y is a subset of x. How can I find the set difference of these two data frames (i.e., x-y)? Thanks, -- Yasin Gocgun __ R-help@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. -- Bert Gunter Genentech Nonclinical Biostatistics (650) 467-7374 -- Yasin Gocgun __ R-help@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. __ R-help@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.
Re: [R] set difference between two data frames
Also, you could try: library(sqldf) sqldf('SELECT * FROM df1 EXCEPT SELECT * FROM df2') A.K. On Thursday, October 31, 2013 4:28 PM, Rui Barradas ruipbarra...@sapo.pt wrote: Hello, Try the following. (I don't remember who wrote this function but I saw it in R-Help) setdiffDF - function(A, B){ f - function(A, B) A[!duplicated(rbind(B, A))[nrow(B) + 1:nrow(A)], ] df1 - f(A, B) df2 - f(B, A) rbind(df1, df2) } df1 - data.frame(A = 1:10, B = 11:20) df2 - data.frame(A = 1:5, B = 11:15) setdiffDF(df1, df2) Hope this helps, Rui Barradas Em 31-10-2013 19:58, Yasin Gocgun escreveu: Hi, I have two data frames, say, x and y, where y is a subset of x. How can I find the set difference of these two data frames (i.e., x-y)? Thanks, __ R-help@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. __ R-help@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.