0

It’s hard to describe what I mean, I mean I have the following data frame

A   1013574 1014475
A   1014005 1014475
A   1014005 1014435

I want to merge these data into A 1013574 1014475,Is there any function that can do me achieve this goal?

My desired output is two have 1 row for each ID (in my case value "A"), the second column will contain the smallest value and the third the highest value for each ID.

Bloxx
  • 1,495
  • 1
  • 9
  • 21
leelee
  • 77
  • 3
  • You have a dataframe with 3 columns, you want to merge the rows into strings? – user2974951 Oct 27 '21 at 08:59
  • 3
    Please provide a larger example of what you need, it is not clear what you are exactly looking for and which condition leads to your expected output – Basti Oct 27 '21 at 09:03
  • There is no "redundant information" here. The only way I can think of to summarize (not reduce) from the 3-row frame to your 1-row is to [summarize](https://stackoverflow.com/q/11562656/3358272) (assuming by group) with the max of each. Please make this question *reproducible*. This includes sample code you've attempted, sample *unambiguous* data (e.g., `data.frame(x=...,y=...)` or the output from `dput(head(x))`), and intended output given that input. Refs: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Oct 27 '21 at 09:29

1 Answers1

0

This is an updated answer. I think that this is what you want. I added additional rows, so you can see how it works with multiple data.

library(dplyr)
df <- tibble(a = c("A", "A", "A","B", "B", "B" ),
             v1 = as.numeric(c(1013574,1014005,1014005, 1014005, 1014305, 1044005)),
             v2 = as.numeric(c(1014475, 1014475,1014435, 1014435, 1014435, 1314435))) 



df_new <-df %>% group_by(a) %>% mutate(v1 = min(v1),
                                       v2 = max(v2)) %>% 
  distinct()
Bloxx
  • 1,495
  • 1
  • 9
  • 21
  • Thank you for your reply. Sorry for not expressing the question that I want to ask. The second column is the starting point of the value, and the third column is the ending point. I want to merge the same rows in the first column into a row with the smallest starting point and the largest ending point. In my example, 1013574 is the smallest starting value and 1014475 is the largest ending value, so I get A 1013574 1014475, this line – leelee Oct 27 '21 at 10:59
  • Ok, now it is clear. I updated the answer! – Bloxx Oct 27 '21 at 11:15