As long as == is an appropriate test for your data, why not just use R's innate ability to handle matrices/data frames?
> x1 <- matrix(1:20, ncol=4) > x2 <- ifelse(x1 > 18, 22, x1) > x1 [,1] [,2] [,3] [,4] [1,] 1 6 11 16 [2,] 2 7 12 17 [3,] 3 8 13 18 [4,] 4 9 14 19 [5,] 5 10 15 20 > x2 [,1] [,2] [,3] [,4] [1,] 1 6 11 16 [2,] 2 7 12 17 [3,] 3 8 13 18 [4,] 4 9 14 22 [5,] 5 10 15 22 > x1 == x2 [,1] [,2] [,3] [,4] [1,] TRUE TRUE TRUE TRUE [2,] TRUE TRUE TRUE TRUE [3,] TRUE TRUE TRUE TRUE [4,] TRUE TRUE TRUE FALSE [5,] TRUE TRUE TRUE FALSE On Wed, Mar 21, 2012 at 8:48 AM, HJ YAN <yhj...@googlemail.com> wrote: > Dear R-user, > > I'm trying to compare two sets of results and wanted to find out which > element in the two data frame/matrix are different. > > I wrote the following function and it works ok, and gives me a long list of > "good" as outcomes. > > > CHECK<- > function (x = "file1", y = "file2") > { > for (i in 1:nrow(x)) { > for (j in 1:ncol(x)) { > if (x[i, j] == y[i, j]) { > print("good") > } > else { > print("check") > } > } > } > } > > > However, as the two datasets I was comparing are large (400*100 roughly), > so I would like to create a matrix to identify which ones are not same in > the two dataframes. > > So I added 'CHECK_XY' in my code but when I run it, I got 'Error in > CHECK_XY[i, j] = c("good") : subscript out of bounds'. > > Could anyone help please?? > > CHECK_1<- > function (x = "file1", y = "file2") > { > NROW <- nrow(x) > NCOL <- ncol(x) > CHECK_XY <- as.matrix(NA, NROW, NCOL) > for (i in 1:nrow(x)) { > for (j in 1:ncol(x)) { > if (x[i, j] == y[i, j]) { > CHECK_XY[i, j] = c("good") > } > else { > CHECK_XY[i, j] = c("check") > } > } > } > print(CHECK_XY) > } > > Thanks! > HJ -- Sarah Goslee http://www.functionaldiversity.org ______________________________________________ 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.