R 搜索不在列表中的下一个最近的元素
我试图从26个字母的向量中替换2个字母(重复) 我的表中已经有26个字母中的13个(键),所以替换字母不应该在这13个“键”中 我正在尝试编写代码,用下一个字母表取代R 搜索不在列表中的下一个最近的元素,r,if-statement,for-loop,R,If Statement,For Loop,我试图从26个字母的向量中替换2个字母(重复) 我的表中已经有26个字母中的13个(键),所以替换字母不应该在这13个“键”中 我正在尝试编写代码,用下一个字母表取代C&S,而下一个字母表不应该是“键”的一部分 下面的代码将repeatC替换为D,S替换为T,但这两个字母都在我的“键”中。有人知道我如何实现这个条件,以便在“key”中已经存在要替换的字母时代码将重新运行循环吗 # alphabets <- toupper(letters) keys <- c("I", "C", "P
C&S
,而下一个字母表不应该是“键”的一部分
下面的代码将repeatC替换为D
,S替换为T
,但这两个字母都在我的“键”中。有人知道我如何实现这个条件,以便在“key”中已经存在要替换的字母时代码将重新运行循环吗
# alphabets <- toupper(letters)
keys <- c("I", "C", "P", "X", "H", "J", "S", "E", "T", "D", "A", "R", "L")
repeats <- c("C", "S")
index_of_repeat_in_26 <- which(repeats %in% alphabets)
# index_of_repeat_in_26 is 3 , 19
# available_keys <- setdiff(alphabets,keys)
available <- alphabets[available_keys]
# available <- c("B", "F", "G", "K", "O", "Q", "U", "V", "W", "Y", "Z")
index_available_keys <- which(alphabets %in% available_keys)
# 2 6 7 11 15 17 21 22 23 25 26
for (i in 1:length(repeat)){
for(j in 1:(26-sort(index_of_repeat_in_26)[1])){
if(index_of_repeat_in_26[i]+j %in% index_available_keys){
char_to_replace_in_key[i] <- alphabets[index_of_capital_repeat_in_26[i]+1]
}
else{
cat("\n keys not available to replace \n")
}
}
}
#alphabets键检查while
函数。u相关,字母
产生与toupper(字母)
相同的输出。我尝试了while循环,但程序仍在运行,而(char_to_replace_in_key!=(可用)){for(I in 1:length(repeat)){for(j in 1:(26排序(索引_repeat_in_26)[1])){char_to_replace_in_key[t]Joel,谢谢,非常有用。是的,对于这个查询,它工作得非常出色。当我有25个键(A-Y),4个重复(C,S,M,N),只有一个字母表要替换(Z)。上面的代码是用Z替换所有重复。我想要的是它应该只用Z替换C,而对于剩余的S,M,N,它应该显示“key not available”消息。你知道如何通过if语句或类似的语句通过for循环实现这个条件吗?@mr_swap ohhh!我不知道这个条件。好吧应该做了!现在我没有访问RStudio的权限。以后再更新吗?问题仍然存在。现在我给了24个键(A-X),4个重复(CSMN)。它仍然用Y替换C,保持其他键不变。它工作:-),你能解释一下函数是如何逐行执行此操作的吗?
keys <- c("I", "C", "P", "X", "H", "J", "S", "E", "T", "D", "A", "R", "L")
repeats <- c("C", "S")
y = sort(setdiff(LETTERS, keys)) # get the letters not present in 'keys'
y = factor(y, levels = LETTERS) # make them factor so that we can do numeric comparisons with the levels
y1 = as.numeric(y) # keep them numeric to compare
z = factor(repeats, levels = LETTERS)
z1 = as.numeric(z)
func <- function(x) { # so here, in each iteration, the index(in this case 1:4 gets passed)
xx = y1 - z1[x] # taking the difference between each 'repeat' element from all 'non-keys'
xx = which(xx>0)[1]# choose the one with smallest difference(because 'y1' is already sorted. So the first nearest non-key gets selected
r = y[xx] # extract the corresponding 'non-key' element
y <<- y[-xx] # after i get the closest letter, I remove that from global list so that it doesn't get captured the next time
y1 <<- y1[-xx] # similarily removed from the equivalent numeric list
r # return the extracted 'closest non-key' chracter
}
# sapply is also a for-loop by itself, in which a single element get passed ro func at a time.
# Here 'seq_along' is used to pass the index. i.e. for 'C' - 1, for 'S' - 2 , etc gets passed.
ans = sapply(seq_along(repeats), func)
if (any(is.na(ans))){
cat("\n",paste0("keys not available to replace for ",
paste0(repeats[which(is.na(ans))], collapse = ",")) ,
"\n")
ans <- ans[!is.na(ans)]
}
# example 2 with :
repeats <- c("Y", "Z")
# output :
# keys not available to replace for Z
# ans
# [1] Z