How to obtain a position of last non-zero element
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I've got a binary variable representing if event happened or not:
event <- c(0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0)
I need to obtain a variable that would indicate the time when the last event happened. The expected output would be:
last_event <- c(0, 0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 13, 13, 13, 13)
How can I obtain that with base R, tidyverse or any other way?
r tidyverse base
add a comment |
I've got a binary variable representing if event happened or not:
event <- c(0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0)
I need to obtain a variable that would indicate the time when the last event happened. The expected output would be:
last_event <- c(0, 0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 13, 13, 13, 13)
How can I obtain that with base R, tidyverse or any other way?
r tidyverse base
add a comment |
I've got a binary variable representing if event happened or not:
event <- c(0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0)
I need to obtain a variable that would indicate the time when the last event happened. The expected output would be:
last_event <- c(0, 0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 13, 13, 13, 13)
How can I obtain that with base R, tidyverse or any other way?
r tidyverse base
I've got a binary variable representing if event happened or not:
event <- c(0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0)
I need to obtain a variable that would indicate the time when the last event happened. The expected output would be:
last_event <- c(0, 0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 13, 13, 13, 13)
How can I obtain that with base R, tidyverse or any other way?
r tidyverse base
r tidyverse base
asked yesterday
jakesjakes
431315
431315
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
Taking advantage of the fact that you have a binary vector, the following gives your desired output:
cummax(seq_along(event) * event)
6
Yes! So much more elegant than my solution. I was thinking about cumulative sums but I didn’t think of multiplying the indices by the binary vector.
– Konrad Rudolph
yesterday
3
or without multiplicationcummax(ifelse(event, seq_along(event), 0))
– jogo
yesterday
@jogo That solution makes sense if the type ofevent
islogical
. It does work even for a numeric vector due to R’s implicit conversions but … eh.
– Konrad Rudolph
yesterday
add a comment |
Whenever you need to fill repetitions with a value, think run-length encoding.
In this case, you can determine the run lengths and then repeat the indices of count == 0
an according number of times:
lengths = rle(event == 0)$lengths
nonzeros = which(event != 0)
runs = c(0, rep(nonzeros, each = 2))
result = rep(runs, lengths)
Alternative, substitute the runs in the RLE and then inverse it:
rle = rle(event == 0)
nonzeros = which(event != 0)
rle$values = c(0, rep(nonzeros, each = 2))
result = inverse.rle(rle)
add a comment |
You can also do somthing like this-
> zero.locf <- function(x) {
v <- x!=0
c(0, x[v])[cumsum(v)+1]
}
> zero.locf(1:length(event)*event)
[1] 0 0 0 0 5 5 5 5 5 5 5 5 13 13 13 13
add a comment |
Another option is to find the index where event == 1
and repeat it based on length
.
rep(c(0, which(event == 1)), tapply(event, cumsum(event == 1), length))
#[1] 0 0 0 0 5 5 5 5 5 5 5 5 13 13 13 13
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%2f55634527%2fhow-to-obtain-a-position-of-last-non-zero-element%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Taking advantage of the fact that you have a binary vector, the following gives your desired output:
cummax(seq_along(event) * event)
6
Yes! So much more elegant than my solution. I was thinking about cumulative sums but I didn’t think of multiplying the indices by the binary vector.
– Konrad Rudolph
yesterday
3
or without multiplicationcummax(ifelse(event, seq_along(event), 0))
– jogo
yesterday
@jogo That solution makes sense if the type ofevent
islogical
. It does work even for a numeric vector due to R’s implicit conversions but … eh.
– Konrad Rudolph
yesterday
add a comment |
Taking advantage of the fact that you have a binary vector, the following gives your desired output:
cummax(seq_along(event) * event)
6
Yes! So much more elegant than my solution. I was thinking about cumulative sums but I didn’t think of multiplying the indices by the binary vector.
– Konrad Rudolph
yesterday
3
or without multiplicationcummax(ifelse(event, seq_along(event), 0))
– jogo
yesterday
@jogo That solution makes sense if the type ofevent
islogical
. It does work even for a numeric vector due to R’s implicit conversions but … eh.
– Konrad Rudolph
yesterday
add a comment |
Taking advantage of the fact that you have a binary vector, the following gives your desired output:
cummax(seq_along(event) * event)
Taking advantage of the fact that you have a binary vector, the following gives your desired output:
cummax(seq_along(event) * event)
answered yesterday
mgiormentimgiormenti
394111
394111
6
Yes! So much more elegant than my solution. I was thinking about cumulative sums but I didn’t think of multiplying the indices by the binary vector.
– Konrad Rudolph
yesterday
3
or without multiplicationcummax(ifelse(event, seq_along(event), 0))
– jogo
yesterday
@jogo That solution makes sense if the type ofevent
islogical
. It does work even for a numeric vector due to R’s implicit conversions but … eh.
– Konrad Rudolph
yesterday
add a comment |
6
Yes! So much more elegant than my solution. I was thinking about cumulative sums but I didn’t think of multiplying the indices by the binary vector.
– Konrad Rudolph
yesterday
3
or without multiplicationcummax(ifelse(event, seq_along(event), 0))
– jogo
yesterday
@jogo That solution makes sense if the type ofevent
islogical
. It does work even for a numeric vector due to R’s implicit conversions but … eh.
– Konrad Rudolph
yesterday
6
6
Yes! So much more elegant than my solution. I was thinking about cumulative sums but I didn’t think of multiplying the indices by the binary vector.
– Konrad Rudolph
yesterday
Yes! So much more elegant than my solution. I was thinking about cumulative sums but I didn’t think of multiplying the indices by the binary vector.
– Konrad Rudolph
yesterday
3
3
or without multiplication
cummax(ifelse(event, seq_along(event), 0))
– jogo
yesterday
or without multiplication
cummax(ifelse(event, seq_along(event), 0))
– jogo
yesterday
@jogo That solution makes sense if the type of
event
is logical
. It does work even for a numeric vector due to R’s implicit conversions but … eh.– Konrad Rudolph
yesterday
@jogo That solution makes sense if the type of
event
is logical
. It does work even for a numeric vector due to R’s implicit conversions but … eh.– Konrad Rudolph
yesterday
add a comment |
Whenever you need to fill repetitions with a value, think run-length encoding.
In this case, you can determine the run lengths and then repeat the indices of count == 0
an according number of times:
lengths = rle(event == 0)$lengths
nonzeros = which(event != 0)
runs = c(0, rep(nonzeros, each = 2))
result = rep(runs, lengths)
Alternative, substitute the runs in the RLE and then inverse it:
rle = rle(event == 0)
nonzeros = which(event != 0)
rle$values = c(0, rep(nonzeros, each = 2))
result = inverse.rle(rle)
add a comment |
Whenever you need to fill repetitions with a value, think run-length encoding.
In this case, you can determine the run lengths and then repeat the indices of count == 0
an according number of times:
lengths = rle(event == 0)$lengths
nonzeros = which(event != 0)
runs = c(0, rep(nonzeros, each = 2))
result = rep(runs, lengths)
Alternative, substitute the runs in the RLE and then inverse it:
rle = rle(event == 0)
nonzeros = which(event != 0)
rle$values = c(0, rep(nonzeros, each = 2))
result = inverse.rle(rle)
add a comment |
Whenever you need to fill repetitions with a value, think run-length encoding.
In this case, you can determine the run lengths and then repeat the indices of count == 0
an according number of times:
lengths = rle(event == 0)$lengths
nonzeros = which(event != 0)
runs = c(0, rep(nonzeros, each = 2))
result = rep(runs, lengths)
Alternative, substitute the runs in the RLE and then inverse it:
rle = rle(event == 0)
nonzeros = which(event != 0)
rle$values = c(0, rep(nonzeros, each = 2))
result = inverse.rle(rle)
Whenever you need to fill repetitions with a value, think run-length encoding.
In this case, you can determine the run lengths and then repeat the indices of count == 0
an according number of times:
lengths = rle(event == 0)$lengths
nonzeros = which(event != 0)
runs = c(0, rep(nonzeros, each = 2))
result = rep(runs, lengths)
Alternative, substitute the runs in the RLE and then inverse it:
rle = rle(event == 0)
nonzeros = which(event != 0)
rle$values = c(0, rep(nonzeros, each = 2))
result = inverse.rle(rle)
answered yesterday
Konrad RudolphKonrad Rudolph
404k1017921041
404k1017921041
add a comment |
add a comment |
You can also do somthing like this-
> zero.locf <- function(x) {
v <- x!=0
c(0, x[v])[cumsum(v)+1]
}
> zero.locf(1:length(event)*event)
[1] 0 0 0 0 5 5 5 5 5 5 5 5 13 13 13 13
add a comment |
You can also do somthing like this-
> zero.locf <- function(x) {
v <- x!=0
c(0, x[v])[cumsum(v)+1]
}
> zero.locf(1:length(event)*event)
[1] 0 0 0 0 5 5 5 5 5 5 5 5 13 13 13 13
add a comment |
You can also do somthing like this-
> zero.locf <- function(x) {
v <- x!=0
c(0, x[v])[cumsum(v)+1]
}
> zero.locf(1:length(event)*event)
[1] 0 0 0 0 5 5 5 5 5 5 5 5 13 13 13 13
You can also do somthing like this-
> zero.locf <- function(x) {
v <- x!=0
c(0, x[v])[cumsum(v)+1]
}
> zero.locf(1:length(event)*event)
[1] 0 0 0 0 5 5 5 5 5 5 5 5 13 13 13 13
answered yesterday
RushabhRushabh
1,299221
1,299221
add a comment |
add a comment |
Another option is to find the index where event == 1
and repeat it based on length
.
rep(c(0, which(event == 1)), tapply(event, cumsum(event == 1), length))
#[1] 0 0 0 0 5 5 5 5 5 5 5 5 13 13 13 13
add a comment |
Another option is to find the index where event == 1
and repeat it based on length
.
rep(c(0, which(event == 1)), tapply(event, cumsum(event == 1), length))
#[1] 0 0 0 0 5 5 5 5 5 5 5 5 13 13 13 13
add a comment |
Another option is to find the index where event == 1
and repeat it based on length
.
rep(c(0, which(event == 1)), tapply(event, cumsum(event == 1), length))
#[1] 0 0 0 0 5 5 5 5 5 5 5 5 13 13 13 13
Another option is to find the index where event == 1
and repeat it based on length
.
rep(c(0, which(event == 1)), tapply(event, cumsum(event == 1), length))
#[1] 0 0 0 0 5 5 5 5 5 5 5 5 13 13 13 13
answered yesterday
Ronak ShahRonak Shah
46.6k104269
46.6k104269
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%2f55634527%2fhow-to-obtain-a-position-of-last-non-zero-element%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