Hi Here i have a dataframe called MyDat. MyDat<- data.frame(NAME = c("ANTONY001", "ARUN002", "AKBAR003", "JONATHAN004", "PETER005", "AVATAR006", "YULIJIE007", "RAM008", "DESILVA009"), COL_A = c(0, 0, 0, 1, 0, 1, 2, 3, 1), COL_B = c(0, 3, 0, 3, 3, 1, 0, 1, 2), COL_C = c(1, 2, 3, 1, 2, 3, 1, 2, 3), stringsAsFactors=FALSE) and here my requirement what is, i need to get the row number, where the NAME column matches with selection criteria. For eg:- If i give NAME = "ARUN", It should select row no: 2 where "ARUN002" comes. i tried with this way nRow<-which("ARUN"==MyDat[,1]) - But here, the row number will select only the NAME column value match exactly, otherwise it wont select. My requirment is, it should select the row number(s), where the searching word should match atleast. Exact match is not compulsory. - Thanks in Advance. Antony. -- View this message in context: http://r.789695.n4.nabble.com/Search-for-Matching-word-in-a-Dataframe-tp4669426.html Sent from the R help mailing list archive at Nabble.com.
Hello, ?grep > grep('ARUN', MyDat$NAME) [1] 2 Regards, Pascal On 13/06/13 16:08, R_Antony wrote:> Hi > > Here i have a dataframe called MyDat. > > MyDat<- data.frame(NAME = c("ANTONY001", "ARUN002", "AKBAR003", > "JONATHAN004", "PETER005", "AVATAR006", "YULIJIE007", "RAM008", > "DESILVA009"), > COL_A = c(0, 0, 0, 1, 0, 1, 2, 3, 1), > COL_B = c(0, 3, 0, 3, 3, 1, 0, 1, 2), > COL_C = c(1, 2, 3, 1, 2, 3, 1, 2, 3), stringsAsFactors=FALSE) > > and here my requirement what is, i need to get the row number, where the > NAME column matches with selection criteria. > > For eg:- If i give NAME = "ARUN", It should select row no: 2 where > "ARUN002" comes. > > i tried with this way > nRow<-which("ARUN"==MyDat[,1]) - But here, the row number will select only > the NAME column value match exactly, otherwise it wont select. > > My requirment is, it should select the row number(s), where the searching > word should match atleast. Exact match is not compulsory. > > > > - Thanks in Advance. > Antony. > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Search-for-Matching-word-in-a-Dataframe-tp4669426.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. >
Hi, You could use: ?grep grep("ARUN",MyDat[,1]) #[1] 2 #or library(stringr) ?which(!is.na(str_match(MyDat[,1],"ARUN"))) #[1] 2 ?vec1<-c(MyDat[,1],"ARUN003","Arun") ?which(!is.na(str_match(toupper(vec1),"ARUN"))) #[1]? 2 10 11 A.K. ----- Original Message ----- From: R_Antony <antony.akkara at ge.com> To: r-help at r-project.org Cc: Sent: Thursday, June 13, 2013 3:08 AM Subject: [R] Search for Matching word in a Dataframe Hi Here i have a dataframe called MyDat. MyDat<- data.frame(NAME = c("ANTONY001", "ARUN002", "AKBAR003", "JONATHAN004", "PETER005", "AVATAR006", "YULIJIE007", "RAM008", "DESILVA009"), COL_A = c(0, 0, 0, 1, 0, 1, 2, 3, 1), COL_B = c(0, 3, 0, 3, 3, 1, 0, 1, 2), COL_C = c(1, 2, 3, 1, 2, 3, 1, 2, 3), stringsAsFactors=FALSE) and here my requirement what is, i need to get the row number, where the NAME column matches with selection criteria. For eg:- If i give NAME = "ARUN", It should select row no: 2 where "ARUN002" comes. i tried with this way nRow<-which("ARUN"==MyDat[,1]) - But here, the row number will select only the NAME column value match exactly, otherwise it wont select. My requirment is, it should select the row number(s), where the searching word should match atleast. Exact match is not compulsory. - Thanks in Advance. Antony. -- View this message in context: http://r.789695.n4.nabble.com/Search-for-Matching-word-in-a-Dataframe-tp4669426.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.