Skip to main content
added 585 characters in body
Source Link
Ram RS
  • 2.5k
  • 11
  • 29

Based on your comments, you wish to obtain a semi-colon separated list of unique values in the gene column. You can create a new column containing these using:

xyz <- data.frame(chr="chr1", gene = c("RUNX3;RUNX3", "RUNX3;RUNX3", "SFN", "NR0B2", "NR0B2", "TFAP2E", "A;B;A"))

xyz
   chr        gene
1 chr1 RUNX3;RUNX3
2 chr1 RUNX3;RUNX3
3 chr1         SFN
4 chr1       NR0B2
5 chr1       NR0B2
6 chr1      TFAP2E
7 chr1       A;B;A

xyz$gene_uniq <- sapply(xyz$gene, function(x) paste(unique(unlist(strsplit(x, ";"))), collapse=";"))

xyz
   chr        gene gene_uniq
1 chr1 RUNX3;RUNX3     RUNX3
2 chr1 RUNX3;RUNX3     RUNX3
3 chr1         SFN       SFN
4 chr1       NR0B2     NR0B2
5 chr1       NR0B2     NR0B2
6 chr1      TFAP2E    TFAP2E
7 chr1       A;B;A       A;B

Or you can use dplyr. I'll edit and paste an exampleI'm adding a column called entry_num below since my example has non-unique combinations of chr and gene that we need to retain.

library(dplyr)
library(tidyr)

xyz <- data.frame(chr="chr1", gene = c("RUNX3;RUNX3", "RUNX3;RUNX3", "SFN", "NR0B2", "NR0B2", "TFAP2E", "A;B;A"))
xyz %>% mutate(entry_num = 1:nrow(xyz)) %>% separate_rows(gene, sep = ";") %>% unique() %>% group_by(chr, entry_num) %>% summarize(gene = paste(gene, collapse=";")) %>% select(-entry_num) %>% as.data.frame()

   chr   gene
1 chr1  RUNX3
2 chr1  RUNX3
3 chr1    SFN
4 chr1  NR0B2
5 chr1  NR0B2
6 chr1 TFAP2E
7 chr1    A;B

Based on your comments, you wish to obtain a semi-colon separated list of unique values in the gene column. You can create a new column containing these using:

xyz <- data.frame(chr="chr1", gene = c("RUNX3;RUNX3", "RUNX3;RUNX3", "SFN", "NR0B2", "NR0B2", "TFAP2E", "A;B;A"))

xyz
   chr        gene
1 chr1 RUNX3;RUNX3
2 chr1 RUNX3;RUNX3
3 chr1         SFN
4 chr1       NR0B2
5 chr1       NR0B2
6 chr1      TFAP2E
7 chr1       A;B;A

xyz$gene_uniq <- sapply(xyz$gene, function(x) paste(unique(unlist(strsplit(x, ";"))), collapse=";"))

xyz
   chr        gene gene_uniq
1 chr1 RUNX3;RUNX3     RUNX3
2 chr1 RUNX3;RUNX3     RUNX3
3 chr1         SFN       SFN
4 chr1       NR0B2     NR0B2
5 chr1       NR0B2     NR0B2
6 chr1      TFAP2E    TFAP2E
7 chr1       A;B;A       A;B

Or you can use dplyr. I'll edit and paste an example below.

Based on your comments, you wish to obtain a semi-colon separated list of unique values in the gene column. You can create a new column containing these using:

xyz <- data.frame(chr="chr1", gene = c("RUNX3;RUNX3", "RUNX3;RUNX3", "SFN", "NR0B2", "NR0B2", "TFAP2E", "A;B;A"))

xyz
   chr        gene
1 chr1 RUNX3;RUNX3
2 chr1 RUNX3;RUNX3
3 chr1         SFN
4 chr1       NR0B2
5 chr1       NR0B2
6 chr1      TFAP2E
7 chr1       A;B;A

xyz$gene_uniq <- sapply(xyz$gene, function(x) paste(unique(unlist(strsplit(x, ";"))), collapse=";"))

xyz
   chr        gene gene_uniq
1 chr1 RUNX3;RUNX3     RUNX3
2 chr1 RUNX3;RUNX3     RUNX3
3 chr1         SFN       SFN
4 chr1       NR0B2     NR0B2
5 chr1       NR0B2     NR0B2
6 chr1      TFAP2E    TFAP2E
7 chr1       A;B;A       A;B

Or you can use dplyr. I'm adding a column called entry_num below since my example has non-unique combinations of chr and gene that we need to retain.

library(dplyr)
library(tidyr)

xyz <- data.frame(chr="chr1", gene = c("RUNX3;RUNX3", "RUNX3;RUNX3", "SFN", "NR0B2", "NR0B2", "TFAP2E", "A;B;A"))
xyz %>% mutate(entry_num = 1:nrow(xyz)) %>% separate_rows(gene, sep = ";") %>% unique() %>% group_by(chr, entry_num) %>% summarize(gene = paste(gene, collapse=";")) %>% select(-entry_num) %>% as.data.frame()

   chr   gene
1 chr1  RUNX3
2 chr1  RUNX3
3 chr1    SFN
4 chr1  NR0B2
5 chr1  NR0B2
6 chr1 TFAP2E
7 chr1    A;B
Source Link
Ram RS
  • 2.5k
  • 11
  • 29

Based on your comments, you wish to obtain a semi-colon separated list of unique values in the gene column. You can create a new column containing these using:

xyz <- data.frame(chr="chr1", gene = c("RUNX3;RUNX3", "RUNX3;RUNX3", "SFN", "NR0B2", "NR0B2", "TFAP2E", "A;B;A"))

xyz
   chr        gene
1 chr1 RUNX3;RUNX3
2 chr1 RUNX3;RUNX3
3 chr1         SFN
4 chr1       NR0B2
5 chr1       NR0B2
6 chr1      TFAP2E
7 chr1       A;B;A

xyz$gene_uniq <- sapply(xyz$gene, function(x) paste(unique(unlist(strsplit(x, ";"))), collapse=";"))

xyz
   chr        gene gene_uniq
1 chr1 RUNX3;RUNX3     RUNX3
2 chr1 RUNX3;RUNX3     RUNX3
3 chr1         SFN       SFN
4 chr1       NR0B2     NR0B2
5 chr1       NR0B2     NR0B2
6 chr1      TFAP2E    TFAP2E
7 chr1       A;B;A       A;B

Or you can use dplyr. I'll edit and paste an example below.