dear all, I tried to use grep to match IDs in two dataframes grep(DF1$ID[i], DF2$ID) I wanted to use condition in a loop, but I have the problem to define what is in return if a match is not found. I used mode() and class() to compare between the attributes when a match is found and not found, but the mode and class are the same in both cases, numeric and integer, respectively. When a match is not found, grep returns "integer(0)". Is.numeric() and Is.integer() of the return gave TRUE but arithmetic calculation is not possible. For example, If a match is not found for i=12, grep(DF1$ID[12], DF2$ID) returns "integer(0)" and grep(DF1$ID[12], DF2$ID)+10 returns "numeric(0)" I will appreciate any suggestions. Thanks in advance. -- View this message in context: nabble.com/what-is-returned-if-a-match-is-not-found-using-grep-tp24914127p24914127.html Sent from the R help mailing list archive at Nabble.com.
Duncan Murdoch
2009-Aug-11 11:04 UTC
[R] what is returned if a match is not found using grep
Rnewbie wrote:> dear all, > > I tried to use grep to match IDs in two dataframes > > grep(DF1$ID[i], DF2$ID) > > I wanted to use condition in a loop, but I have the problem to define what > is in return if a match is not found. I used mode() and class() to compare > between the attributes when a match is found and not found, but the mode and > class are the same in both cases, numeric and integer, respectively. > > When a match is not found, grep returns "integer(0)". Is.numeric() and > Is.integer() of the return gave TRUE but arithmetic calculation is not > possible. >See the Value section of the ?grep man page. By default, R returns a vector of indices of entries in x that match the pattern. Since you have no matches, you get a vector of length 0. That is printed as integer(0). (This is also the function call that would create a vector of length 0.) One simple test for no results is length(grep(...)) == 0. Duncan Murdoch> For example, > If a match is not found for i=12, > > grep(DF1$ID[12], DF2$ID) returns "integer(0)" and > > grep(DF1$ID[12], DF2$ID)+10 returns "numeric(0)" > > I will appreciate any suggestions. Thanks in advance. > > >
Thank you very much!!! Duncan Murdoch-2 wrote:> > Rnewbie wrote: >> dear all, >> >> I tried to use grep to match IDs in two dataframes >> >> grep(DF1$ID[i], DF2$ID) >> >> I wanted to use condition in a loop, but I have the problem to define >> what >> is in return if a match is not found. I used mode() and class() to >> compare >> between the attributes when a match is found and not found, but the mode >> and >> class are the same in both cases, numeric and integer, respectively. >> >> When a match is not found, grep returns "integer(0)". Is.numeric() and >> Is.integer() of the return gave TRUE but arithmetic calculation is not >> possible. >> > See the Value section of the ?grep man page. By default, R returns a > vector of indices of entries in x that match the pattern. Since you > have no matches, you get a vector of length 0. That is printed as > integer(0). (This is also the function call that would create a vector > of length 0.) > > One simple test for no results is length(grep(...)) == 0. > > Duncan Murdoch >> For example, >> If a match is not found for i=12, >> >> grep(DF1$ID[12], DF2$ID) returns "integer(0)" and >> >> grep(DF1$ID[12], DF2$ID)+10 returns "numeric(0)" >> >> I will appreciate any suggestions. Thanks in advance. >> >> >> > > ______________________________________________ > R-help at r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >-- View this message in context: nabble.com/what-is-returned-if-a-match-is-not-found-using-grep-tp24914127p24915747.html Sent from the R help mailing list archive at Nabble.com.
sorry, I still have a question. What is the difference between an empty vector and a vector of length 0? When I assign x<-c() is.null(x) is TRUE x<-integer(0) is.null(x) is FALSE Duncan Murdoch-2 wrote:> > Rnewbie wrote: >> dear all, >> >> I tried to use grep to match IDs in two dataframes >> >> grep(DF1$ID[i], DF2$ID) >> >> I wanted to use condition in a loop, but I have the problem to define >> what >> is in return if a match is not found. I used mode() and class() to >> compare >> between the attributes when a match is found and not found, but the mode >> and >> class are the same in both cases, numeric and integer, respectively. >> >> When a match is not found, grep returns "integer(0)". Is.numeric() and >> Is.integer() of the return gave TRUE but arithmetic calculation is not >> possible. >> > See the Value section of the ?grep man page. By default, R returns a > vector of indices of entries in x that match the pattern. Since you > have no matches, you get a vector of length 0. That is printed as > integer(0). (This is also the function call that would create a vector > of length 0.) > > One simple test for no results is length(grep(...)) == 0. > > Duncan Murdoch >> For example, >> If a match is not found for i=12, >> >> grep(DF1$ID[12], DF2$ID) returns "integer(0)" and >> >> grep(DF1$ID[12], DF2$ID)+10 returns "numeric(0)" >> >> I will appreciate any suggestions. Thanks in advance. >> >> >> > > ______________________________________________ > R-help at r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >-- View this message in context: nabble.com/what-is-returned-if-a-match-is-not-found-using-grep-tp24914127p24916597.html Sent from the R help mailing list archive at Nabble.com.