我目前正在嘗試檢查元素是否爲列表的一部分,如果是大小寫則返回1,否則返回0。測試元素是否在列表中並返回0或1
我一直在做這個循環,但由於矩陣將會非常大(這裏只提供一個簡單的例子),我想知道是否有一種有效的方法來做到這一點。
rm(list=ls()) # clear memory
names <- c("a","b","c","d","e","f","g","h","i","j","k50","l50","m50","n50","o50")
proba <- c(1,1,1,1,1,1,1,1,1,1,0.5,0.5,0.5,0.5,0.5)
T1_temp <- sample(names,4,prob=proba)
# Loop to check which element of T1_temp is included in names
T1 <- c()
for (i in 1:length(names)){
if (is.element(names[i],T1_temp)){T1[i]=1}
else{T1[i]=0}
}
,結果
[1] 0 1 0 0 1 1 0 0 1 0 0 0 0 0 0
@Donkey香港感謝您重塑我的問題,並感謝JOGO FO提示 – richpiana