How to use date as filter
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
- 2017
- 2017.01
- until 2017.01
r date dataframe matrix
add a comment |
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
- 2017
- 2017.01
- until 2017.01
r date dataframe matrix
add a comment |
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
- 2017
- 2017.01
- until 2017.01
r date dataframe matrix
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
- 2017
- 2017.01
- until 2017.01
r date dataframe matrix
r date dataframe matrix
asked Nov 19 '18 at 17:21
ConfusulumConfusulum
225
225
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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)
add a comment |
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)
Thank you a lot for explaining the integer-problem to me. I was not aware of that.
– Confusulum
Nov 20 '18 at 9:57
add a comment |
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"),])
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
add a comment |
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)
add a comment |
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)
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)
answered Nov 19 '18 at 17:37
Jake KauppJake Kaupp
5,62721428
5,62721428
add a comment |
add a comment |
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)
Thank you a lot for explaining the integer-problem to me. I was not aware of that.
– Confusulum
Nov 20 '18 at 9:57
add a comment |
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)
Thank you a lot for explaining the integer-problem to me. I was not aware of that.
– Confusulum
Nov 20 '18 at 9:57
add a comment |
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)
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)
answered Nov 19 '18 at 17:36
Rui BarradasRui Barradas
16.8k51730
16.8k51730
Thank you a lot for explaining the integer-problem to me. I was not aware of that.
– Confusulum
Nov 20 '18 at 9:57
add a comment |
Thank you a lot for explaining the integer-problem to me. I was not aware of that.
– Confusulum
Nov 20 '18 at 9:57
Thank you a lot for explaining the integer-problem to me. I was not aware of that.
– Confusulum
Nov 20 '18 at 9:57
Thank you a lot for explaining the integer-problem to me. I was not aware of that.
– Confusulum
Nov 20 '18 at 9:57
add a comment |
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"),])
add a comment |
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"),])
add a comment |
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"),])
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"),])
answered Nov 19 '18 at 17:37
iodiod
3,8332722
3,8332722
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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