Changing the default delimiter in summarise_all in R dply package

Viewed 42

I'm combining my duplicated values into a single column using delimiters. I'm using the R dplyr library.

library (dplyr)
Input = read.csv("test.csv")
test=Input%>%
group_by(V1,V2,V3,V4,V5,V6)%>%
summarise_all(~toString(na.omit(.)))

My input data looks like this.*

V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
chr1 12364 12365 A T Lung 1236 cosmic Cancer, dominant reported
chr1 12364 12365 A T Lung 3531 Pubed recessive
chr1 12364 12365 A T heart 4616 HGMD dominant pathogenic
chr1 12364 12365 A T brain 9471 Pubed recessive
chr1 12364 12365 A T Lung cosmic Cancer
chr1 12364 12365 A T heart 36481 Pubed Cancer benign
chr1 12364 12365 A T Lung 8351 cosmic Cancer
chr3 19261 19262 G C Lung 453 HGMD Cancer likely pathogenic
chr5 171672 171673 T G 6451 HGMD Cancer likely pathogenic
chr15 10391 10391 G T 8537 HGMD Cancer likely pathogenic

My output looks like this.

V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
chr1 12364 12365 A T brain 9471 Pubed recessive
chr1 12364 12365 A T heart 4616, 36481 HGMD, Pubed dominant, Cancer pathogenic, benign
chr1 12364 12365 A T Lung 1236, 3531, 8351 cosmic, Pubed, cosmic, cosmic Cancer, dominant, recessive, Cancer, Cancer reported, , ,
chr15 10391 10391 G T 8537 HGMD Cancer likely pathogenic
chr3 19261 19262 G C Lung 453 HGMD Cancer likely pathogenic
chr5 171672 171673 T G 6451 HGMD Cancer likely

The problem is that my data already has a comma as the delimiter. So, I need to change the delimiter from the default comma to something else. Can anyone suggest me any better ideas for it?

1 Answers

Use summarise_all(~paste(., collapse = "|")) instead.

Example:

library(tidyverse)

n <- 30
db <- tibble(x = sample(letters[1:4], n, replace = T),
             y = sample(letters[1:5], n, replace = T))
db %>% group_by(x) %>% summarise_all(~paste(., collapse = "|")) %>% ungroup()

Output:

# A tibble: 4 × 2
  x     y                  
  <chr> <chr>              
1 a     b|a|c|d|d|e|a|b|b|a
2 b     c|b|e|c|b|c|b|a    
3 c     a|a|d|b|d|e|b|d|e|a
4 d     a|d                

However, I don't quite understand the problem with the comma in your input dataset: once it has been imported, it's no longer a comma-separated file. Of course, if you want to write the result as csv, you may change the separator, but you could also use quotes around field values.

Related