Sum column based on conditions in another column in a data frameDrop factor levels in a subsetted data frameHow to join (merge) data frames (inner, outer, left, right)Convert a list of data frames into one data frameR - list to data frameDrop data frame columns by nameHow to drop columns by name in a data frameChanging column names of a data frameExtracting specific columns from a data frameSum of hybrid data frames depending on multiple conditions in RSelect rows from a DataFrame based on values in a column in pandas

Is there any way to land a rover on the Moon without using any thrusters?

getting syntax error in simple bash script

My research paper filed as a patent in China by my Chinese supervisor without me as inventor

What explanation do proponents of a Scotland-NI bridge give for it breaking Brexit impasse?

Why does the speed of sound decrease at high altitudes although the air density decreases?

Can I conceal an antihero's insanity - and should I?

Does an oscilloscope subtract voltages as phasors?

How are aircraft depainted?

Is the Dodge action perceptible to other characters?

How do certain apps show new notifications when internet access is restricted to them?

What organs or modifications would be needed for an animal not to require sleep?

How major are these paintwork & rust problems?

Thematic, genred concepts in Ancient Greek?

What is the mathematical notation for rounding a given number to the nearest integer?

Ambiguity in notation resolved by +

If I want an interpretable model, are there methods other than Linear Regression?

Is a suit against a University Dorm for changing policies on a whim likely to succeed (USA)?

What does a Light weapon mean mechanically?

What was redacted in the Yellowhammer report? (Point 15)

Is there a real-world mythological counterpart to WoW's "kill your gods for power" theme?

Does my opponent need to prove his creature has morph?

Should I leave the first authorship of our paper to the student who did the project whereas I solved it?

What hard drive connector is this?

What is the CR of a Metallic Dragon that used Change Shape?



Sum column based on conditions in another column in a data frame


Drop factor levels in a subsetted data frameHow to join (merge) data frames (inner, outer, left, right)Convert a list of data frames into one data frameR - list to data frameDrop data frame columns by nameHow to drop columns by name in a data frameChanging column names of a data frameExtracting specific columns from a data frameSum of hybrid data frames depending on multiple conditions in RSelect rows from a DataFrame based on values in a column in pandas






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I have a data frame, which contains two columns, Time and Response



df = cbind.data.frame(
Time = c(1, 1.2, 1.9, 2.2, 2.9, 3.1, 3.2, 3.2, 3.2, 3.6, 3.9, 4, 5.1, 5.99),
Response = c(1, 1, 1, 2, 3, 3, 3, 4, 3.5, 3.6, 3.3, 6, 11, 13)
)


I want to transform it by summing the Response within the same minute (Time) . [1-2), [2-3), [3-4), [4-5), and [5 and above].



The expected data frame will be



dfe = cbind.data.frame(
time.range = c(1, 2, 3, 4, 5),
Response = c(3, 5, 19.4, 6, 24)
)









