0

I have the following Issue with formatting a data frame in Rstudio:

The baseline data frame has the following structure: enter image description here

There are two column with numbers. The first column includes several numbers several time. The goal is to conclude all number in the second column which correspond to the same number in the first column into one cell. This shall more or less look like this:

enter image description here

E.g. row 11 in the test column now has all numbers, which correspond to 2 in the first column.

This was produced applying the following code:

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

Results <- Data %>% 
    group_by(V1) %>% 
    summarise(test = p(as.character(V8))) %>%
    merge(., Data , by = 'V1') %>%
    select(V1, V8, test)

But instead of 2312, the format shall be {2,3,12}. In case that all numbers have the same length of 2, this works via:

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

(This code was posted by @Mohanasundaram in Formatting of Data Frames in R)

Does someone have an idea how that could work with varying size of the numbers?

user438383
  • 4,338
  • 6
  • 23
  • 35
A321
  • 7
  • 3
  • 1
    Please use ``dput()`` to share your data and do not include images of data or code. Also, you are programming in `R` and not `Rstudio`. Rstudio is an IDE not a programming language. – user438383 Aug 06 '21 at 11:23

0 Answers0