0

I have a data.frame with the following structure:

enter image description here

What I need is that in case that a value in the first column occures more than once, all corresponding entries in column V18 are concluded in one cell. I applied the folling code.

p <- function(v) {
  Reduce(f=paste0, x = v)
}

Data %>% 
    group_by(V1) %>% 
    summarise(test = p(as.character(V18))) %>%
    merge(., M_TEST, by = 'V1') %>%
    select(V1, V18, test)

It gives:

enter image description here

What I need is that instead of 4344, it is {43,44}. How can I do this?

Thank you really much for your help!

Sincerely

Martin Gal
  • 16,640
  • 5
  • 21
  • 39
A321
  • 17
  • 4
  • 1
    Please don't post data as images. Take a look at how to make a [great reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for ways of showing data. – Martin Gal Jul 20 '21 at 18:32
  • 1
    Something like this? `Data %>% group_by(V1) %>% summarize(V18 = paste(V18, collapse = ","))` –  Jul 20 '21 at 18:43
  • This is actually pretty good. Thank you @Jean-Claude Arbaut. That gives 43,44. What I need is {43,44}. Do you know if that is somehow possible as well? – A321 Jul 20 '21 at 18:51
  • Sure. `paste0("{", paste(V18, collapse=","), "}")` –  Jul 20 '21 at 19:16
  • You may also do `Data %>% group_by(V1) %>% summarize(V18 = list(V18))`. It's not exactly what you want, but it stores the grouped elements in an actual R list (of vectors). Can be handy in some cases. –  Jul 20 '21 at 19:20

1 Answers1

0

Try This:

Data %>% 
  group_by(V1) %>% 
  summarise(test = p(as.character(V18))) %>%
  merge(., M_TEST, by = 'V1') %>%
  select(V1, V18, test) %>% 
  mutate(test = str_remove_all(test, pattern = "NA")) %>% 
  mutate(test = formatC(as.numeric(test), big.mark=",", big.interval = 2L)) %>% 
  mutate(test = paste0("{", test, "}"))

Edit: For Multiple Columns, this should work:

Data %>% 
  group_by(V1) %>% 
  summarise_at(vars(V2:V18), paste0, collapse="") %>% 
  mutate_at(vars(V2:V18), str_remove_all, pattern = "NA") %>% 
  mutate_at(vars(V2:V18), as.numeric) %>% 
  mutate_at(vars(V2:V18), formatC, big.mark=",", big.interval = 2L)
Mohanasundaram
  • 2,889
  • 1
  • 8
  • 18