share|improve this question






























    1















    I have a data frame, which contains two columns, Time and Response



    df = cbind.data.frame(
    Time = c(1, 1.2, 1.9, 2.2, 2.9, 3.1, 3.2, 3.2, 3.2, 3.6, 3.9, 4, 5.1, 5.99),
    Response = c(1, 1, 1, 2, 3, 3, 3, 4, 3.5, 3.6, 3.3, 6, 11, 13)
    )


    I want to transform it by summing the Response within the same minute (Time) . [1-2), [2-3), [3-4), [4-5), and [5 and above].



    The expected data frame will be



    dfe = cbind.data.frame(
    time.range = c(1, 2, 3, 4, 5),
    Response = c(3, 5, 19.4, 6, 24)
    )









    share|improve this question


























      1












      1








      1


      0






      I have a data frame, which contains two columns, Time and Response



      df = cbind.data.frame(
      Time = c(1, 1.2, 1.9, 2.2, 2.9, 3.1, 3.2, 3.2, 3.2, 3.6, 3.9, 4, 5.1, 5.99),
      Response = c(1, 1, 1, 2, 3, 3, 3, 4, 3.5, 3.6, 3.3, 6, 11, 13)
      )


      I want to transform it by summing the Response within the same minute (Time) . [1-2), [2-3), [3-4), [4-5), and [5 and above].



      The expected data frame will be



      dfe = cbind.data.frame(
      time.range = c(1, 2, 3, 4, 5),
      Response = c(3, 5, 19.4, 6, 24)
      )









      share|improve this question














      I have a data frame, which contains two columns, Time and Response



      df = cbind.data.frame(
      Time = c(1, 1.2, 1.9, 2.2, 2.9, 3.1, 3.2, 3.2, 3.2, 3.6, 3.9, 4, 5.1, 5.99),
      Response = c(1, 1, 1, 2, 3, 3, 3, 4, 3.5, 3.6, 3.3, 6, 11, 13)
      )


      I want to transform it by summing the Response within the same minute (Time) . [1-2), [2-3), [3-4), [4-5), and [5 and above].



      The expected data frame will be



      dfe = cbind.data.frame(
      time.range = c(1, 2, 3, 4, 5),
      Response = c(3, 5, 19.4, 6, 24)
      )






      r dataframe






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 28 at 10:29









      WangWang

      2161 silver badge9 bronze badges




      2161 silver badge9 bronze badges

























          2 Answers
          2






          active

          oldest

          votes


















          6
















          We can use floor to group it for every minute



          library(dplyr)

          df %>%
          group_by(minute = floor(Time)) %>%
          summarise(Response = sum(Response))

          # minute Response
          # <dbl> <dbl>
          #1 1 3
          #2 2 5
          #3 3 20.4
          #4 4 6
          #5 5 24



          Using aggregate in base R



          aggregate(Response~floor(Time), df, sum)


          Also with tapply



          tapply(df$Response, floor(df$Time), sum)



          And for completion data.table option



          library(data.table)
          setDT(df)[,sum(Response), by = floor(Time)]





          share|improve this answer


































            1
















            We can use rowsum from base R



            rowsum(df$Response, as.integer(df$Time))
            # [,1]
            #1 3.0
            #2 5.0
            #3 20.4
            #4 6.0
            #5 24.0





            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/4.0/"u003ecc by-sa 4.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%2f55395339%2fsum-column-based-on-conditions-in-another-column-in-a-data-frame%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              6
















              We can use floor to group it for every minute



              library(dplyr)

              df %>%
              group_by(minute = floor(Time)) %>%
              summarise(Response = sum(Response))

              # minute Response
              # <dbl> <dbl>
              #1 1 3
              #2 2 5
              #3 3 20.4
              #4 4 6
              #5 5 24



              Using aggregate in base R



              aggregate(Response~floor(Time), df, sum)


              Also with tapply



              tapply(df$Response, floor(df$Time), sum)



              And for completion data.table option



              library(data.table)
              setDT(df)[,sum(Response), by = floor(Time)]





              share|improve this answer































                6
















                We can use floor to group it for every minute



                library(dplyr)

                df %>%
                group_by(minute = floor(Time)) %>%
                summarise(Response = sum(Response))

                # minute Response
                # <dbl> <dbl>
                #1 1 3
                #2 2 5
                #3 3 20.4
                #4 4 6
                #5 5 24



                Using aggregate in base R



                aggregate(Response~floor(Time), df, sum)


                Also with tapply



                tapply(df$Response, floor(df$Time), sum)



                And for completion data.table option



                library(data.table)
                setDT(df)[,sum(Response), by = floor(Time)]





                share|improve this answer





























                  6














                  6










                  6









                  We can use floor to group it for every minute



                  library(dplyr)

                  df %>%
                  group_by(minute = floor(Time)) %>%
                  summarise(Response = sum(Response))

                  # minute Response
                  # <dbl> <dbl>
                  #1 1 3
                  #2 2 5
                  #3 3 20.4
                  #4 4 6
                  #5 5 24



                  Using aggregate in base R



                  aggregate(Response~floor(Time), df, sum)


                  Also with tapply



                  tapply(df$Response, floor(df$Time), sum)



                  And for completion data.table option



                  library(data.table)
                  setDT(df)[,sum(Response), by = floor(Time)]





                  share|improve this answer















                  We can use floor to group it for every minute



                  library(dplyr)

                  df %>%
                  group_by(minute = floor(Time)) %>%
                  summarise(Response = sum(Response))

                  # minute Response
                  # <dbl> <dbl>
                  #1 1 3
                  #2 2 5
                  #3 3 20.4
                  #4 4 6
                  #5 5 24



                  Using aggregate in base R



                  aggregate(Response~floor(Time), df, sum)


                  Also with tapply



                  tapply(df$Response, floor(df$Time), sum)



                  And for completion data.table option



                  library(data.table)
                  setDT(df)[,sum(Response), by = floor(Time)]






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Mar 28 at 10:42

























                  answered Mar 28 at 10:32









                  Ronak ShahRonak Shah

                  82k13 gold badges50 silver badges87 bronze badges




                  82k13 gold badges50 silver badges87 bronze badges


























                      1
















                      We can use rowsum from base R



                      rowsum(df$Response, as.integer(df$Time))
                      # [,1]
                      #1 3.0
                      #2 5.0
                      #3 20.4
                      #4 6.0
                      #5 24.0





                      share|improve this answer





























                        1
















                        We can use rowsum from base R



                        rowsum(df$Response, as.integer(df$Time))
                        # [,1]
                        #1 3.0
                        #2 5.0
                        #3 20.4
                        #4 6.0
                        #5 24.0





                        share|improve this answer



























                          1














                          1










                          1









                          We can use rowsum from base R



                          rowsum(df$Response, as.integer(df$Time))
                          # [,1]
                          #1 3.0
                          #2 5.0
                          #3 20.4
                          #4 6.0
                          #5 24.0





                          share|improve this answer













                          We can use rowsum from base R



                          rowsum(df$Response, as.integer(df$Time))
                          # [,1]
                          #1 3.0
                          #2 5.0
                          #3 20.4
                          #4 6.0
                          #5 24.0






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Mar 28 at 17:02









                          akrunakrun

                          466k15 gold badges261 silver badges343 bronze badges




                          466k15 gold badges261 silver badges343 bronze badges































                              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%2f55395339%2fsum-column-based-on-conditions-in-another-column-in-a-data-frame%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권, 지리지 충청도 공주목 은진현