How to use date as filter











up vote
1
down vote

favorite












My knowledge of R and scripting in general is almost not existent. So I hope you will be patient with this basic question.



library(lubridate)
date.depature <- c("2016.06.16", "2016.11.16", "2017.01.05", "2017.01.12", "2017.02.25")
airport.departure <- c("CDG", "QNY", "QXO", "CDG", "QNY")
airport.arrival <- c("SYD", "CDG", "QNY", "SYD", "QXO")
amount <- c("1", "3", "1", "10", "5")
date.depature <- as_date(date.depature)
df <- data.frame(date.depature, airport.departure, airport.arrival, amount)

xtabs(as.integer(amount) ~ airport.arrival + airport.departure, df)


With this code we get the sum of the amount as a matrix with the airports as row/column. Now I need just the results for




  1. 2017

  2. 2017.01

  3. until 2017.01










share|improve this question


























    up vote
    1
    down vote

    favorite












    My knowledge of R and scripting in general is almost not existent. So I hope you will be patient with this basic question.



    library(lubridate)
    date.depature <- c("2016.06.16", "2016.11.16", "2017.01.05", "2017.01.12", "2017.02.25")
    airport.departure <- c("CDG", "QNY", "QXO", "CDG", "QNY")
    airport.arrival <- c("SYD", "CDG", "QNY", "SYD", "QXO")
    amount <- c("1", "3", "1", "10", "5")
    date.depature <- as_date(date.depature)
    df <- data.frame(date.depature, airport.departure, airport.arrival, amount)

    xtabs(as.integer(amount) ~ airport.arrival + airport.departure, df)


    With this code we get the sum of the amount as a matrix with the airports as row/column. Now I need just the results for




    1. 2017

    2. 2017.01

    3. until 2017.01










    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      My knowledge of R and scripting in general is almost not existent. So I hope you will be patient with this basic question.



      library(lubridate)
      date.depature <- c("2016.06.16", "2016.11.16", "2017.01.05", "2017.01.12", "2017.02.25")
      airport.departure <- c("CDG", "QNY", "QXO", "CDG", "QNY")
      airport.arrival <- c("SYD", "CDG", "QNY", "SYD", "QXO")
      amount <- c("1", "3", "1", "10", "5")
      date.depature <- as_date(date.depature)
      df <- data.frame(date.depature, airport.departure, airport.arrival, amount)

      xtabs(as.integer(amount) ~ airport.arrival + airport.departure, df)


      With this code we get the sum of the amount as a matrix with the airports as row/column. Now I need just the results for




      1. 2017

      2. 2017.01

      3. until 2017.01










      share|improve this question













      My knowledge of R and scripting in general is almost not existent. So I hope you will be patient with this basic question.



      library(lubridate)
      date.depature <- c("2016.06.16", "2016.11.16", "2017.01.05", "2017.01.12", "2017.02.25")
      airport.departure <- c("CDG", "QNY", "QXO", "CDG", "QNY")
      airport.arrival <- c("SYD", "CDG", "QNY", "SYD", "QXO")
      amount <- c("1", "3", "1", "10", "5")
      date.depature <- as_date(date.depature)
      df <- data.frame(date.depature, airport.departure, airport.arrival, amount)

      xtabs(as.integer(amount) ~ airport.arrival + airport.departure, df)


      With this code we get the sum of the amount as a matrix with the airports as row/column. Now I need just the results for




      1. 2017

      2. 2017.01

      3. until 2017.01







      r date dataframe matrix






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 at 17:21









      Confusulum

      104




      104
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          Since you're already using lubridate, I'm going to show you an approach using dplyr (part of the tidyverse alongside lubridate).



          The solutions all apply. filter alongside month, year and as_date functions from lubridate to create conditions to filter your data, then use the pipe %>% to pass that long to xtabs





          library(dplyr)
          #>
          #> Attaching package: 'dplyr'
          #> The following objects are masked from 'package:stats':
          #>
          #> filter, lag
          #> The following objects are masked from 'package:base':
          #>
          #> intersect, setdiff, setequal, union
          library(lubridate)
          #>
          #> Attaching package: 'lubridate'
          #> The following object is masked from 'package:base':
          #>
          #> date

          date.depature <- c("2016.06.16", "2016.11.16", "2017.01.05", "2017.01.12", "2017.02.25")
          airport.departure <- c("CDG", "QNY", "QXO", "CDG", "QNY")
          airport.arrival <- c("SYD", "CDG", "QNY", "SYD", "QXO")
          amount <- c("1", "3", "1", "10", "5")
          date.depature <- as_date(date.depature)
          df <- data.frame(date.depature, airport.departure, airport.arrival, amount)

          # For 2017
          df %>%
          filter(year(date.depature) == 2017) %>%
          xtabs(as.integer(amount) ~ airport.arrival + airport.departure, .)
          #> airport.departure
          #> airport.arrival CDG QNY QXO
          #> CDG 0 0 0
          #> QNY 0 0 1
          #> QXO 0 4 0
          #> SYD 2 0 0

          # 2017.01
          df %>%
          filter(year(date.depature) == 2017, month(date.depature) == 1) %>%
          xtabs(as.integer(amount) ~ airport.arrival + airport.departure, .)
          #> airport.departure
          #> airport.arrival CDG QNY QXO
          #> CDG 0 0 0
          #> QNY 0 0 1
          #> QXO 0 0 0
          #> SYD 2 0 0

          # until 2017.01
          df %>%
          filter(date.depature <= as_date("2017.01.01")) %>%
          xtabs(as.integer(amount) ~ airport.arrival + airport.departure, .)
          #> airport.departure
          #> airport.arrival CDG QNY QXO
          #> CDG 0 3 0
          #> QNY 0 0 0
          #> QXO 0 0 0
          #> SYD 1 0 0


          Created on 2018-11-19 by the reprex package (v0.2.1)






          share|improve this answer




























            up vote
            0
            down vote













            Why don't you coerce amount to class "integer" when you create df? Just get rid of the double quotes in



            amount <- c("1", "3", "1", "10", "5")


            or



            amount <- as.integer(c("1", "3", "1", "10", "5"))


            This is because as.integer(df$amount) does not return



            c(1, 3, 1, 10, 5)


            When you create the dataframe df that vector is coerced to class "factor" and what you now have is



            as.integer(df$amount)
            #[1] 1 3 1 2 4


            The right way would be



            as.integer(as.character(df$amount))
            #[1] 1 3 1 10 5


            Or more simply:



            date.depature <- c("2016.06.16", "2016.11.16", "2017.01.05", "2017.01.12", "2017.02.25")
            airport.departure <- c("CDG", "QNY", "QXO", "CDG", "QNY")
            airport.arrival <- c("SYD", "CDG", "QNY", "SYD", "QXO")
            amount <- c(1, 3, 1, 10, 5)
            date.depature <- as_date(date.depature)
            df <- data.frame(date.depature, airport.departure, airport.arrival, amount)


            Now the question.



            This is basically a subsetting problem.

            Subset the data extracting the years and months you want then run the same xtabs command.



            df1 <- df[year(df$date.depature) == 2017, ]
            df2 <- df1[month(df1$date.depature) == 1, ]
            df3 <- cbind(df[year(df$date.depature) < 2017, ], df2)


            Now xtabs, with the sub-dataframes above.



            xtabs(amount ~ airport.arrival + airport.departure, df1)
            xtabs(amount ~ airport.arrival + airport.departure, df2)
            xtabs(amount ~ airport.arrival + airport.departure, df3)





            share|improve this answer





















            • Thank you a lot for explaining the integer-problem to me. I was not aware of that.
              – Confusulum
              Nov 20 at 9:57


















            up vote
            0
            down vote













            You need to subset date.departure in your xtabs call. For year == 2017:



            xtabs(as.integer(amount) ~ airport.arrival + airport.departure, df[year(df$date.depature)==2017,])


            For year==2017 and month==1:



            xtabs(as.integer(amount) ~ airport.arrival + airport.departure, df[year(df$date.depature)==2017 & month(df$date.departure)==1,])


            And for anything before Jan 2017:



            xtabs(as.integer(amount) ~ airport.arrival + airport.departure, df[df$date.depature<as_date("2017-01-01"),])





            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',
              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%2f53379735%2fhow-to-use-date-as-filter%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








              up vote
              0
              down vote



              accepted










              Since you're already using lubridate, I'm going to show you an approach using dplyr (part of the tidyverse alongside lubridate).



              The solutions all apply. filter alongside month, year and as_date functions from lubridate to create conditions to filter your data, then use the pipe %>% to pass that long to xtabs





              library(dplyr)
              #>
              #> Attaching package: 'dplyr'
              #> The following objects are masked from 'package:stats':
              #>
              #> filter, lag
              #> The following objects are masked from 'package:base':
              #>
              #> intersect, setdiff, setequal, union
              library(lubridate)
              #>
              #> Attaching package: 'lubridate'
              #> The following object is masked from 'package:base':
              #>
              #> date

              date.depature <- c("2016.06.16", "2016.11.16", "2017.01.05", "2017.01.12", "2017.02.25")
              airport.departure <- c("CDG", "QNY", "QXO", "CDG", "QNY")
              airport.arrival <- c("SYD", "CDG", "QNY", "SYD", "QXO")
              amount <- c("1", "3", "1", "10", "5")
              date.depature <- as_date(date.depature)
              df <- data.frame(date.depature, airport.departure, airport.arrival, amount)

              # For 2017
              df %>%
              filter(year(date.depature) == 2017) %>%
              xtabs(as.integer(amount) ~ airport.arrival + airport.departure, .)
              #> airport.departure
              #> airport.arrival CDG QNY QXO
              #> CDG 0 0 0
              #> QNY 0 0 1
              #> QXO 0 4 0
              #> SYD 2 0 0

              # 2017.01
              df %>%
              filter(year(date.depature) == 2017, month(date.depature) == 1) %>%
              xtabs(as.integer(amount) ~ airport.arrival + airport.departure, .)
              #> airport.departure
              #> airport.arrival CDG QNY QXO
              #> CDG 0 0 0
              #> QNY 0 0 1
              #> QXO 0 0 0
              #> SYD 2 0 0

              # until 2017.01
              df %>%
              filter(date.depature <= as_date("2017.01.01")) %>%
              xtabs(as.integer(amount) ~ airport.arrival + airport.departure, .)
              #> airport.departure
              #> airport.arrival CDG QNY QXO
              #> CDG 0 3 0
              #> QNY 0 0 0
              #> QXO 0 0 0
              #> SYD 1 0 0


              Created on 2018-11-19 by the reprex package (v0.2.1)






              share|improve this answer

























                up vote
                0
                down vote



                accepted










                Since you're already using lubridate, I'm going to show you an approach using dplyr (part of the tidyverse alongside lubridate).



                The solutions all apply. filter alongside month, year and as_date functions from lubridate to create conditions to filter your data, then use the pipe %>% to pass that long to xtabs





                library(dplyr)
                #>
                #> Attaching package: 'dplyr'
                #> The following objects are masked from 'package:stats':
                #>
                #> filter, lag
                #> The following objects are masked from 'package:base':
                #>
                #> intersect, setdiff, setequal, union
                library(lubridate)
                #>
                #> Attaching package: 'lubridate'
                #> The following object is masked from 'package:base':
                #>
                #> date

                date.depature <- c("2016.06.16", "2016.11.16", "2017.01.05", "2017.01.12", "2017.02.25")
                airport.departure <- c("CDG", "QNY", "QXO", "CDG", "QNY")
                airport.arrival <- c("SYD", "CDG", "QNY", "SYD", "QXO")
                amount <- c("1", "3", "1", "10", "5")
                date.depature <- as_date(date.depature)
                df <- data.frame(date.depature, airport.departure, airport.arrival, amount)

                # For 2017
                df %>%
                filter(year(date.depature) == 2017) %>%
                xtabs(as.integer(amount) ~ airport.arrival + airport.departure, .)
                #> airport.departure
                #> airport.arrival CDG QNY QXO
                #> CDG 0 0 0
                #> QNY 0 0 1
                #> QXO 0 4 0
                #> SYD 2 0 0

                # 2017.01
                df %>%
                filter(year(date.depature) == 2017, month(date.depature) == 1) %>%
                xtabs(as.integer(amount) ~ airport.arrival + airport.departure, .)
                #> airport.departure
                #> airport.arrival CDG QNY QXO
                #> CDG 0 0 0
                #> QNY 0 0 1
                #> QXO 0 0 0
                #> SYD 2 0 0

                # until 2017.01
                df %>%
                filter(date.depature <= as_date("2017.01.01")) %>%
                xtabs(as.integer(amount) ~ airport.arrival + airport.departure, .)
                #> airport.departure
                #> airport.arrival CDG QNY QXO
                #> CDG 0 3 0
                #> QNY 0 0 0
                #> QXO 0 0 0
                #> SYD 1 0 0


                Created on 2018-11-19 by the reprex package (v0.2.1)






                share|improve this answer























                  up vote
                  0
                  down vote



                  accepted







                  up vote
                  0
                  down vote



                  accepted






                  Since you're already using lubridate, I'm going to show you an approach using dplyr (part of the tidyverse alongside lubridate).



                  The solutions all apply. filter alongside month, year and as_date functions from lubridate to create conditions to filter your data, then use the pipe %>% to pass that long to xtabs





                  library(dplyr)
                  #>
                  #> Attaching package: 'dplyr'
                  #> The following objects are masked from 'package:stats':
                  #>
                  #> filter, lag
                  #> The following objects are masked from 'package:base':
                  #>
                  #> intersect, setdiff, setequal, union
                  library(lubridate)
                  #>
                  #> Attaching package: 'lubridate'
                  #> The following object is masked from 'package:base':
                  #>
                  #> date

                  date.depature <- c("2016.06.16", "2016.11.16", "2017.01.05", "2017.01.12", "2017.02.25")
                  airport.departure <- c("CDG", "QNY", "QXO", "CDG", "QNY")
                  airport.arrival <- c("SYD", "CDG", "QNY", "SYD", "QXO")
                  amount <- c("1", "3", "1", "10", "5")
                  date.depature <- as_date(date.depature)
                  df <- data.frame(date.depature, airport.departure, airport.arrival, amount)

                  # For 2017
                  df %>%
                  filter(year(date.depature) == 2017) %>%
                  xtabs(as.integer(amount) ~ airport.arrival + airport.departure, .)
                  #> airport.departure
                  #> airport.arrival CDG QNY QXO
                  #> CDG 0 0 0
                  #> QNY 0 0 1
                  #> QXO 0 4 0
                  #> SYD 2 0 0

                  # 2017.01
                  df %>%
                  filter(year(date.depature) == 2017, month(date.depature) == 1) %>%
                  xtabs(as.integer(amount) ~ airport.arrival + airport.departure, .)
                  #> airport.departure
                  #> airport.arrival CDG QNY QXO
                  #> CDG 0 0 0
                  #> QNY 0 0 1
                  #> QXO 0 0 0
                  #> SYD 2 0 0

                  # until 2017.01
                  df %>%
                  filter(date.depature <= as_date("2017.01.01")) %>%
                  xtabs(as.integer(amount) ~ airport.arrival + airport.departure, .)
                  #> airport.departure
                  #> airport.arrival CDG QNY QXO
                  #> CDG 0 3 0
                  #> QNY 0 0 0
                  #> QXO 0 0 0
                  #> SYD 1 0 0


                  Created on 2018-11-19 by the reprex package (v0.2.1)






                  share|improve this answer












                  Since you're already using lubridate, I'm going to show you an approach using dplyr (part of the tidyverse alongside lubridate).



                  The solutions all apply. filter alongside month, year and as_date functions from lubridate to create conditions to filter your data, then use the pipe %>% to pass that long to xtabs





                  library(dplyr)
                  #>
                  #> Attaching package: 'dplyr'
                  #> The following objects are masked from 'package:stats':
                  #>
                  #> filter, lag
                  #> The following objects are masked from 'package:base':
                  #>
                  #> intersect, setdiff, setequal, union
                  library(lubridate)
                  #>
                  #> Attaching package: 'lubridate'
                  #> The following object is masked from 'package:base':
                  #>
                  #> date

                  date.depature <- c("2016.06.16", "2016.11.16", "2017.01.05", "2017.01.12", "2017.02.25")
                  airport.departure <- c("CDG", "QNY", "QXO", "CDG", "QNY")
                  airport.arrival <- c("SYD", "CDG", "QNY", "SYD", "QXO")
                  amount <- c("1", "3", "1", "10", "5")
                  date.depature <- as_date(date.depature)
                  df <- data.frame(date.depature, airport.departure, airport.arrival, amount)

                  # For 2017
                  df %>%
                  filter(year(date.depature) == 2017) %>%
                  xtabs(as.integer(amount) ~ airport.arrival + airport.departure, .)
                  #> airport.departure
                  #> airport.arrival CDG QNY QXO
                  #> CDG 0 0 0
                  #> QNY 0 0 1
                  #> QXO 0 4 0
                  #> SYD 2 0 0

                  # 2017.01
                  df %>%
                  filter(year(date.depature) == 2017, month(date.depature) == 1) %>%
                  xtabs(as.integer(amount) ~ airport.arrival + airport.departure, .)
                  #> airport.departure
                  #> airport.arrival CDG QNY QXO
                  #> CDG 0 0 0
                  #> QNY 0 0 1
                  #> QXO 0 0 0
                  #> SYD 2 0 0

                  # until 2017.01
                  df %>%
                  filter(date.depature <= as_date("2017.01.01")) %>%
                  xtabs(as.integer(amount) ~ airport.arrival + airport.departure, .)
                  #> airport.departure
                  #> airport.arrival CDG QNY QXO
                  #> CDG 0 3 0
                  #> QNY 0 0 0
                  #> QXO 0 0 0
                  #> SYD 1 0 0


                  Created on 2018-11-19 by the reprex package (v0.2.1)







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 19 at 17:37









                  Jake Kaupp

                  5,12221428




                  5,12221428
























                      up vote
                      0
                      down vote













                      Why don't you coerce amount to class "integer" when you create df? Just get rid of the double quotes in



                      amount <- c("1", "3", "1", "10", "5")


                      or



                      amount <- as.integer(c("1", "3", "1", "10", "5"))


                      This is because as.integer(df$amount) does not return



                      c(1, 3, 1, 10, 5)


                      When you create the dataframe df that vector is coerced to class "factor" and what you now have is



                      as.integer(df$amount)
                      #[1] 1 3 1 2 4


                      The right way would be



                      as.integer(as.character(df$amount))
                      #[1] 1 3 1 10 5


                      Or more simply:



                      date.depature <- c("2016.06.16", "2016.11.16", "2017.01.05", "2017.01.12", "2017.02.25")
                      airport.departure <- c("CDG", "QNY", "QXO", "CDG", "QNY")
                      airport.arrival <- c("SYD", "CDG", "QNY", "SYD", "QXO")
                      amount <- c(1, 3, 1, 10, 5)
                      date.depature <- as_date(date.depature)
                      df <- data.frame(date.depature, airport.departure, airport.arrival, amount)


                      Now the question.



                      This is basically a subsetting problem.

                      Subset the data extracting the years and months you want then run the same xtabs command.



                      df1 <- df[year(df$date.depature) == 2017, ]
                      df2 <- df1[month(df1$date.depature) == 1, ]
                      df3 <- cbind(df[year(df$date.depature) < 2017, ], df2)


                      Now xtabs, with the sub-dataframes above.



                      xtabs(amount ~ airport.arrival + airport.departure, df1)
                      xtabs(amount ~ airport.arrival + airport.departure, df2)
                      xtabs(amount ~ airport.arrival + airport.departure, df3)





                      share|improve this answer





















                      • Thank you a lot for explaining the integer-problem to me. I was not aware of that.
                        – Confusulum
                        Nov 20 at 9:57















                      up vote
                      0
                      down vote













                      Why don't you coerce amount to class "integer" when you create df? Just get rid of the double quotes in



                      amount <- c("1", "3", "1", "10", "5")


                      or



                      amount <- as.integer(c("1", "3", "1", "10", "5"))


                      This is because as.integer(df$amount) does not return



                      c(1, 3, 1, 10, 5)


                      When you create the dataframe df that vector is coerced to class "factor" and what you now have is



                      as.integer(df$amount)
                      #[1] 1 3 1 2 4


                      The right way would be



                      as.integer(as.character(df$amount))
                      #[1] 1 3 1 10 5


                      Or more simply:



                      date.depature <- c("2016.06.16", "2016.11.16", "2017.01.05", "2017.01.12", "2017.02.25")
                      airport.departure <- c("CDG", "QNY", "QXO", "CDG", "QNY")
                      airport.arrival <- c("SYD", "CDG", "QNY", "SYD", "QXO")
                      amount <- c(1, 3, 1, 10, 5)
                      date.depature <- as_date(date.depature)
                      df <- data.frame(date.depature, airport.departure, airport.arrival, amount)


                      Now the question.



                      This is basically a subsetting problem.

                      Subset the data extracting the years and months you want then run the same xtabs command.



                      df1 <- df[year(df$date.depature) == 2017, ]
                      df2 <- df1[month(df1$date.depature) == 1, ]
                      df3 <- cbind(df[year(df$date.depature) < 2017, ], df2)


                      Now xtabs, with the sub-dataframes above.



                      xtabs(amount ~ airport.arrival + airport.departure, df1)
                      xtabs(amount ~ airport.arrival + airport.departure, df2)
                      xtabs(amount ~ airport.arrival + airport.departure, df3)





                      share|improve this answer





















                      • Thank you a lot for explaining the integer-problem to me. I was not aware of that.
                        – Confusulum
                        Nov 20 at 9:57













                      up vote
                      0
                      down vote










                      up vote
                      0
                      down vote









                      Why don't you coerce amount to class "integer" when you create df? Just get rid of the double quotes in



                      amount <- c("1", "3", "1", "10", "5")


                      or



                      amount <- as.integer(c("1", "3", "1", "10", "5"))


                      This is because as.integer(df$amount) does not return



                      c(1, 3, 1, 10, 5)


                      When you create the dataframe df that vector is coerced to class "factor" and what you now have is



                      as.integer(df$amount)
                      #[1] 1 3 1 2 4


                      The right way would be



                      as.integer(as.character(df$amount))
                      #[1] 1 3 1 10 5


                      Or more simply:



                      date.depature <- c("2016.06.16", "2016.11.16", "2017.01.05", "2017.01.12", "2017.02.25")
                      airport.departure <- c("CDG", "QNY", "QXO", "CDG", "QNY")
                      airport.arrival <- c("SYD", "CDG", "QNY", "SYD", "QXO")
                      amount <- c(1, 3, 1, 10, 5)
                      date.depature <- as_date(date.depature)
                      df <- data.frame(date.depature, airport.departure, airport.arrival, amount)


                      Now the question.



                      This is basically a subsetting problem.

                      Subset the data extracting the years and months you want then run the same xtabs command.



                      df1 <- df[year(df$date.depature) == 2017, ]
                      df2 <- df1[month(df1$date.depature) == 1, ]
                      df3 <- cbind(df[year(df$date.depature) < 2017, ], df2)


                      Now xtabs, with the sub-dataframes above.



                      xtabs(amount ~ airport.arrival + airport.departure, df1)
                      xtabs(amount ~ airport.arrival + airport.departure, df2)
                      xtabs(amount ~ airport.arrival + airport.departure, df3)





                      share|improve this answer












                      Why don't you coerce amount to class "integer" when you create df? Just get rid of the double quotes in



                      amount <- c("1", "3", "1", "10", "5")


                      or



                      amount <- as.integer(c("1", "3", "1", "10", "5"))


                      This is because as.integer(df$amount) does not return



                      c(1, 3, 1, 10, 5)


                      When you create the dataframe df that vector is coerced to class "factor" and what you now have is



                      as.integer(df$amount)
                      #[1] 1 3 1 2 4


                      The right way would be



                      as.integer(as.character(df$amount))
                      #[1] 1 3 1 10 5


                      Or more simply:



                      date.depature <- c("2016.06.16", "2016.11.16", "2017.01.05", "2017.01.12", "2017.02.25")
                      airport.departure <- c("CDG", "QNY", "QXO", "CDG", "QNY")
                      airport.arrival <- c("SYD", "CDG", "QNY", "SYD", "QXO")
                      amount <- c(1, 3, 1, 10, 5)
                      date.depature <- as_date(date.depature)
                      df <- data.frame(date.depature, airport.departure, airport.arrival, amount)


                      Now the question.



                      This is basically a subsetting problem.

                      Subset the data extracting the years and months you want then run the same xtabs command.



                      df1 <- df[year(df$date.depature) == 2017, ]
                      df2 <- df1[month(df1$date.depature) == 1, ]
                      df3 <- cbind(df[year(df$date.depature) < 2017, ], df2)


                      Now xtabs, with the sub-dataframes above.



                      xtabs(amount ~ airport.arrival + airport.departure, df1)
                      xtabs(amount ~ airport.arrival + airport.departure, df2)
                      xtabs(amount ~ airport.arrival + airport.departure, df3)






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 19 at 17:36









                      Rui Barradas

                      15.3k31730




                      15.3k31730












                      • Thank you a lot for explaining the integer-problem to me. I was not aware of that.
                        – Confusulum
                        Nov 20 at 9:57


















                      • Thank you a lot for explaining the integer-problem to me. I was not aware of that.
                        – Confusulum
                        Nov 20 at 9:57
















                      Thank you a lot for explaining the integer-problem to me. I was not aware of that.
                      – Confusulum
                      Nov 20 at 9:57




                      Thank you a lot for explaining the integer-problem to me. I was not aware of that.
                      – Confusulum
                      Nov 20 at 9:57










                      up vote
                      0
                      down vote













                      You need to subset date.departure in your xtabs call. For year == 2017:



                      xtabs(as.integer(amount) ~ airport.arrival + airport.departure, df[year(df$date.depature)==2017,])


                      For year==2017 and month==1:



                      xtabs(as.integer(amount) ~ airport.arrival + airport.departure, df[year(df$date.depature)==2017 & month(df$date.departure)==1,])


                      And for anything before Jan 2017:



                      xtabs(as.integer(amount) ~ airport.arrival + airport.departure, df[df$date.depature<as_date("2017-01-01"),])





                      share|improve this answer

























                        up vote
                        0
                        down vote













                        You need to subset date.departure in your xtabs call. For year == 2017:



                        xtabs(as.integer(amount) ~ airport.arrival + airport.departure, df[year(df$date.depature)==2017,])


                        For year==2017 and month==1:



                        xtabs(as.integer(amount) ~ airport.arrival + airport.departure, df[year(df$date.depature)==2017 & month(df$date.departure)==1,])


                        And for anything before Jan 2017:



                        xtabs(as.integer(amount) ~ airport.arrival + airport.departure, df[df$date.depature<as_date("2017-01-01"),])





                        share|improve this answer























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          You need to subset date.departure in your xtabs call. For year == 2017:



                          xtabs(as.integer(amount) ~ airport.arrival + airport.departure, df[year(df$date.depature)==2017,])


                          For year==2017 and month==1:



                          xtabs(as.integer(amount) ~ airport.arrival + airport.departure, df[year(df$date.depature)==2017 & month(df$date.departure)==1,])


                          And for anything before Jan 2017:



                          xtabs(as.integer(amount) ~ airport.arrival + airport.departure, df[df$date.depature<as_date("2017-01-01"),])





                          share|improve this answer












                          You need to subset date.departure in your xtabs call. For year == 2017:



                          xtabs(as.integer(amount) ~ airport.arrival + airport.departure, df[year(df$date.depature)==2017,])


                          For year==2017 and month==1:



                          xtabs(as.integer(amount) ~ airport.arrival + airport.departure, df[year(df$date.depature)==2017 & month(df$date.departure)==1,])


                          And for anything before Jan 2017:



                          xtabs(as.integer(amount) ~ airport.arrival + airport.departure, df[df$date.depature<as_date("2017-01-01"),])






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 19 at 17:37









                          iod

                          3,3741620




                          3,3741620






























                              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.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • 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%2f53379735%2fhow-to-use-date-as-filter%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

                              Costa Masnaga

                              Fotorealismo

                              Sidney Franklin