Filtering materials of two columns in R
1
0
Entering edit mode
10.1 years ago
mjoyraj ▴ 80

I have a two column dataset as given below. I want to add a third column which will contain all genes from column 1 and 2, but none of the genes should repeat. How can I do this in R?

ID        ID2
Gene1     Gene3
Gene1     Gene5
Gene2     Gene7
Gene2     Gene4
Gene3     Gene6
Gene3     Gene8
R • 2.4k views
ADD COMMENT
0
Entering edit mode

please add an example of column 3

ADD REPLY
0
Entering edit mode

The example of column 3 is given below. It contains all characters of col 1 and 2 but genes names do not repeat

Gene1
Gene2
Gene3
Gene4
Gene5
Gene6
Gene7
Gene8
ADD REPLY
1
Entering edit mode
10.1 years ago
linus ▴ 360

Select the two columns (df['ID'], df['ID2']), combine them to a vector (e.g. c()), use the unique() function two remove duplicates. But adding the new vector to the data.frame using cbind will not be possible since it has different length than the original dataframe

ADD COMMENT
0
Entering edit mode

It is okay, if I output the column separately

ADD REPLY
0
Entering edit mode

The suggestion worked with a little modification. Thanks a lot..

ADD REPLY

Login before adding your answer.

Traffic: 3088 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