Set multiple attributes to a single node in R
3
0
Entering edit mode
7.1 years ago
Spacebio ▴ 200

Hello,

I have a data.frame of genes like this:

GENE1 ACTIVATION
GENE1 INHIBITION
GENE1 ACTIVATION
GENE1 ACTIVATION
GENE2 UNKNOWN
GENE2 INHIBITION
GENE2 UNKNOWN
GENE3 ACTIVATION
GENE3 UNKNOWN
GENE3 ACTIVATION

and I would like to keep the table in the following format

GENE1 ACTIVATION
GENE1 INHIBITION
GENE2 UNKNOWN
GENE2 INHIBITION
GENE3 ACTIVATION
GENE3 UNKNOWN

I tried with the following code:

genes <- genes[!duplicated(genes[1]),]

but like that, I just get the "unique" genes of the column one, for example

GENE1 ACTIVATION
GENE2 INHIBITION
GENE3 UNKNOWN

Any suggestions? Thanks in advance.

R igraph attributes • 2.0k views
ADD COMMENT
3
Entering edit mode
7.1 years ago

1) import/convert your table to a dataframe, called genes

2) run:

genes[!duplicated(genes[, 1:2]), ]

3) you can then transform this to a graph using igraph:

geneNetwork = graph.data.frame(genes)
ADD COMMENT
0
Entering edit mode

Thanks for your help! I just tried with genes[!duplicated(genes[, 1:2]), ] and it works perfectly! Thank you so much!!

ADD REPLY
0
Entering edit mode

[edit] I see that you edited because you solved it, you are welcome :)

ADD REPLY
0
Entering edit mode
7.1 years ago
e.rempel ★ 1.1k

Hi,

is it a data.frame? I am asking because you said

I have a list of genes

If it is a data.frame, you could just remove [1] from

genes <- genes[!duplicated(genes[1]),]

because duplicated can work on rows.

ADD COMMENT
0
Entering edit mode

Sorry, my bad! It is a data.frame. But following your suggestion, I get the following:

GENE1 ACTIVATION
GENE1 INHIBITION
GENE1 ACTIVATION
GENE2 UNKNOWN
GENE2 INHIBITION
GENE2 UNKNOWN
GENE3 ACTIVATION
GENE3 UNKNOWN
GENE3 ACTIVATION

At the end it is the same result.

ADD REPLY
0
Entering edit mode
7.1 years ago
> genes= read.csv("test",header=F, stringsAsFactors=F, sep="")

.

> genes
      V1         V2
1  GENE1 ACTIVATION
2  GENE1 INHIBITION
3  GENE1 ACTIVATION
4  GENE1 ACTIVATION
5  GENE2    UNKNOWN
6  GENE2 INHIBITION
7  GENE2    UNKNOWN
8  GENE3 ACTIVATION
9  GENE3    UNKNOWN
10 GENE3 ACTIVATION

.

> unique(genes)
     V1         V2
1 GENE1 ACTIVATION
2 GENE1 INHIBITION
5 GENE2    UNKNOWN
6 GENE2 INHIBITION
8 GENE3 ACTIVATION
9 GENE3    UNKNOWN
ADD COMMENT

Login before adding your answer.

Traffic: 2771 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6