Identifying positions of the last TRUEs in a sequence of TRUEs and FALSEsCorrect order for control structure logic (true/false, false/true)?check a vector and output a singular TRUE or FALSE valuedata.table vs dplyr: can one do something well the other can't or does poorly?change beg or end of an object containing TRUE and FALSEidentify time sequence in data and subset by that sequence rR - filtering Matrix based off True/False vectorR find maximum sequence length and positionCompare two columns for string and print “TRUE” or “FALSE”Place an annotation onto a ggplot with TRUE and FALSE on the y-axisVectors & Ifelse Logic - Won't Populate Vector
My previous employer committed a severe violation of the law and is also being sued by me. How do I explain the situation to future employers?
How does one acquire an undead eyeball encased in a gem?
Why the Cauchy Distribution is so useful?
Perl regex matching apostrophe fails
Why won't the U.S. sign a peace treaty with North Korea?
What was the profession 芸者 (female entertainer) called in Russia?
Delete elements less than the last largest element
How to "add vert" in blender 2.8?
How do I explain that I don't want to maintain old projects?
What are the consequences for a developed nation to not accept any refugees?
How many Jimmys can fit?
How to understand flavors and when to use combination of them?
How should I ask for a "pint" in countries that use metric?
Can Jimmy hang on his rope?
Why do people prefer metropolitan areas, considering monsters and villains?
Can a landlord force all residents to use the landlord's in-house debit card accounts?
Computer name naming convention for security
Is there an In-Universe reason why Thor and the Asgardians think Rocket is a rabbit?
How do I separate enchants from items?
Strong Password Detection in Python
Would denouncing cheaters from an exam make me less likely to receive penalties?
Why am I getting unevenly-spread results when using $RANDOM?
Wires do not connect in Circuitikz
Why did Robert F. Kennedy loathe Lyndon B. Johnson?
Identifying positions of the last TRUEs in a sequence of TRUEs and FALSEs
Correct order for control structure logic (true/false, false/true)?check a vector and output a singular TRUE or FALSE valuedata.table vs dplyr: can one do something well the other can't or does poorly?change beg or end of an object containing TRUE and FALSEidentify time sequence in data and subset by that sequence rR - filtering Matrix based off True/False vectorR find maximum sequence length and positionCompare two columns for string and print “TRUE” or “FALSE”Place an annotation onto a ggplot with TRUE and FALSE on the y-axisVectors & Ifelse Logic - Won't Populate Vector
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have a vector of TRUEs and FALSEs:
x <- c(F,F,F,T,T,T,F,F,F,T,T,T,F,T,T)
I'd like to elegantly (and in base) identify the position of the last TRUE before it changes to FALSE.
The following works, though, it seems like it could be simplified:
c((x[-1] != x[-length(x)]),T) & x
> FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE
Input and output:
r vector logic cumsum
add a comment |
I have a vector of TRUEs and FALSEs:
x <- c(F,F,F,T,T,T,F,F,F,T,T,T,F,T,T)
I'd like to elegantly (and in base) identify the position of the last TRUE before it changes to FALSE.
The following works, though, it seems like it could be simplified:
c((x[-1] != x[-length(x)]),T) & x
> FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE
Input and output:
r vector logic cumsum
add a comment |
I have a vector of TRUEs and FALSEs:
x <- c(F,F,F,T,T,T,F,F,F,T,T,T,F,T,T)
I'd like to elegantly (and in base) identify the position of the last TRUE before it changes to FALSE.
The following works, though, it seems like it could be simplified:
c((x[-1] != x[-length(x)]),T) & x
> FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE
Input and output:
r vector logic cumsum
I have a vector of TRUEs and FALSEs:
x <- c(F,F,F,T,T,T,F,F,F,T,T,T,F,T,T)
I'd like to elegantly (and in base) identify the position of the last TRUE before it changes to FALSE.
The following works, though, it seems like it could be simplified:
c((x[-1] != x[-length(x)]),T) & x
> FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE
Input and output:
r vector logic cumsum
r vector logic cumsum
edited Jun 29 at 22:47
Khaynes
asked Jun 29 at 2:41
KhaynesKhaynes
8818 silver badges22 bronze badges
8818 silver badges22 bronze badges
add a comment |
add a comment |
7 Answers
7
active
oldest
votes
We may look where x
is greater than shifted x
with 0
appended.
x>c(x[-1],0)
# [1] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE
add a comment |
Taking advantage of diff
with an appended FALSE
to catch the implied TRUE
-to-FALSE
at the end.
diff(c(x,FALSE)) == -1
# [1] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE
#[13] FALSE FALSE TRUE
3
More golfydiff(c(x,0))<0
.
– jay.sf
Jun 29 at 6:37
1
@jay.sf - all good by me.
– thelatemail
Jun 29 at 7:19
add a comment |
Check rle
rlex = rle(x)
end = cumsum(rlex$lengths)
x&(seq(length(x)) %in% end)
[1] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE
Another layout suggested by Frank
seq_along(x) %in% with(rle(x), cumsum(lengths)[values])
[1] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE
add a comment |
Another version with rle
x[setdiff(seq_along(x), with(rle(x), cumsum(lengths) * values))] <- FALSE
x
#[1] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE
add a comment |
An option with duplicated
library(data.table)
!duplicated(rleid(x), fromLast = TRUE) & x
#[1] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE
add a comment |
benchmarks
Thanks for all the solutions. If anyone is interested in benchmarks:
library(dplyr)
library(data.table)
set.seed(1)
x <- sample(c(TRUE, FALSE), 1000000, replace = T)
y <- data.frame(x = x) # For M. Viking's solution
x_dt <- x # For Ronak Shah's solution
microbenchmark::microbenchmark(Khaynes = Khaynes <- c((x[-1] != x[-length(x)]),T) & x,
jay.sf = jay.sf <- x>c(x[-1],0),
jay.sf_2 = jay.sf_2 <- diff(c(x,0))<0,
thelatemail = thelatemail <- diff(c(x,FALSE)) == -1,
WeNYoBen = rlex = rle(x); end = cumsum(rlex$lengths); WeNYoBen <- x&(seq(length(x)) %in% end),
M._Viking = M._Viking <- y %>% mutate(lasttrue = case_when(x > lead(x) ~ T, T ~ F)),
akrun = akrun <- !duplicated(rleid(x), fromLast = TRUE) & x,
frank = frank <- seq_along(x) %in% with(rle(x), cumsum(lengths)[values]),
Ronak_Shah = x_dt[setdiff(seq_along(x_dt), with(rle(x_dt), cumsum(lengths) * values))] <- FALSE,
times = 50)
# Output:
# Unit: milliseconds
# expr min lq mean median uq max neval
# Khaynes 23.0283 26.5010 31.76180 31.71290 37.1449 46.3824 50
# jay.sf 13.0630 13.5373 17.84056 13.77135 20.5462 73.5926 50
# jay.sf_2 26.1960 27.7653 35.25296 36.39615 39.3686 61.8858 50
# thelatemail 24.8204 26.7178 32.51675 33.50165 36.6328 41.9279 50
# WeNYoBen 83.9070 98.4700 107.79965 101.88475 107.1933 170.2940 50
# M._Viking 73.5963 83.4467 93.99603 86.58535 94.0915 151.7075 50
# akrun 42.5265 43.2879 48.42697 44.98085 51.1533 105.2836 50
# frank 81.9115 90.1559 95.40261 93.97015 98.2921 129.6162 50
# Ronak_Shah 109.0678 121.8230 133.10690 125.63930 133.7222 231.5350 50
all.equal(Khaynes, jay.sf)
all.equal(Khaynes, jay.sf_2)
all.equal(Khaynes, thelatemail)
all.equal(Khaynes, WeNYoBen)
all.equal(Khaynes, M._Viking$lasttrue) # When the last element is TRUE it will return false.
all.equal(Khaynes, akrun)
all.equal(Khaynes, frank)
all.equal(Khaynes, x_dt) # Ronak Shah solution.
2
Nice follow up. @jay.sf 'sx>c(x[-1],0)
is the winner.
– M. Viking
Jun 30 at 0:34
add a comment |
Non-base
solution for identifying the last TRUE
before a FALSE
.
library(dplyr)
y <- data.frame(x = c(FALSE,FALSE,FALSE,TRUE,TRUE,TRUE,FALSE,FALSE,
FALSE,TRUE,TRUE,TRUE,FALSE,TRUE,TRUE))
y %>%
mutate(lasttrue = case_when(x == TRUE & lead(x) == FALSE ~ TRUE,
TRUE ~ FALSE))
Edit:
y %>%
mutate(lasttrue = case_when(x > lead(x) ~ T,
T ~ F))
Returns the wrong result with the last two elements are FALSE, TRUE
– Khaynes
Jun 29 at 21:02
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%2f56814732%2fidentifying-positions-of-the-last-trues-in-a-sequence-of-trues-and-falses%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
We may look where x
is greater than shifted x
with 0
appended.
x>c(x[-1],0)
# [1] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE
add a comment |
We may look where x
is greater than shifted x
with 0
appended.
x>c(x[-1],0)
# [1] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE
add a comment |
We may look where x
is greater than shifted x
with 0
appended.
x>c(x[-1],0)
# [1] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE
We may look where x
is greater than shifted x
with 0
appended.
x>c(x[-1],0)
# [1] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE
answered Jun 29 at 7:08
jay.sfjay.sf
9,5243 gold badges20 silver badges45 bronze badges
9,5243 gold badges20 silver badges45 bronze badges
add a comment |
add a comment |
Taking advantage of diff
with an appended FALSE
to catch the implied TRUE
-to-FALSE
at the end.
diff(c(x,FALSE)) == -1
# [1] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE
#[13] FALSE FALSE TRUE
3
More golfydiff(c(x,0))<0
.
– jay.sf
Jun 29 at 6:37
1
@jay.sf - all good by me.
– thelatemail
Jun 29 at 7:19
add a comment |
Taking advantage of diff
with an appended FALSE
to catch the implied TRUE
-to-FALSE
at the end.
diff(c(x,FALSE)) == -1
# [1] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE
#[13] FALSE FALSE TRUE
3
More golfydiff(c(x,0))<0
.
– jay.sf
Jun 29 at 6:37
1
@jay.sf - all good by me.
– thelatemail
Jun 29 at 7:19
add a comment |
Taking advantage of diff
with an appended FALSE
to catch the implied TRUE
-to-FALSE
at the end.
diff(c(x,FALSE)) == -1
# [1] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE
#[13] FALSE FALSE TRUE
Taking advantage of diff
with an appended FALSE
to catch the implied TRUE
-to-FALSE
at the end.
diff(c(x,FALSE)) == -1
# [1] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE
#[13] FALSE FALSE TRUE
answered Jun 29 at 4:31
thelatemailthelatemail
70.5k10 gold badges91 silver badges156 bronze badges
70.5k10 gold badges91 silver badges156 bronze badges
3
More golfydiff(c(x,0))<0
.
– jay.sf
Jun 29 at 6:37
1
@jay.sf - all good by me.
– thelatemail
Jun 29 at 7:19
add a comment |
3
More golfydiff(c(x,0))<0
.
– jay.sf
Jun 29 at 6:37
1
@jay.sf - all good by me.
– thelatemail
Jun 29 at 7:19
3
3
More golfy
diff(c(x,0))<0
.– jay.sf
Jun 29 at 6:37
More golfy
diff(c(x,0))<0
.– jay.sf
Jun 29 at 6:37
1
1
@jay.sf - all good by me.
– thelatemail
Jun 29 at 7:19
@jay.sf - all good by me.
– thelatemail
Jun 29 at 7:19
add a comment |
Check rle
rlex = rle(x)
end = cumsum(rlex$lengths)
x&(seq(length(x)) %in% end)
[1] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE
Another layout suggested by Frank
seq_along(x) %in% with(rle(x), cumsum(lengths)[values])
[1] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE
add a comment |
Check rle
rlex = rle(x)
end = cumsum(rlex$lengths)
x&(seq(length(x)) %in% end)
[1] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE
Another layout suggested by Frank
seq_along(x) %in% with(rle(x), cumsum(lengths)[values])
[1] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE
add a comment |
Check rle
rlex = rle(x)
end = cumsum(rlex$lengths)
x&(seq(length(x)) %in% end)
[1] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE
Another layout suggested by Frank
seq_along(x) %in% with(rle(x), cumsum(lengths)[values])
[1] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE
Check rle
rlex = rle(x)
end = cumsum(rlex$lengths)
x&(seq(length(x)) %in% end)
[1] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE
Another layout suggested by Frank
seq_along(x) %in% with(rle(x), cumsum(lengths)[values])
[1] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE
edited Jun 29 at 4:24
answered Jun 29 at 2:50
WeNYoBenWeNYoBen
145k8 gold badges51 silver badges81 bronze badges
145k8 gold badges51 silver badges81 bronze badges
add a comment |
add a comment |
Another version with rle
x[setdiff(seq_along(x), with(rle(x), cumsum(lengths) * values))] <- FALSE
x
#[1] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE
add a comment |
Another version with rle
x[setdiff(seq_along(x), with(rle(x), cumsum(lengths) * values))] <- FALSE
x
#[1] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE
add a comment |
Another version with rle
x[setdiff(seq_along(x), with(rle(x), cumsum(lengths) * values))] <- FALSE
x
#[1] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE
Another version with rle
x[setdiff(seq_along(x), with(rle(x), cumsum(lengths) * values))] <- FALSE
x
#[1] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE
answered Jun 29 at 4:14
Ronak ShahRonak Shah
65.3k10 gold badges46 silver badges80 bronze badges
65.3k10 gold badges46 silver badges80 bronze badges
add a comment |
add a comment |
An option with duplicated
library(data.table)
!duplicated(rleid(x), fromLast = TRUE) & x
#[1] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE
add a comment |
An option with duplicated
library(data.table)
!duplicated(rleid(x), fromLast = TRUE) & x
#[1] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE
add a comment |
An option with duplicated
library(data.table)
!duplicated(rleid(x), fromLast = TRUE) & x
#[1] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE
An option with duplicated
library(data.table)
!duplicated(rleid(x), fromLast = TRUE) & x
#[1] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE
answered Jun 29 at 8:43
akrunakrun
447k15 gold badges247 silver badges329 bronze badges
447k15 gold badges247 silver badges329 bronze badges
add a comment |
add a comment |
benchmarks
Thanks for all the solutions. If anyone is interested in benchmarks:
library(dplyr)
library(data.table)
set.seed(1)
x <- sample(c(TRUE, FALSE), 1000000, replace = T)
y <- data.frame(x = x) # For M. Viking's solution
x_dt <- x # For Ronak Shah's solution
microbenchmark::microbenchmark(Khaynes = Khaynes <- c((x[-1] != x[-length(x)]),T) & x,
jay.sf = jay.sf <- x>c(x[-1],0),
jay.sf_2 = jay.sf_2 <- diff(c(x,0))<0,
thelatemail = thelatemail <- diff(c(x,FALSE)) == -1,
WeNYoBen = rlex = rle(x); end = cumsum(rlex$lengths); WeNYoBen <- x&(seq(length(x)) %in% end),
M._Viking = M._Viking <- y %>% mutate(lasttrue = case_when(x > lead(x) ~ T, T ~ F)),
akrun = akrun <- !duplicated(rleid(x), fromLast = TRUE) & x,
frank = frank <- seq_along(x) %in% with(rle(x), cumsum(lengths)[values]),
Ronak_Shah = x_dt[setdiff(seq_along(x_dt), with(rle(x_dt), cumsum(lengths) * values))] <- FALSE,
times = 50)
# Output:
# Unit: milliseconds
# expr min lq mean median uq max neval
# Khaynes 23.0283 26.5010 31.76180 31.71290 37.1449 46.3824 50
# jay.sf 13.0630 13.5373 17.84056 13.77135 20.5462 73.5926 50
# jay.sf_2 26.1960 27.7653 35.25296 36.39615 39.3686 61.8858 50
# thelatemail 24.8204 26.7178 32.51675 33.50165 36.6328 41.9279 50
# WeNYoBen 83.9070 98.4700 107.79965 101.88475 107.1933 170.2940 50
# M._Viking 73.5963 83.4467 93.99603 86.58535 94.0915 151.7075 50
# akrun 42.5265 43.2879 48.42697 44.98085 51.1533 105.2836 50
# frank 81.9115 90.1559 95.40261 93.97015 98.2921 129.6162 50
# Ronak_Shah 109.0678 121.8230 133.10690 125.63930 133.7222 231.5350 50
all.equal(Khaynes, jay.sf)
all.equal(Khaynes, jay.sf_2)
all.equal(Khaynes, thelatemail)
all.equal(Khaynes, WeNYoBen)
all.equal(Khaynes, M._Viking$lasttrue) # When the last element is TRUE it will return false.
all.equal(Khaynes, akrun)
all.equal(Khaynes, frank)
all.equal(Khaynes, x_dt) # Ronak Shah solution.
2
Nice follow up. @jay.sf 'sx>c(x[-1],0)
is the winner.
– M. Viking
Jun 30 at 0:34
add a comment |
benchmarks
Thanks for all the solutions. If anyone is interested in benchmarks:
library(dplyr)
library(data.table)
set.seed(1)
x <- sample(c(TRUE, FALSE), 1000000, replace = T)
y <- data.frame(x = x) # For M. Viking's solution
x_dt <- x # For Ronak Shah's solution
microbenchmark::microbenchmark(Khaynes = Khaynes <- c((x[-1] != x[-length(x)]),T) & x,
jay.sf = jay.sf <- x>c(x[-1],0),
jay.sf_2 = jay.sf_2 <- diff(c(x,0))<0,
thelatemail = thelatemail <- diff(c(x,FALSE)) == -1,
WeNYoBen = rlex = rle(x); end = cumsum(rlex$lengths); WeNYoBen <- x&(seq(length(x)) %in% end),
M._Viking = M._Viking <- y %>% mutate(lasttrue = case_when(x > lead(x) ~ T, T ~ F)),
akrun = akrun <- !duplicated(rleid(x), fromLast = TRUE) & x,
frank = frank <- seq_along(x) %in% with(rle(x), cumsum(lengths)[values]),
Ronak_Shah = x_dt[setdiff(seq_along(x_dt), with(rle(x_dt), cumsum(lengths) * values))] <- FALSE,
times = 50)
# Output:
# Unit: milliseconds
# expr min lq mean median uq max neval
# Khaynes 23.0283 26.5010 31.76180 31.71290 37.1449 46.3824 50
# jay.sf 13.0630 13.5373 17.84056 13.77135 20.5462 73.5926 50
# jay.sf_2 26.1960 27.7653 35.25296 36.39615 39.3686 61.8858 50
# thelatemail 24.8204 26.7178 32.51675 33.50165 36.6328 41.9279 50
# WeNYoBen 83.9070 98.4700 107.79965 101.88475 107.1933 170.2940 50
# M._Viking 73.5963 83.4467 93.99603 86.58535 94.0915 151.7075 50
# akrun 42.5265 43.2879 48.42697 44.98085 51.1533 105.2836 50
# frank 81.9115 90.1559 95.40261 93.97015 98.2921 129.6162 50
# Ronak_Shah 109.0678 121.8230 133.10690 125.63930 133.7222 231.5350 50
all.equal(Khaynes, jay.sf)
all.equal(Khaynes, jay.sf_2)
all.equal(Khaynes, thelatemail)
all.equal(Khaynes, WeNYoBen)
all.equal(Khaynes, M._Viking$lasttrue) # When the last element is TRUE it will return false.
all.equal(Khaynes, akrun)
all.equal(Khaynes, frank)
all.equal(Khaynes, x_dt) # Ronak Shah solution.
2
Nice follow up. @jay.sf 'sx>c(x[-1],0)
is the winner.
– M. Viking
Jun 30 at 0:34
add a comment |
benchmarks
Thanks for all the solutions. If anyone is interested in benchmarks:
library(dplyr)
library(data.table)
set.seed(1)
x <- sample(c(TRUE, FALSE), 1000000, replace = T)
y <- data.frame(x = x) # For M. Viking's solution
x_dt <- x # For Ronak Shah's solution
microbenchmark::microbenchmark(Khaynes = Khaynes <- c((x[-1] != x[-length(x)]),T) & x,
jay.sf = jay.sf <- x>c(x[-1],0),
jay.sf_2 = jay.sf_2 <- diff(c(x,0))<0,
thelatemail = thelatemail <- diff(c(x,FALSE)) == -1,
WeNYoBen = rlex = rle(x); end = cumsum(rlex$lengths); WeNYoBen <- x&(seq(length(x)) %in% end),
M._Viking = M._Viking <- y %>% mutate(lasttrue = case_when(x > lead(x) ~ T, T ~ F)),
akrun = akrun <- !duplicated(rleid(x), fromLast = TRUE) & x,
frank = frank <- seq_along(x) %in% with(rle(x), cumsum(lengths)[values]),
Ronak_Shah = x_dt[setdiff(seq_along(x_dt), with(rle(x_dt), cumsum(lengths) * values))] <- FALSE,
times = 50)
# Output:
# Unit: milliseconds
# expr min lq mean median uq max neval
# Khaynes 23.0283 26.5010 31.76180 31.71290 37.1449 46.3824 50
# jay.sf 13.0630 13.5373 17.84056 13.77135 20.5462 73.5926 50
# jay.sf_2 26.1960 27.7653 35.25296 36.39615 39.3686 61.8858 50
# thelatemail 24.8204 26.7178 32.51675 33.50165 36.6328 41.9279 50
# WeNYoBen 83.9070 98.4700 107.79965 101.88475 107.1933 170.2940 50
# M._Viking 73.5963 83.4467 93.99603 86.58535 94.0915 151.7075 50
# akrun 42.5265 43.2879 48.42697 44.98085 51.1533 105.2836 50
# frank 81.9115 90.1559 95.40261 93.97015 98.2921 129.6162 50
# Ronak_Shah 109.0678 121.8230 133.10690 125.63930 133.7222 231.5350 50
all.equal(Khaynes, jay.sf)
all.equal(Khaynes, jay.sf_2)
all.equal(Khaynes, thelatemail)
all.equal(Khaynes, WeNYoBen)
all.equal(Khaynes, M._Viking$lasttrue) # When the last element is TRUE it will return false.
all.equal(Khaynes, akrun)
all.equal(Khaynes, frank)
all.equal(Khaynes, x_dt) # Ronak Shah solution.
benchmarks
Thanks for all the solutions. If anyone is interested in benchmarks:
library(dplyr)
library(data.table)
set.seed(1)
x <- sample(c(TRUE, FALSE), 1000000, replace = T)
y <- data.frame(x = x) # For M. Viking's solution
x_dt <- x # For Ronak Shah's solution
microbenchmark::microbenchmark(Khaynes = Khaynes <- c((x[-1] != x[-length(x)]),T) & x,
jay.sf = jay.sf <- x>c(x[-1],0),
jay.sf_2 = jay.sf_2 <- diff(c(x,0))<0,
thelatemail = thelatemail <- diff(c(x,FALSE)) == -1,
WeNYoBen = rlex = rle(x); end = cumsum(rlex$lengths); WeNYoBen <- x&(seq(length(x)) %in% end),
M._Viking = M._Viking <- y %>% mutate(lasttrue = case_when(x > lead(x) ~ T, T ~ F)),
akrun = akrun <- !duplicated(rleid(x), fromLast = TRUE) & x,
frank = frank <- seq_along(x) %in% with(rle(x), cumsum(lengths)[values]),
Ronak_Shah = x_dt[setdiff(seq_along(x_dt), with(rle(x_dt), cumsum(lengths) * values))] <- FALSE,
times = 50)
# Output:
# Unit: milliseconds
# expr min lq mean median uq max neval
# Khaynes 23.0283 26.5010 31.76180 31.71290 37.1449 46.3824 50
# jay.sf 13.0630 13.5373 17.84056 13.77135 20.5462 73.5926 50
# jay.sf_2 26.1960 27.7653 35.25296 36.39615 39.3686 61.8858 50
# thelatemail 24.8204 26.7178 32.51675 33.50165 36.6328 41.9279 50
# WeNYoBen 83.9070 98.4700 107.79965 101.88475 107.1933 170.2940 50
# M._Viking 73.5963 83.4467 93.99603 86.58535 94.0915 151.7075 50
# akrun 42.5265 43.2879 48.42697 44.98085 51.1533 105.2836 50
# frank 81.9115 90.1559 95.40261 93.97015 98.2921 129.6162 50
# Ronak_Shah 109.0678 121.8230 133.10690 125.63930 133.7222 231.5350 50
all.equal(Khaynes, jay.sf)
all.equal(Khaynes, jay.sf_2)
all.equal(Khaynes, thelatemail)
all.equal(Khaynes, WeNYoBen)
all.equal(Khaynes, M._Viking$lasttrue) # When the last element is TRUE it will return false.
all.equal(Khaynes, akrun)
all.equal(Khaynes, frank)
all.equal(Khaynes, x_dt) # Ronak Shah solution.
answered Jun 29 at 22:47
KhaynesKhaynes
8818 silver badges22 bronze badges
8818 silver badges22 bronze badges
2
Nice follow up. @jay.sf 'sx>c(x[-1],0)
is the winner.
– M. Viking
Jun 30 at 0:34
add a comment |
2
Nice follow up. @jay.sf 'sx>c(x[-1],0)
is the winner.
– M. Viking
Jun 30 at 0:34
2
2
Nice follow up. @jay.sf 's
x>c(x[-1],0)
is the winner.– M. Viking
Jun 30 at 0:34
Nice follow up. @jay.sf 's
x>c(x[-1],0)
is the winner.– M. Viking
Jun 30 at 0:34
add a comment |
Non-base
solution for identifying the last TRUE
before a FALSE
.
library(dplyr)
y <- data.frame(x = c(FALSE,FALSE,FALSE,TRUE,TRUE,TRUE,FALSE,FALSE,
FALSE,TRUE,TRUE,TRUE,FALSE,TRUE,TRUE))
y %>%
mutate(lasttrue = case_when(x == TRUE & lead(x) == FALSE ~ TRUE,
TRUE ~ FALSE))
Edit:
y %>%
mutate(lasttrue = case_when(x > lead(x) ~ T,
T ~ F))
Returns the wrong result with the last two elements are FALSE, TRUE
– Khaynes
Jun 29 at 21:02
add a comment |
Non-base
solution for identifying the last TRUE
before a FALSE
.
library(dplyr)
y <- data.frame(x = c(FALSE,FALSE,FALSE,TRUE,TRUE,TRUE,FALSE,FALSE,
FALSE,TRUE,TRUE,TRUE,FALSE,TRUE,TRUE))
y %>%
mutate(lasttrue = case_when(x == TRUE & lead(x) == FALSE ~ TRUE,
TRUE ~ FALSE))
Edit:
y %>%
mutate(lasttrue = case_when(x > lead(x) ~ T,
T ~ F))
Returns the wrong result with the last two elements are FALSE, TRUE
– Khaynes
Jun 29 at 21:02
add a comment |
Non-base
solution for identifying the last TRUE
before a FALSE
.
library(dplyr)
y <- data.frame(x = c(FALSE,FALSE,FALSE,TRUE,TRUE,TRUE,FALSE,FALSE,
FALSE,TRUE,TRUE,TRUE,FALSE,TRUE,TRUE))
y %>%
mutate(lasttrue = case_when(x == TRUE & lead(x) == FALSE ~ TRUE,
TRUE ~ FALSE))
Edit:
y %>%
mutate(lasttrue = case_when(x > lead(x) ~ T,
T ~ F))
Non-base
solution for identifying the last TRUE
before a FALSE
.
library(dplyr)
y <- data.frame(x = c(FALSE,FALSE,FALSE,TRUE,TRUE,TRUE,FALSE,FALSE,
FALSE,TRUE,TRUE,TRUE,FALSE,TRUE,TRUE))
y %>%
mutate(lasttrue = case_when(x == TRUE & lead(x) == FALSE ~ TRUE,
TRUE ~ FALSE))
Edit:
y %>%
mutate(lasttrue = case_when(x > lead(x) ~ T,
T ~ F))
edited Jun 29 at 16:12
answered Jun 29 at 3:14
M. VikingM. Viking
5211 silver badge6 bronze badges
5211 silver badge6 bronze badges
Returns the wrong result with the last two elements are FALSE, TRUE
– Khaynes
Jun 29 at 21:02
add a comment |
Returns the wrong result with the last two elements are FALSE, TRUE
– Khaynes
Jun 29 at 21:02
Returns the wrong result with the last two elements are FALSE, TRUE
– Khaynes
Jun 29 at 21:02
Returns the wrong result with the last two elements are FALSE, TRUE
– Khaynes
Jun 29 at 21:02
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%2f56814732%2fidentifying-positions-of-the-last-trues-in-a-sequence-of-trues-and-falses%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