R script merge 2 rows Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How to merge two dictionaries in a single expression?How to merge a specific commit in GitHow to join (merge) data frames (inner, outer, left, right)How do you merge two Git repositories?How to merge two arrays in JavaScript and de-duplicate itemsGrouping functions (tapply, by, aggregate) and the *apply familyDrop data frame columns by nameRemove rows with all or some NAs (missing values) in data.frameAdding new column to existing DataFrame in Python pandasHow to iterate over rows in a DataFrame in Pandas?

What does it mean that physics no longer uses mechanical models to describe phenomena?

How do living politicians protect their readily obtainable signatures from misuse?

Why should I vote and accept answers?

The code below, is it ill-formed NDR or is it well formed?

Does the Weapon Master feat grant you a fighting style?

A term for a woman complaining about things/begging in a cute/childish way

When a candle burns, why does the top of wick glow if bottom of flame is hottest?

Why weren't discrete x86 CPUs ever used in game hardware?

What do you call the main part of a joke?

Find 108 by using 3,4,6

What is this clumpy 20-30cm high yellow-flowered plant?

How does light 'choose' between wave and particle behaviour?

Central Vacuuming: Is it worth it, and how does it compare to normal vacuuming?

AppleTVs create a chatty alternate WiFi network

Do wooden building fires get hotter than 600°C?

How can I reduce the gap between left and right of cdot with a macro?

Can the Great Weapon Master feat's "Power Attack" apply to attacks from the Spiritual Weapon spell?

Do I really need to have a message in a novel to appeal to readers?

What was the first language to use conditional keywords?

Do any jurisdictions seriously consider reclassifying social media websites as publishers?

Question about debouncing - delay of state change

What is the font for "b" letter?

Project Euler #1 in C++

How could we fake a moon landing now?



R script merge 2 rows



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How to merge two dictionaries in a single expression?How to merge a specific commit in GitHow to join (merge) data frames (inner, outer, left, right)How do you merge two Git repositories?How to merge two arrays in JavaScript and de-duplicate itemsGrouping functions (tapply, by, aggregate) and the *apply familyDrop data frame columns by nameRemove rows with all or some NAs (missing values) in data.frameAdding new column to existing DataFrame in Python pandasHow to iterate over rows in a DataFrame in Pandas?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








-1















I have one data frame like:



 a b c d e f g 
1 Car 10/02 01/02 30/02 14 1 NA
2 Car 10/02 07/02 20/02 0 NA 7


I want to get :



 a b c d e f g 
1 Car 10/02 01/02 20/02 14 1 7


Like a group by (a,b) , select the min Date for c and d, select the max for e and select the non-null for f and g



How can I solve it in R ?










share|improve this question
























  • Please share the data using dput and the logic

    – Sonny
    Mar 22 at 10:14











  • I want to group by (a,b) select the Date min on c and on d select the max number on e select the not null on f and g

    – Catapultaa
    Mar 22 at 10:35











  • whats the pattern. in col d you take the second value . in col c you take the first row

    – MatthewR
    Mar 22 at 10:36











  • I take the min Date

    – Catapultaa
    Mar 22 at 10:38

















-1















I have one data frame like:



 a b c d e f g 
1 Car 10/02 01/02 30/02 14 1 NA
2 Car 10/02 07/02 20/02 0 NA 7


I want to get :



 a b c d e f g 
1 Car 10/02 01/02 20/02 14 1 7


Like a group by (a,b) , select the min Date for c and d, select the max for e and select the non-null for f and g



How can I solve it in R ?










share|improve this question
























  • Please share the data using dput and the logic

    – Sonny
    Mar 22 at 10:14











  • I want to group by (a,b) select the Date min on c and on d select the max number on e select the not null on f and g

    – Catapultaa
    Mar 22 at 10:35











  • whats the pattern. in col d you take the second value . in col c you take the first row

    – MatthewR
    Mar 22 at 10:36











  • I take the min Date

    – Catapultaa
    Mar 22 at 10:38













-1












-1








-1








I have one data frame like:



 a b c d e f g 
1 Car 10/02 01/02 30/02 14 1 NA
2 Car 10/02 07/02 20/02 0 NA 7


I want to get :



 a b c d e f g 
1 Car 10/02 01/02 20/02 14 1 7


Like a group by (a,b) , select the min Date for c and d, select the max for e and select the non-null for f and g



How can I solve it in R ?










share|improve this question
















I have one data frame like:



 a b c d e f g 
1 Car 10/02 01/02 30/02 14 1 NA
2 Car 10/02 07/02 20/02 0 NA 7


I want to get :



 a b c d e f g 
