[R] Matched Items in rows + issue with writing a table

abo dalash abo_dlsh at hotmail.com
Sun May 14 18:36:08 CEST 2017


I would like to thank anyone here spending the time to help.


Jeff, I did not understand your recommendations regarding understanding the code. I tried to break the parts of the code but this does not make sense.




> x[1]
"1"
> x[2]

"2"

N <- strsplit(x[1],sep)
> N
[[1]]
[1] "1"

> strsplit(x444$w,sep)
[[1]]
[1] "cyp3" "cyp7"

[[2]]
[1] "cyp2"

[[3]]
[1] "c1" "c3" "c6"

> unlist(x444$w,sep)
[1] "cyp3,cyp7" "cyp2"      "c1,c3,c6"


Jim, thank you so much for writing the code which works but I will not complete my analysis until I understand what happens in each line. Otherwise, I will try to search for alternative solutions which I can understand.  If anyone can explain me what each part of the code has done, then please kindly replay . Please do not reply to just say try to teach yourself as I usually do not post any question here or ask for explanation until after I have already spent many hours or sometimes days of multiple tries. I also

have registered to an R course but many of what I have learnt until now was with just simple examples.  The blue lines bellows are the ones I did not understand.



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)



________________________________
From: Jeff Newmiller <jdnewmil at dcn.davis.ca.us>
Sent: 12 May 2017 10:43 PM
To: r-help at r-project.org; abo dalash; Jim Lemon
Cc: r-help at R-project.org
Subject: Re: [R] Matched Items in rows + issue with writing a table

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_dlsh at 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 <drjimlemon at gmail.com>
>Sent: 12 May 2017 04:14 AM
>To: abo dalash
>Cc: r-help at 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_dlsh at 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 at 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 ...



>
>
>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 at 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]]



More information about the R-help mailing list