Jim is generous enough that he might do this, but such assistance is not sustainable. Fortunately, you can type a ? in front of the name of a function and read about what goes in and what comes out. You can also type expressions like x[2] or which(matches) right before you execute the line of code, and you can use debug(findMatches) to cause R to let you step through the function one line at a time. These are all skills you should start developing soon, because in the long run they will teach you more than you can learn by asking questions here. -- Sent from my phone. Please excuse my brevity.
On May 12, 2017 11:08:50 AM PDT, abo dalash <abo_d...@hotmail.com> wrote: >Dear Jim.., > > >Many thanks for your answer. > > >As I'm a new R user, could you please provide a short explanation > >about what each line of the following does ? > > >findMatches<-function(x,sep=",") { > matchval<-NA > x1bits<-unlist(strsplit(x[1],sep)) > x2bits<-unlist(strsplit(x[2],sep)) > matches<-x1bits %in% x2bits > if(any(matches)) matchval<-x1bits[which(matches)] > return(matchval) >} >x444$matched_items<-apply(x444,1,findMatches) > > >I would like to understand so I can apply the same for any further >analysis > >I may need in the future. > > >Regards > > > >________________________________ >From: Jim Lemon <drjimle...@gmail.com> >Sent: 12 May 2017 04:14 AM >To: abo dalash >Cc: r-help@R-project.org >Subject: Re: [R] Matched Items in rows + issue with writing a table > >Hi abo, >I think you want to split your strings and do your matching like this: > >x444<-read.table(text="w r > cyp3,cyp7 cyp2,cyp1,cyp3 > cyp2 cyp2 > c1,c3,c6 c6,c8,c5", > header=TRUE,stringsAsFactors=FALSE) >findMatches<-function(x,sep=",") { > matchval<-NA > x1bits<-unlist(strsplit(x[1],sep)) > x2bits<-unlist(strsplit(x[2],sep)) > matches<-x1bits %in% x2bits > if(any(matches)) matchval<-x1bits[which(matches)] > return(matchval) >} >x444$matched_items<-apply(x444,1,findMatches) > >Note that this will only work with character values, _not_ factors. > >Jim > >On Fri, May 12, 2017 at 9:16 AM, abo dalash <abo_d...@hotmail.com> >wrote: >> Hi All .., >> >> >> I have a table called "x444" and I would like to create a new column >contains the matched items in each row between column w & r . I used >match()function as below but this does not return the results I want >because of 2 issues. The 1st one is that this gives the row number of >shared items while I want to see the item itself (e.g. in the table >below, I want to see cyp2 instead of the row number 2). The 2nd issue >is that I need to know matched items considering every item in the row >instead of the entire row. For example, the item cyp3 is a matched item >in the first row between columns w & r. The same applies for c6 in row >3. These don't appear in the results below. >> >> >> >>>x444 >> w r >> 1 cyp3,cyp7 cyp2, cyp1,cyp3 >> 2 cyp2 cyp2 >> 3 c1,c3,c6 c6,c8,c5 >> >> >>> r = c(match(x444$w,X444$r)) >>> r >> [1] NA 2 NA >> >> >> >> The desired output should be like this :- >> >> w r matched >items >> 1 cyp3,cyp7 cyp2, cyp1,cyp3 cyp3 >> 2 cyp2 cyp2 cyp2 >> 3 c1,c3,c6 c6,c8,c5 c6 >> >> >> The second issue is that when I write a table produced in R as >follows : >> >> write.table(MyTable,file="MyTable.txt", sep = "\t", quote = F, >row.names = F) >> >> and the read this txt. file in excel, some items from column B >appears in Column A and some empty rows also appear?. >> >> Could you please guide me about the mistakes I have done and suggest >> some solutions? >> >> Regards >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help > > >thz.ch/mailman/listinfo/r-help> >stat.ethz.ch >The main R mailing list, for announcements about the development of R >and the availability of new code, questions and answers about problems >and solutions using R ... > > > >> PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. > > [[alternative HTML version deleted]] > >______________________________________________ >R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >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 -- To UNSUBSCRIBE and more, see 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.