1 Car 10/02 01/02 20/02 14 1 7


Like a group by (a,b) , select the min Date for c and d, select the max for e and select the non-null for f and g



How can I solve it in R ?







r dataframe merge row






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 22 at 10:41







Catapultaa

















asked Mar 22 at 10:12









CatapultaaCatapultaa

357




357












  • Please share the data using dput and the logic

    – Sonny
    Mar 22 at 10:14











  • I want to group by (a,b) select the Date min on c and on d select the max number on e select the not null on f and g

    – Catapultaa
    Mar 22 at 10:35











  • whats the pattern. in col d you take the second value . in col c you take the first row

    – MatthewR
    Mar 22 at 10:36











  • I take the min Date

    – Catapultaa
    Mar 22 at 10:38

















  • Please share the data using dput and the logic

    – Sonny
    Mar 22 at 10:14











  • I want to group by (a,b) select the Date min on c and on d select the max number on e select the not null on f and g

    – Catapultaa
    Mar 22 at 10:35











  • whats the pattern. in col d you take the second value . in col c you take the first row

    – MatthewR
    Mar 22 at 10:36











  • I take the min Date

    – Catapultaa
    Mar 22 at 10:38
















Please share the data using dput and the logic

– Sonny
Mar 22 at 10:14





Please share the data using dput and the logic

– Sonny
Mar 22 at 10:14













I want to group by (a,b) select the Date min on c and on d select the max number on e select the not null on f and g

– Catapultaa
Mar 22 at 10:35





I want to group by (a,b) select the Date min on c and on d select the max number on e select the not null on f and g

– Catapultaa
Mar 22 at 10:35













whats the pattern. in col d you take the second value . in col c you take the first row

– MatthewR
Mar 22 at 10:36





whats the pattern. in col d you take the second value . in col c you take the first row

– MatthewR
Mar 22 at 10:36













I take the min Date

– Catapultaa
Mar 22 at 10:38





I take the min Date

– Catapultaa
Mar 22 at 10:38












3 Answers
3






active

oldest

votes


















0














Using dplyr we can group_by a and b, convert c and d to actual dates using dmy from lubridate and select the minimum date, select maximum value of e and non-NA value of f and g.



library(dplyr)
library(lubridate)

df %>%
group_by(a, b) %>%
summarise(c = c[which.min(dmy(paste0(c, "/19")))],
d = d[which.min(dmy(paste0(d, "/19")))],
e = max(e),
f = f[!is.na(f)],
g = g[!is.na(g)])

# a b c d e f g
# <fct> <fct> <fct> <fct> <dbl> <int> <int>
#1 Car 10/02 01/02 20/02 14 1 7


As shown in the example, I am assuming you would have only one non-NA value for f and g if you have more than one then use which.max to select the first non-NA value from those columns.






