我有一个整数序列。我想做的是识别所有3的序列,这些序列在AND之前加上5。例如:
c(5,3,3,5,5,4,3,3,5)
所需的输出将是:
c(F,T,T,F,F,F,F,F,F)
说明:3的第一个序列在其后,后跟5。因此True
。第二个序列以4开头,因此为False
。
答案 0 :(得分:0)
无法提供更智能的解决方案,因此这里是一个for
循环
x <- c(5,3,3,5,5,4,3,3,5) #Initial vector
current_inds <- numeric() #Variable to hold indices which need to be changed
saw_3 <- FALSE #If 3 was seen before
output <- rep(FALSE, length(x)) #output vector
num_to_check <- 5 #Value to compare
last_val <- 0 #Last non-3 value
for (i in seq_along(x)) {
#If the current value is not equal to 3
if (x[i] != 3 ) {
#Check if we previously saw 3 and if previous non_3 value was 5
# and the next value is 5
if(saw_3 & x[i + 1] == num_to_check & last_val == num_to_check) {
#Change the group of 3 indices to TRUE
output[current_inds] <- TRUE
#Make the saw_3 flag as FALSE
saw_3 <- FALSE
}
#Update the last seen non_3 value to current value
last_val = x[i]
}
else {
#If it is a 3 then append the indices in current_inds
current_inds <- c(current_inds, i)
#Make saw_3 flag TRUE
saw_3 = TRUE
}
}
output
#[1] FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
答案 1 :(得分:0)
我有一个非常冗长和丑陋的解决方案,但是它有效:p我希望有人可以找到一个更干净的方法:)我首先创建一个矩阵,该矩阵包含1列,该列是以非重复的方式表示的(不是唯一的,但没有连续数),然后再加上1列,并重复此数字。然后,我应用逻辑函数来查看3是否被5s包围,并在最后一步中,使用rep()函数将向量解译为原始长度...
x <- c(5,3,3,5,5,4,3,3,5)
x_reduced <- x[x!=c(x[-1], FALSE)]
x_mat <- matrix(0, ncol = 3, nrow = length(x_reduced))
x_mat[ , 1] <- x_reduced
ctr = 1
x_ctr = 1
while (ctr < length(x)) {
x_mat[x_ctr ,1] = x[ctr]
x_mat[x_ctr, 2] = x_mat[x_ctr, 2] + 1
if(x[ctr+1] == x[ctr]){
ctr = ctr + 1
} else {
x_ctr = x_ctr + 1
ctr = ctr + 1
}
}
x_mat[nrow(x_mat), 1] <- x[length(x)]
x_mat[nrow(x_mat), 2] <- x_mat[nrow(x_mat), 2] + 1
check_element <- function(pos) {
if(pos == 1 | pos == nrow(x_mat)) return(FALSE)
if(x_mat[pos+1, 1] == 5 & x_mat[pos-1, 1] == 5){
return(TRUE)
} else {
return(FALSE)
}
}
x_mat[,3] <- sapply(1:nrow(x_mat), check_element)
rep(x_mat[,3], x_mat[,2])
答案 2 :(得分:0)
还有优化的空间,但是使用dplyr
和rle()
当然可以实现。
> df_result
# A tibble: 9 x 1
result
<lgl>
1 FALSE
2 TRUE
3 TRUE
4 FALSE
5 FALSE
6 FALSE
7 FALSE
8 FALSE
9 FALSE
df_result <- df %>%
group_by(seq = {seq = rle(value); rep(seq_along(seq$lengths), seq$lengths)}) %>%
ungroup() %>%
mutate(last_3 = case_when(lag(seq) != seq ~ as.numeric(lag(value) == 5),
TRUE ~ NA_real_),
next_5 = case_when(lead(seq) != seq ~ as.numeric(lead(value) == 5),
TRUE ~ NA_real_)) %>%
group_by(seq, value) %>%
mutate(result = case_when(sum(last_3, na.rm = TRUE) + sum(next_5, na.rm = TRUE) == 2 ~ TRUE,
TRUE ~ FALSE)) %>%
ungroup() %>%
select(result)
library(dplyr)
df <- data.frame(value = c(5,3,3,5,5,4,3,3,5))