[R] Matched Items in rows + issue with writing a table
abo dalash
abo_dlsh at hotmail.com
Fri May 12 20:08:50 CEST 2017
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 ...
> 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