這段代碼是假設取一個單詞,並根據單詞中字母的位置計算單詞字母的值。因此,對於像「爆發」這是假設計算值字母「r」和「K」我是否正確使用sapply?
strg <- 'broke'
#this part stores everything except the first,
#last, and middle position of the word
strg.leng <- nchar(strg)
other.letts <- sequence(strg.leng)
if (length(other.letts) %% 2 != 0) {
oth_let1 <- other.letts[-c(1, ceiling(length(other.letts)/2), length(other.letts))]
} else {
oth_let0 <- other.letts[-c(1, c(1,0) + floor(length(other.letts)/2), length(other.letts))]
}
print(paste("Values of the other letters of: ", strg))
#here is where the computation starts, taking in the objects created above
if ((nchar(strg) %% 2) != 0) {
sapply(oth_let1, function(i) print(paste(oth_let1[i], "L", (.66666*1.00001) - (oth_let1[i] - 1) *.05)))
} else {
sapply(oth_let0, function(i) print(paste(oth_let0[i], "L", (.66666*1.00001) - (oth_let0[i] - 1) *.05)))
}
然而,對於「爆料」我知道這裏面是隻計算的「K」值和一個字一些其他的東西:
[1] "4 L 0.5166666666"
[1] "NA L NA"
[1] "4 L 0.5166666666" "NA L NA"
雖然所需的輸出應該是兩個「R」和「k」的值,所以是這樣的:
我在做什麼錯?我錯誤地使用了sapply
嗎?
爲什麼你使用'oth_let0 [i]'?它應該是簡單的「我」。而「如果其他」在那裏是多餘的。你應該簡單地放一個sapply(並使用一個變量而不是兩個:'oth_let0'和'oth_let1')。並且,看看[這](http://stackoverflow.com/questions/19480652/sapply-in-r-why-returns-input)瞭解最後一個。 – m0nhawk