I noticed a problem or a concern with the startsWith()
function.
The following code displays two different selection.
The first one behave normally which is this chunk of code:
dt_test <- data.table(a = c("abcd", "poo", "abla", "ba"),
id = c(1,2,3, 4))
dt_test[startsWith(a, c("ab", "ao")),id]
# [1] 1 3
startsWith(dt_test$a, c("ab", "ao"))
# TRUE FALSE TRUE FALSE
And if you noticed, this one only selects the first one which is counter intuitive since id
2 and 4 are supposed to be TRUE
dt_test <- data.table(a = c("ab","abcd", "poo", "abla", "ba"),
id = c(1,2,3, 4,5))
dt_test[startsWith(a, c("ab", "ao")),id]
# [1] 1
startsWith(dt_test$a, c("ab", "ao"))
# [1] TRUE FALSE FALSE FALSE FALSE
What I'm I supposed to use as a substitute to startsWith()
in this precise case?
question from:
https://stackoverflow.com/questions/65836695/mutate-a-new-column-using-dplyr-with-values-based-on-multiple-conditions 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…