share|improve this answer






























    0














    Use library dplyr, let's say your dataframe is df. First thing is replace NA by 0



    df[is.na(df)] <- 0 # you can now directly take sum of f and g column in group by
    library(dplyr)
    df_1 <- df %>% group_by(a,b) %>% summarise(c = min(c),d = min(d),e = max(e),f = sum(f),g = sum(g))





    share|improve this answer























    • min would not work for dates. (column c and d). Here it works because min(c("01", "07")) gives you "01" but it would give you unexpected results for min(c("11", "7")). You need to convert it to date first to use min, max functions.

      – Ronak Shah
      Mar 22 at 10:50


















    0














    using data.table. I had to change one of your dates because there is no feb 30th



     x <- data.frame( 
    a= c( "Car","Car"), b=c("10/20","10/20"), c=c("01/02","07/02"),d=c("28/02","20/02"), e=c(14,0), f=c(1, NA), g=c(NA,7))

    library( data.table) # you may need to install
    x <- data.table( x )

    #convert to dates
    x$c <- as.Date(x$c, "%d/%m")
    x$d <- as.Date(x$d, "%d/%m")

    # group as you specfied
    x[ ,

    .(
    c = min( c ),
    d = min( d ),
    e = max( e ),
    f= max( f , na.rm=T),
    g= max( g , na.rm=T)
    ) ,

    by= c( "a","b")
    ]





    share|improve this answer

























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55297335%2fr-script-merge-2-rows%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Using dplyr we can group_by a and b, convert c and d to actual dates using dmy from lubridate and select the minimum date, select maximum value of e and non-NA value of f and g.



      library(dplyr)
      library(lubridate)

      df %>%
      group_by(a, b) %>%
      summarise(c = c[which.min(dmy(paste0(c, "/19")))],
      d = d[which.min(dmy(paste0(d, "/19")))],
      e = max(e),
      f = f[!is.na(f)],
      g = g[!is.na(g)])

      # a b c d e f g
      # <fct> <fct> <fct> <fct> <dbl> <int> <int>
      #1 Car 10/02 01/02 20/02 14 1 7


      As shown in the example, I am assuming you would have only one non-NA value for f and g if you have more than one then use which.max to select the first non-NA value from those columns.






      share|improve this answer



























        0














        Using dplyr we can group_by a and b, convert c and d to actual dates using dmy from lubridate and select the minimum date, select maximum value of e and non-NA value of f and g.



        library(dplyr)
        library(lubridate)

        df %>%
        group_by(a, b) %>%
        summarise(c = c[which.min(dmy(paste0(c, "/19")))],
        d = d[which.min(dmy(paste0(d, "/19")))],
        e = max(e),
        f = f[!is.na(f)],
        g = g[!is.na(g)])

        # a b c d e f g
        # <fct> <fct> <fct> <fct> <dbl> <int> <int>
        #1 Car 10/02 01/02 20/02 14 1 7


        As shown in the example, I am assuming you would have only one non-NA value for f and g if you have more than one then use which.max to select the first non-NA value from those columns.






        share|improve this answer

























          0












          0








          0







          Using dplyr we can group_by a and b, convert c and d to actual dates using dmy from lubridate and select the minimum date, select maximum value of e and non-NA value of f and g.



          library(dplyr)
          library(lubridate)

          df %>%
          group_by(a, b) %>%
          summarise(c = c[which.min(dmy(paste0(c, "/19")))],
          d = d[which.min(dmy(paste0(d, "/19")))],
          e = max(e),
          f = f[!is.na(f)],
          g = g[!is.na(g)])

          # a b c d e f g
          # <fct> <fct> <fct> <fct> <dbl> <int> <int>
          #1 Car 10/02 01/02 20/02 14 1 7


          As shown in the example, I am assuming you would have only one non-NA value for f and g if you have more than one then use which.max to select the first non-NA value from those columns.






          share|improve this answer













          Using dplyr we can group_by a and b, convert c and d to actual dates using dmy from lubridate and select the minimum date, select maximum value of e and non-NA value of f and g.



          library(dplyr)
          library(lubridate)

          df %>%
          group_by(a, b) %>%
          summarise(c = c[which.min(dmy(paste0(c, "/19")))],
          d = d[which.min(dmy(paste0(d, "/19")))],
          e = max(e),
          f = f[!is.na(f)],
          g = g[!is.na(g)])

          # a b c d e f g
          # <fct> <fct> <fct> <fct> <dbl> <int> <int>
          #1 Car 10/02 01/02 20/02 14 1 7


          As shown in the example, I am assuming you would have only one non-NA value for f and g if you have more than one then use which.max to select the first non-NA value from those columns.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 22 at 10:41









          Ronak ShahRonak Shah

          48.2k104369




          48.2k104369























              0














              Use library dplyr, let's say your dataframe is df. First thing is replace NA by 0



              df[is.na(df)] <- 0 # you can now directly take sum of f and g column in group by
              library(dplyr)
              df_1 <- df %>% group_by(a,b) %>% summarise(c = min(c),d = min(d),e = max(e),f = sum(f),g = sum(g))





              share|improve this answer























              • min would not work for dates. (column c and d). Here it works because min(c("01", "07")) gives you "01" but it would give you unexpected results for min(c("11", "7")). You need to convert it to date first to use min, max functions.

                – Ronak Shah
                Mar 22 at 10:50















              0














              Use library dplyr, let's say your dataframe is df. First thing is replace NA by 0



              df[is.na(df)] <- 0 # you can now directly take sum of f and g column in group by
              library(dplyr)
              df_1 <- df %>% group_by(a,b) %>% summarise(c = min(c),d = min(d),e = max(e),f = sum(f),g = sum(g))





              share|improve this answer























              • min would not work for dates. (column c and d). Here it works because min(c("01", "07")) gives you "01" but it would give you unexpected results for min(c("11", "7")). You need to convert it to date first to use min, max functions.

                – Ronak Shah
                Mar 22 at 10:50













              0












              0








              0







              Use library dplyr, let's say your dataframe is df. First thing is replace NA by 0



              df[is.na(df)] <- 0 # you can now directly take sum of f and g column in group by
              library(dplyr)
              df_1 <- df %>% group_by(a,b) %>% summarise(c = min(c),d = min(d),e = max(e),f = sum(f),g = sum(g))





              share|improve this answer













              Use library dplyr, let's say your dataframe is df. First thing is replace NA by 0



              df[is.na(df)] <- 0 # you can now directly take sum of f and g column in group by
              library(dplyr)
              df_1 <- df %>% group_by(a,b) %>% summarise(c = min(c),d = min(d),e = max(e),f = sum(f),g = sum(g))






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Mar 22 at 10:43









              adjustedR2adjustedR2

              1413




              1413












              • min would not work for dates. (column c and d). Here it works because min(c("01", "07")) gives you "01" but it would give you unexpected results for min(c("11", "7")). You need to convert it to date first to use min, max functions.

                – Ronak Shah
                Mar 22 at 10:50

















              • min would not work for dates. (column c and d). Here it works because min(c("01", "07")) gives you "01" but it would give you unexpected results for min(c("11", "7")). You need to convert it to date first to use min, max functions.

                – Ronak Shah
                Mar 22 at 10:50
















              min would not work for dates. (column c and d). Here it works because min(c("01", "07")) gives you "01" but it would give you unexpected results for min(c("11", "7")). You need to convert it to date first to use min, max functions.

              – Ronak Shah
              Mar 22 at 10:50





              min would not work for dates. (column c and d). Here it works because min(c("01", "07")) gives you "01" but it would give you unexpected results for min(c("11", "7")). You need to convert it to date first to use min, max functions.

              – Ronak Shah
              Mar 22 at 10:50











              0














              using data.table. I had to change one of your dates because there is no feb 30th



               x <- data.frame( 
              a= c( "Car","Car"), b=c("10/20","10/20"), c=c("01/02","07/02"),d=c("28/02","20/02"), e=c(14,0), f=c(1, NA), g=c(NA,7))

              library( data.table) # you may need to install
              x <- data.table( x )

              #convert to dates
              x$c <- as.Date(x$c, "%d/%m")
              x$d <- as.Date(x$d, "%d/%m")

              # group as you specfied
              x[ ,

              .(
              c = min( c ),
              d = min( d ),
              e = max( e ),
              f= max( f , na.rm=T),
              g= max( g , na.rm=T)
              ) ,

              by= c( "a","b")
              ]





              share|improve this answer





























                0














                using data.table. I had to change one of your dates because there is no feb 30th



                 x <- data.frame( 
                a= c( "Car","Car"), b=c("10/20","10/20"), c=c("01/02","07/02"),d=c("28/02","20/02"), e=c(14,0), f=c(1, NA), g=c(NA,7))

                library( data.table) # you may need to install
                x <- data.table( x )

                #convert to dates
                x$c <- as.Date(x$c, "%d/%m")
                x$d <- as.Date(x$d, "%d/%m")

                # group as you specfied
                x[ ,

                .(
                c = min( c ),
                d = min( d ),
                e = max( e ),
                f= max( f , na.rm=T),
                g= max( g , na.rm=T)
                ) ,

                by= c( "a","b")
                ]





                share|improve this answer



























                  0












                  0








                  0







                  using data.table. I had to change one of your dates because there is no feb 30th



                   x <- data.frame( 
                  a= c( "Car","Car"), b=c("10/20","10/20"), c=c("01/02","07/02"),d=c("28/02","20/02"), e=c(14,0), f=c(1, NA), g=c(NA,7))

                  library( data.table) # you may need to install
                  x <- data.table( x )

                  #convert to dates
                  x$c <- as.Date(x$c, "%d/%m")
                  x$d <- as.Date(x$d, "%d/%m")

                  # group as you specfied
                  x[ ,

                  .(
                  c = min( c ),
                  d = min( d ),
                  e = max( e ),
                  f= max( f , na.rm=T),
                  g= max( g , na.rm=T)
                  ) ,

                  by= c( "a","b")
                  ]





                  share|improve this answer















                  using data.table. I had to change one of your dates because there is no feb 30th



                   x <- data.frame( 
                  a= c( "Car","Car"), b=c("10/20","10/20"), c=c("01/02","07/02"),d=c("28/02","20/02"), e=c(14,0), f=c(1, NA), g=c(NA,7))

                  library( data.table) # you may need to install
                  x <- data.table( x )

                  #convert to dates
                  x$c <- as.Date(x$c, "%d/%m")
                  x$d <- as.Date(x$d, "%d/%m")

                  # group as you specfied
                  x[ ,

                  .(
                  c = min( c ),
                  d = min( d ),
                  e = max( e ),
                  f= max( f , na.rm=T),
                  g= max( g , na.rm=T)
                  ) ,

                  by= c( "a","b")
                  ]






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Mar 22 at 10:53

























                  answered Mar 22 at 10:35









                  MatthewRMatthewR

                  93011019




                  93011019



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55297335%2fr-script-merge-2-rows%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                      SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                      은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현