Given two Year + 1/3/5 integers, generate a vector of all Year + 1/3/5 integers between them (inclusive)
My start on an example:
yr_min <- 20181
yr_max <- 20195
as.numeric(paste0(rep(
seq(as.numeric(substr(yr_min, 1, 4)),
as.numeric(substr(yr_max, 1, 4))),
each = 3),
c(1, 3, 5)))
[1] 20181 20183 20185 20191 20193 20195
What is wrong with the above code? It will not generalize beyond situations where yr_min ends in a 1 and yr_max ends in a 5.
For example:
yr_min <- 20183
yr_max <- 20193
as.numeric(paste0(rep(
seq(as.numeric(substr(yr_min, 1, 4)),
as.numeric(substr(yr_max, 1, 4))),
each = 3),
c(1, 3, 5)))
[1] 20181 20183 20185 20191 20193 20195
The desired output is
[1] 20183 20185 20191 20193
r
add a comment |
My start on an example:
yr_min <- 20181
yr_max <- 20195
as.numeric(paste0(rep(
seq(as.numeric(substr(yr_min, 1, 4)),
as.numeric(substr(yr_max, 1, 4))),
each = 3),
c(1, 3, 5)))
[1] 20181 20183 20185 20191 20193 20195
What is wrong with the above code? It will not generalize beyond situations where yr_min ends in a 1 and yr_max ends in a 5.
For example:
yr_min <- 20183
yr_max <- 20193
as.numeric(paste0(rep(
seq(as.numeric(substr(yr_min, 1, 4)),
as.numeric(substr(yr_max, 1, 4))),
each = 3),
c(1, 3, 5)))
[1] 20181 20183 20185 20191 20193 20195
The desired output is
[1] 20183 20185 20191 20193
r
It seems easier to just generate the sequence of base decades 20180, 20190... and add the offsets 1,3,5 yourself. Or else generate the entire sequence of yearsseq(yr_min, yr_max)andFilter()out anything which isn't 1,3,5 modulo 10.
– smci
Nov 19 '18 at 13:55
Oh, was this supposed to represent year-month like 2018 Jan, Mar, May, 2019 Jan, Mar, May? It would break on two-digit months, as @TimBiegeleisen suggests. (You didn't tell us so I just coded what you originally asked for)
– smci
Nov 19 '18 at 13:59
add a comment |
My start on an example:
yr_min <- 20181
yr_max <- 20195
as.numeric(paste0(rep(
seq(as.numeric(substr(yr_min, 1, 4)),
as.numeric(substr(yr_max, 1, 4))),
each = 3),
c(1, 3, 5)))
[1] 20181 20183 20185 20191 20193 20195
What is wrong with the above code? It will not generalize beyond situations where yr_min ends in a 1 and yr_max ends in a 5.
For example:
yr_min <- 20183
yr_max <- 20193
as.numeric(paste0(rep(
seq(as.numeric(substr(yr_min, 1, 4)),
as.numeric(substr(yr_max, 1, 4))),
each = 3),
c(1, 3, 5)))
[1] 20181 20183 20185 20191 20193 20195
The desired output is
[1] 20183 20185 20191 20193
r
My start on an example:
yr_min <- 20181
yr_max <- 20195
as.numeric(paste0(rep(
seq(as.numeric(substr(yr_min, 1, 4)),
as.numeric(substr(yr_max, 1, 4))),
each = 3),
c(1, 3, 5)))
[1] 20181 20183 20185 20191 20193 20195
What is wrong with the above code? It will not generalize beyond situations where yr_min ends in a 1 and yr_max ends in a 5.
For example:
yr_min <- 20183
yr_max <- 20193
as.numeric(paste0(rep(
seq(as.numeric(substr(yr_min, 1, 4)),
as.numeric(substr(yr_max, 1, 4))),
each = 3),
c(1, 3, 5)))
[1] 20181 20183 20185 20191 20193 20195
The desired output is
[1] 20183 20185 20191 20193
r
r
asked Nov 19 '18 at 13:48
ClarinetistClarinetist
234726
234726
It seems easier to just generate the sequence of base decades 20180, 20190... and add the offsets 1,3,5 yourself. Or else generate the entire sequence of yearsseq(yr_min, yr_max)andFilter()out anything which isn't 1,3,5 modulo 10.
– smci
Nov 19 '18 at 13:55
Oh, was this supposed to represent year-month like 2018 Jan, Mar, May, 2019 Jan, Mar, May? It would break on two-digit months, as @TimBiegeleisen suggests. (You didn't tell us so I just coded what you originally asked for)
– smci
Nov 19 '18 at 13:59
add a comment |
It seems easier to just generate the sequence of base decades 20180, 20190... and add the offsets 1,3,5 yourself. Or else generate the entire sequence of yearsseq(yr_min, yr_max)andFilter()out anything which isn't 1,3,5 modulo 10.
– smci
Nov 19 '18 at 13:55
Oh, was this supposed to represent year-month like 2018 Jan, Mar, May, 2019 Jan, Mar, May? It would break on two-digit months, as @TimBiegeleisen suggests. (You didn't tell us so I just coded what you originally asked for)
– smci
Nov 19 '18 at 13:59
It seems easier to just generate the sequence of base decades 20180, 20190... and add the offsets 1,3,5 yourself. Or else generate the entire sequence of years
seq(yr_min, yr_max) and Filter() out anything which isn't 1,3,5 modulo 10.– smci
Nov 19 '18 at 13:55
It seems easier to just generate the sequence of base decades 20180, 20190... and add the offsets 1,3,5 yourself. Or else generate the entire sequence of years
seq(yr_min, yr_max) and Filter() out anything which isn't 1,3,5 modulo 10.– smci
Nov 19 '18 at 13:55
Oh, was this supposed to represent year-month like 2018 Jan, Mar, May, 2019 Jan, Mar, May? It would break on two-digit months, as @TimBiegeleisen suggests. (You didn't tell us so I just coded what you originally asked for)
– smci
Nov 19 '18 at 13:59
Oh, was this supposed to represent year-month like 2018 Jan, Mar, May, 2019 Jan, Mar, May? It would break on two-digit months, as @TimBiegeleisen suggests. (You didn't tell us so I just coded what you originally asked for)
– smci
Nov 19 '18 at 13:59
add a comment |
3 Answers
3
active
oldest
votes
1) Generate the entire sequence of years then Filter() out anything which isn't 1,3,5 modulo 10.
rem_135_mod10 <- function(x) { (x %% 10) %in% c(1,3,5) }
Filter(rem_135_mod10, seq(yr_min, yr_max))
# 20181 20183 20185 20191 20193 20195
- This is functional programming in R with the constructs Filter(), Map(), Reduce()...)
- as the others say, for these specific remainders 1,3,5, you can exploit that you only need step 2:
seq(yr_min, yr_max, by=2)
or 2) generate the sequence of base decades 20180, 20190... and add the offsets 1,3,5 yourself. Same difference.
Before someone decides to tell me that I'm wrong for not choosing the highest-voted answer, I want to say that I'm actually really happy with this answer. It's a really clever use of modular arithmetic, and for collaboration, it is the easiest to read and modify.
– Clarinetist
Nov 19 '18 at 14:12
@Clarinetist: Thanks. More about functional programming in R:Filter(), Map(), Reduce()...
– smci
Nov 19 '18 at 14:22
As the others say, you can exploit that you only need step 2:seq(yr_min, yr_max, by=2)
– smci
Nov 19 '18 at 22:22
add a comment |
I would recommend using bona fide dates in your calculation. This allows us to take advantage of base R's seq function:
x <- seq(as.Date("2018/3/1"), as.Date("2019/3/1"), by="month")
x[format(x, "%m") %in% c("01", "03", "05")]
[1] "2018-03-01" "2018-05-01" "2019-01-01" "2019-03-01"
If you really want the exact format you have, you can easily do that with another call to format:
y <- x[format(x, "%m") %in% c("01", "03", "05")]
format(y, "%Y%m")
[1] "201803" "201805" "201901" "201903"
Or, for your exact output:
sub("(?<=\d{4}).", "", format(y, "%Y%m"), perl=TRUE)
[1] "20183" "20185" "20191" "20193"
This is -almost- perfect, but I cannot have the extra zero in there - i.e., the output needs to be20183,20185, etc.
– Clarinetist
Nov 19 '18 at 13:59
@Clarinetist I gave you an update, but I think that two digits is actually more appropriate, because perhaps at a later date you could have October through December, which are two digit months.
– Tim Biegeleisen
Nov 19 '18 at 14:03
I do not set any of the formatting that has been laid out here (this is for a SQL query). These numbers do not represent months, but rather, partitions of the year into three segments. Mathematically, the problem is laid out in the original question.
– Clarinetist
Nov 19 '18 at 14:04
1
I have an interest in SQL, and maybe you should add some details about that to your question. Note that most SQL databases have the ability to generate dates, so you might not even need R.
– Tim Biegeleisen
Nov 19 '18 at 14:05
I don't have access to a program like SQL Developer or SQL Server Management Studio in this situation for accessing these particular data, otherwise I would use it.
– Clarinetist
Nov 19 '18 at 14:08
add a comment |
predefined data:
end_vec <- c(1, 3, 5)
yr_min <- 20183
yr_max <- 20193
code:
ans<-
c(
sapply(c(yr_min, yr_max), function(x) {n<-nchar(x);as.numeric(paste0(substr(x,1,n-1),end_vec))})
)
ans[dplyr::between(ans, yr_min, yr_max)]
result:
# [1] 20183 20185 20191 20193
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%2f53376033%2fgiven-two-year-1-3-5-integers-generate-a-vector-of-all-year-1-3-5-integers%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
1) Generate the entire sequence of years then Filter() out anything which isn't 1,3,5 modulo 10.
rem_135_mod10 <- function(x) { (x %% 10) %in% c(1,3,5) }
Filter(rem_135_mod10, seq(yr_min, yr_max))
# 20181 20183 20185 20191 20193 20195
- This is functional programming in R with the constructs Filter(), Map(), Reduce()...)
- as the others say, for these specific remainders 1,3,5, you can exploit that you only need step 2:
seq(yr_min, yr_max, by=2)
or 2) generate the sequence of base decades 20180, 20190... and add the offsets 1,3,5 yourself. Same difference.
Before someone decides to tell me that I'm wrong for not choosing the highest-voted answer, I want to say that I'm actually really happy with this answer. It's a really clever use of modular arithmetic, and for collaboration, it is the easiest to read and modify.
– Clarinetist
Nov 19 '18 at 14:12
@Clarinetist: Thanks. More about functional programming in R:Filter(), Map(), Reduce()...
– smci
Nov 19 '18 at 14:22
As the others say, you can exploit that you only need step 2:seq(yr_min, yr_max, by=2)
– smci
Nov 19 '18 at 22:22
add a comment |
1) Generate the entire sequence of years then Filter() out anything which isn't 1,3,5 modulo 10.
rem_135_mod10 <- function(x) { (x %% 10) %in% c(1,3,5) }
Filter(rem_135_mod10, seq(yr_min, yr_max))
# 20181 20183 20185 20191 20193 20195
- This is functional programming in R with the constructs Filter(), Map(), Reduce()...)
- as the others say, for these specific remainders 1,3,5, you can exploit that you only need step 2:
seq(yr_min, yr_max, by=2)
or 2) generate the sequence of base decades 20180, 20190... and add the offsets 1,3,5 yourself. Same difference.
Before someone decides to tell me that I'm wrong for not choosing the highest-voted answer, I want to say that I'm actually really happy with this answer. It's a really clever use of modular arithmetic, and for collaboration, it is the easiest to read and modify.
– Clarinetist
Nov 19 '18 at 14:12
@Clarinetist: Thanks. More about functional programming in R:Filter(), Map(), Reduce()...
– smci
Nov 19 '18 at 14:22
As the others say, you can exploit that you only need step 2:seq(yr_min, yr_max, by=2)
– smci
Nov 19 '18 at 22:22
add a comment |
1) Generate the entire sequence of years then Filter() out anything which isn't 1,3,5 modulo 10.
rem_135_mod10 <- function(x) { (x %% 10) %in% c(1,3,5) }
Filter(rem_135_mod10, seq(yr_min, yr_max))
# 20181 20183 20185 20191 20193 20195
- This is functional programming in R with the constructs Filter(), Map(), Reduce()...)
- as the others say, for these specific remainders 1,3,5, you can exploit that you only need step 2:
seq(yr_min, yr_max, by=2)
or 2) generate the sequence of base decades 20180, 20190... and add the offsets 1,3,5 yourself. Same difference.
1) Generate the entire sequence of years then Filter() out anything which isn't 1,3,5 modulo 10.
rem_135_mod10 <- function(x) { (x %% 10) %in% c(1,3,5) }
Filter(rem_135_mod10, seq(yr_min, yr_max))
# 20181 20183 20185 20191 20193 20195
- This is functional programming in R with the constructs Filter(), Map(), Reduce()...)
- as the others say, for these specific remainders 1,3,5, you can exploit that you only need step 2:
seq(yr_min, yr_max, by=2)
or 2) generate the sequence of base decades 20180, 20190... and add the offsets 1,3,5 yourself. Same difference.
edited Nov 19 '18 at 22:20
answered Nov 19 '18 at 13:57
smcismci
14.8k672104
14.8k672104
Before someone decides to tell me that I'm wrong for not choosing the highest-voted answer, I want to say that I'm actually really happy with this answer. It's a really clever use of modular arithmetic, and for collaboration, it is the easiest to read and modify.
– Clarinetist
Nov 19 '18 at 14:12
@Clarinetist: Thanks. More about functional programming in R:Filter(), Map(), Reduce()...
– smci
Nov 19 '18 at 14:22
As the others say, you can exploit that you only need step 2:seq(yr_min, yr_max, by=2)
– smci
Nov 19 '18 at 22:22
add a comment |
Before someone decides to tell me that I'm wrong for not choosing the highest-voted answer, I want to say that I'm actually really happy with this answer. It's a really clever use of modular arithmetic, and for collaboration, it is the easiest to read and modify.
– Clarinetist
Nov 19 '18 at 14:12
@Clarinetist: Thanks. More about functional programming in R:Filter(), Map(), Reduce()...
– smci
Nov 19 '18 at 14:22
As the others say, you can exploit that you only need step 2:seq(yr_min, yr_max, by=2)
– smci
Nov 19 '18 at 22:22
Before someone decides to tell me that I'm wrong for not choosing the highest-voted answer, I want to say that I'm actually really happy with this answer. It's a really clever use of modular arithmetic, and for collaboration, it is the easiest to read and modify.
– Clarinetist
Nov 19 '18 at 14:12
Before someone decides to tell me that I'm wrong for not choosing the highest-voted answer, I want to say that I'm actually really happy with this answer. It's a really clever use of modular arithmetic, and for collaboration, it is the easiest to read and modify.
– Clarinetist
Nov 19 '18 at 14:12
@Clarinetist: Thanks. More about functional programming in R:
Filter(), Map(), Reduce()...– smci
Nov 19 '18 at 14:22
@Clarinetist: Thanks. More about functional programming in R:
Filter(), Map(), Reduce()...– smci
Nov 19 '18 at 14:22
As the others say, you can exploit that you only need step 2:
seq(yr_min, yr_max, by=2)– smci
Nov 19 '18 at 22:22
As the others say, you can exploit that you only need step 2:
seq(yr_min, yr_max, by=2)– smci
Nov 19 '18 at 22:22
add a comment |
I would recommend using bona fide dates in your calculation. This allows us to take advantage of base R's seq function:
x <- seq(as.Date("2018/3/1"), as.Date("2019/3/1"), by="month")
x[format(x, "%m") %in% c("01", "03", "05")]
[1] "2018-03-01" "2018-05-01" "2019-01-01" "2019-03-01"
If you really want the exact format you have, you can easily do that with another call to format:
y <- x[format(x, "%m") %in% c("01", "03", "05")]
format(y, "%Y%m")
[1] "201803" "201805" "201901" "201903"
Or, for your exact output:
sub("(?<=\d{4}).", "", format(y, "%Y%m"), perl=TRUE)
[1] "20183" "20185" "20191" "20193"
This is -almost- perfect, but I cannot have the extra zero in there - i.e., the output needs to be20183,20185, etc.
– Clarinetist
Nov 19 '18 at 13:59
@Clarinetist I gave you an update, but I think that two digits is actually more appropriate, because perhaps at a later date you could have October through December, which are two digit months.
– Tim Biegeleisen
Nov 19 '18 at 14:03
I do not set any of the formatting that has been laid out here (this is for a SQL query). These numbers do not represent months, but rather, partitions of the year into three segments. Mathematically, the problem is laid out in the original question.
– Clarinetist
Nov 19 '18 at 14:04
1
I have an interest in SQL, and maybe you should add some details about that to your question. Note that most SQL databases have the ability to generate dates, so you might not even need R.
– Tim Biegeleisen
Nov 19 '18 at 14:05
I don't have access to a program like SQL Developer or SQL Server Management Studio in this situation for accessing these particular data, otherwise I would use it.
– Clarinetist
Nov 19 '18 at 14:08
add a comment |
I would recommend using bona fide dates in your calculation. This allows us to take advantage of base R's seq function:
x <- seq(as.Date("2018/3/1"), as.Date("2019/3/1"), by="month")
x[format(x, "%m") %in% c("01", "03", "05")]
[1] "2018-03-01" "2018-05-01" "2019-01-01" "2019-03-01"
If you really want the exact format you have, you can easily do that with another call to format:
y <- x[format(x, "%m") %in% c("01", "03", "05")]
format(y, "%Y%m")
[1] "201803" "201805" "201901" "201903"
Or, for your exact output:
sub("(?<=\d{4}).", "", format(y, "%Y%m"), perl=TRUE)
[1] "20183" "20185" "20191" "20193"
This is -almost- perfect, but I cannot have the extra zero in there - i.e., the output needs to be20183,20185, etc.
– Clarinetist
Nov 19 '18 at 13:59
@Clarinetist I gave you an update, but I think that two digits is actually more appropriate, because perhaps at a later date you could have October through December, which are two digit months.
– Tim Biegeleisen
Nov 19 '18 at 14:03
I do not set any of the formatting that has been laid out here (this is for a SQL query). These numbers do not represent months, but rather, partitions of the year into three segments. Mathematically, the problem is laid out in the original question.
– Clarinetist
Nov 19 '18 at 14:04
1
I have an interest in SQL, and maybe you should add some details about that to your question. Note that most SQL databases have the ability to generate dates, so you might not even need R.
– Tim Biegeleisen
Nov 19 '18 at 14:05
I don't have access to a program like SQL Developer or SQL Server Management Studio in this situation for accessing these particular data, otherwise I would use it.
– Clarinetist
Nov 19 '18 at 14:08
add a comment |
I would recommend using bona fide dates in your calculation. This allows us to take advantage of base R's seq function:
x <- seq(as.Date("2018/3/1"), as.Date("2019/3/1"), by="month")
x[format(x, "%m") %in% c("01", "03", "05")]
[1] "2018-03-01" "2018-05-01" "2019-01-01" "2019-03-01"
If you really want the exact format you have, you can easily do that with another call to format:
y <- x[format(x, "%m") %in% c("01", "03", "05")]
format(y, "%Y%m")
[1] "201803" "201805" "201901" "201903"
Or, for your exact output:
sub("(?<=\d{4}).", "", format(y, "%Y%m"), perl=TRUE)
[1] "20183" "20185" "20191" "20193"
I would recommend using bona fide dates in your calculation. This allows us to take advantage of base R's seq function:
x <- seq(as.Date("2018/3/1"), as.Date("2019/3/1"), by="month")
x[format(x, "%m") %in% c("01", "03", "05")]
[1] "2018-03-01" "2018-05-01" "2019-01-01" "2019-03-01"
If you really want the exact format you have, you can easily do that with another call to format:
y <- x[format(x, "%m") %in% c("01", "03", "05")]
format(y, "%Y%m")
[1] "201803" "201805" "201901" "201903"
Or, for your exact output:
sub("(?<=\d{4}).", "", format(y, "%Y%m"), perl=TRUE)
[1] "20183" "20185" "20191" "20193"
edited Nov 19 '18 at 14:02
answered Nov 19 '18 at 13:56
Tim BiegeleisenTim Biegeleisen
221k1388141
221k1388141
This is -almost- perfect, but I cannot have the extra zero in there - i.e., the output needs to be20183,20185, etc.
– Clarinetist
Nov 19 '18 at 13:59
@Clarinetist I gave you an update, but I think that two digits is actually more appropriate, because perhaps at a later date you could have October through December, which are two digit months.
– Tim Biegeleisen
Nov 19 '18 at 14:03
I do not set any of the formatting that has been laid out here (this is for a SQL query). These numbers do not represent months, but rather, partitions of the year into three segments. Mathematically, the problem is laid out in the original question.
– Clarinetist
Nov 19 '18 at 14:04
1
I have an interest in SQL, and maybe you should add some details about that to your question. Note that most SQL databases have the ability to generate dates, so you might not even need R.
– Tim Biegeleisen
Nov 19 '18 at 14:05
I don't have access to a program like SQL Developer or SQL Server Management Studio in this situation for accessing these particular data, otherwise I would use it.
– Clarinetist
Nov 19 '18 at 14:08
add a comment |
This is -almost- perfect, but I cannot have the extra zero in there - i.e., the output needs to be20183,20185, etc.
– Clarinetist
Nov 19 '18 at 13:59
@Clarinetist I gave you an update, but I think that two digits is actually more appropriate, because perhaps at a later date you could have October through December, which are two digit months.
– Tim Biegeleisen
Nov 19 '18 at 14:03
I do not set any of the formatting that has been laid out here (this is for a SQL query). These numbers do not represent months, but rather, partitions of the year into three segments. Mathematically, the problem is laid out in the original question.
– Clarinetist
Nov 19 '18 at 14:04
1
I have an interest in SQL, and maybe you should add some details about that to your question. Note that most SQL databases have the ability to generate dates, so you might not even need R.
– Tim Biegeleisen
Nov 19 '18 at 14:05
I don't have access to a program like SQL Developer or SQL Server Management Studio in this situation for accessing these particular data, otherwise I would use it.
– Clarinetist
Nov 19 '18 at 14:08
This is -almost- perfect, but I cannot have the extra zero in there - i.e., the output needs to be
20183, 20185, etc.– Clarinetist
Nov 19 '18 at 13:59
This is -almost- perfect, but I cannot have the extra zero in there - i.e., the output needs to be
20183, 20185, etc.– Clarinetist
Nov 19 '18 at 13:59
@Clarinetist I gave you an update, but I think that two digits is actually more appropriate, because perhaps at a later date you could have October through December, which are two digit months.
– Tim Biegeleisen
Nov 19 '18 at 14:03
@Clarinetist I gave you an update, but I think that two digits is actually more appropriate, because perhaps at a later date you could have October through December, which are two digit months.
– Tim Biegeleisen
Nov 19 '18 at 14:03
I do not set any of the formatting that has been laid out here (this is for a SQL query). These numbers do not represent months, but rather, partitions of the year into three segments. Mathematically, the problem is laid out in the original question.
– Clarinetist
Nov 19 '18 at 14:04
I do not set any of the formatting that has been laid out here (this is for a SQL query). These numbers do not represent months, but rather, partitions of the year into three segments. Mathematically, the problem is laid out in the original question.
– Clarinetist
Nov 19 '18 at 14:04
1
1
I have an interest in SQL, and maybe you should add some details about that to your question. Note that most SQL databases have the ability to generate dates, so you might not even need R.
– Tim Biegeleisen
Nov 19 '18 at 14:05
I have an interest in SQL, and maybe you should add some details about that to your question. Note that most SQL databases have the ability to generate dates, so you might not even need R.
– Tim Biegeleisen
Nov 19 '18 at 14:05
I don't have access to a program like SQL Developer or SQL Server Management Studio in this situation for accessing these particular data, otherwise I would use it.
– Clarinetist
Nov 19 '18 at 14:08
I don't have access to a program like SQL Developer or SQL Server Management Studio in this situation for accessing these particular data, otherwise I would use it.
– Clarinetist
Nov 19 '18 at 14:08
add a comment |
predefined data:
end_vec <- c(1, 3, 5)
yr_min <- 20183
yr_max <- 20193
code:
ans<-
c(
sapply(c(yr_min, yr_max), function(x) {n<-nchar(x);as.numeric(paste0(substr(x,1,n-1),end_vec))})
)
ans[dplyr::between(ans, yr_min, yr_max)]
result:
# [1] 20183 20185 20191 20193
add a comment |
predefined data:
end_vec <- c(1, 3, 5)
yr_min <- 20183
yr_max <- 20193
code:
ans<-
c(
sapply(c(yr_min, yr_max), function(x) {n<-nchar(x);as.numeric(paste0(substr(x,1,n-1),end_vec))})
)
ans[dplyr::between(ans, yr_min, yr_max)]
result:
# [1] 20183 20185 20191 20193
add a comment |
predefined data:
end_vec <- c(1, 3, 5)
yr_min <- 20183
yr_max <- 20193
code:
ans<-
c(
sapply(c(yr_min, yr_max), function(x) {n<-nchar(x);as.numeric(paste0(substr(x,1,n-1),end_vec))})
)
ans[dplyr::between(ans, yr_min, yr_max)]
result:
# [1] 20183 20185 20191 20193
predefined data:
end_vec <- c(1, 3, 5)
yr_min <- 20183
yr_max <- 20193
code:
ans<-
c(
sapply(c(yr_min, yr_max), function(x) {n<-nchar(x);as.numeric(paste0(substr(x,1,n-1),end_vec))})
)
ans[dplyr::between(ans, yr_min, yr_max)]
result:
# [1] 20183 20185 20191 20193
answered Nov 19 '18 at 14:40
Andre ElricoAndre Elrico
5,66011028
5,66011028
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%2f53376033%2fgiven-two-year-1-3-5-integers-generate-a-vector-of-all-year-1-3-5-integers%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
It seems easier to just generate the sequence of base decades 20180, 20190... and add the offsets 1,3,5 yourself. Or else generate the entire sequence of years
seq(yr_min, yr_max)andFilter()out anything which isn't 1,3,5 modulo 10.– smci
Nov 19 '18 at 13:55
Oh, was this supposed to represent year-month like 2018 Jan, Mar, May, 2019 Jan, Mar, May? It would break on two-digit months, as @TimBiegeleisen suggests. (You didn't tell us so I just coded what you originally asked for)
– smci
Nov 19 '18 at 13:59