Delete row if two values in a matrix are equal

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Delete row if two values in a matrix are equal

JDINIS
Hi all and thank you for your time.

I would like to delete rows from this matrix I call "var" if the character in Ref_Allele is equal to the character in Var_Allele. I have attached a before and after, to help my poor explanation. If someone could provide me with some code, or some guidance I would really appreciate it.

Thank you again.

"Before"
> var
Ref_Pos Ref_Allele Var_Allele Var_Freq
     1         A              A             100
     2                T              G              50
     3                G              G             100
     4                G              T              45
     5                 T              C              80



"After"
Ref_Pos Ref_Allele Var_Allele Var_Freq
     2                T              G              50
     4                G              T              45
     5                 T              C              80
Reply | Threaded
Open this post in threaded view
|

Re: Delete row if two values in a matrix are equal

Rui Barradas
Hello,

First, two notes:
1. 'var' is a really bad name for a variable, it already is an R function.
2. Your matrix seems more like a data.frame. The difference is important
because data.frames by default coerce character strings to factors. I
have tried to make the code work if this is the case or not.


vr <- read.table(text="
Ref_Pos Ref_Allele Var_Allele Var_Freq
      1             A                  A                 100
      2                    T                  G                  50
      3                    G                  G                 100
      4                    G                  T                  45
      5                     T                  C                  80
", header = TRUE)

yes <- as.character(vr[, "Ref_Allele"]) != as.character(vr[, "Var_Allele"])
vr[yes, ]



Hope this helps,

Rui Barradas

Em 26-10-2012 15:26, JDINIS escreveu:

> Hi all and thank you for your time.
>
> I would like to delete rows from this matrix I call "var" if the character
> in Ref_Allele is equal to the character in Var_Allele. I have attached a
> before and after, to help my poor explanation. If someone could provide me
> with some code, or some guidance I would really appreciate it.
>
> Thank you again.
>
> "Before"
>> var
> Ref_Pos Ref_Allele Var_Allele Var_Freq
>       1         A              A             100
>       2                T              G              50
>       3                G              G             100
>       4                G              T              45
>       5                 T              C              80
>
>
>
> "After"
> Ref_Pos Ref_Allele Var_Allele Var_Freq
>       2                T              G              50
>       4                G              T              45
>       5                 T              C              80
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Delete-row-if-two-values-in-a-matrix-are-equal-tp4647554.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Delete row if two values in a matrix are equal

JDINIS
Thanks Rui for all your help and I really appreciate you taking the time to explain everything!
Reply | Threaded
Open this post in threaded view
|

Re: Delete row if two values in a matrix are equal

arun kirshna
In reply to this post by JDINIS
Hi,
You can try either one of these:
dat1<-read.table(text="
Ref_Pos    Ref_Allele    Var_Allele    Var_Freq
      1            A                  A                100
      2                    T                  G                  50
      3                    G                  G                100
      4                    G                  T                  45
      5                    T                  C                  80
",sep="",header=TRUE,stringsAsFactors=FALSE)


dat2<-dat1[unlist(lapply(split(dat1[,2:3],1:nrow(dat1[,2:3])),function(x) unlist(gregexpr(x[1],x[2]))!=1),use.names=FALSE),]
dat2
#  Ref_Pos Ref_Allele Var_Allele Var_Freq
#2       2          T          G       50
#4       4          G          T       45
#5       5          T          C       80

#or

dat3<-dat1[apply(dat1,1,function(x) x[2]!=x[3]),]
dat3
#  Ref_Pos Ref_Allele Var_Allele Var_Freq
#2       2          T          G       50
#4       4          G          T       45
#5       5          T          C       80

#or

dat4<-dat1[dat1[,2]!=dat1[,3],]
dat4
#  Ref_Pos Ref_Allele Var_Allele Var_Freq
#2       2          T          G       50
#4       4          G          T       45
#5       5          T          C       80
identical(dat2,dat3)
#[1] TRUE
identical(dat2,dat4)
#[1] TRUE

A.K.



----- Original Message -----
From: JDINIS <[hidden email]>
To: [hidden email]
Cc:
Sent: Friday, October 26, 2012 10:26 AM
Subject: [R] Delete row if two values in a matrix are equal

Hi all and thank you for your time.

I would like to delete rows from this matrix I call "var" if the character
in Ref_Allele is equal to the character in Var_Allele. I have attached a
before and after, to help my poor explanation. If someone could provide me
with some code, or some guidance I would really appreciate it.

Thank you again.

"Before"
> var
Ref_Pos    Ref_Allele    Var_Allele    Var_Freq
     1             A                  A                 100
     2                    T                  G                  50
     3                    G                  G                 100
     4                    G                  T                  45
     5                     T                  C                  80



"After"
Ref_Pos    Ref_Allele    Var_Allele    Var_Freq
     2                    T                  G                  50
     4                    G                  T                  45
     5                     T                  C                  80




--
View this message in context: http://r.789695.n4.nabble.com/Delete-row-if-two-values-in-a-matrix-are-equal-tp4647554.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
[hidden email] 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.


______________________________________________
[hidden email] 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.