samedi 1 décembre 2018

Replace rows with another neighbor rows in R with statement

I have a problem may be too easy to solve but i'm trying.

>data

       xloc     genes
1 XLOC_000002 AT1G01046
2 XLOC_000006 AT1G01130
3 XLOC_000009 CUFF.14.1
4 XLOC_000010 CUFF.15.1
5 XLOC_000027 AT1G01448
6 XLOC_000027 AT1G01448

I want to join both columns replacing the rows that contain "CUFF" with the elements from the first column, like this:

       xloc     genes      x
1 XLOC_000002 AT1G01046 AT1G01046  
2 XLOC_000006 AT1G01130 AT1G01130
3 XLOC_000009 CUFF.14.1 XLOC_000009
4 XLOC_000010 CUFF.15.1 XLOC_000010
5 XLOC_000027 AT1G01448 AT1G01448
6 XLOC_000027 AT1G01448 AT1G01448

Im trying with:

data$x <- ifelse(grepl("^CUFF",data$genes),data$xloc, data$genes)

But the output is like this:

        xloc     genes  x
1 XLOC_000002 AT1G01046  5
2 XLOC_000006 AT1G01130 16
3 XLOC_000009 CUFF.14.1  9
4 XLOC_000010 CUFF.15.1 10
5 XLOC_000027 AT1G01448 51
6 XLOC_000027 AT1G01448 51

Help please

Regards

Aucun commentaire:

Enregistrer un commentaire