0
V是與多個NAS的載體。編寫一個函數來替換這些NA值,使得在索引i處的缺失值應該由在索引p和q處的非NA值的平均值代替,其中| p - i | + | q - i |被最小化。
所以,如果我的矢量("NA", 1, 2, "NA", "NA", 3)
那麼我的結果必須是(1.5, 1, 2, 1.5, 1.5, 3)
我如何寫一個嵌套的for循環來產生這種輸出?
V是與多個NAS的載體。編寫一個函數來替換這些NA值,使得在索引i處的缺失值應該由在索引p和q處的非NA值的平均值代替,其中| p - i | + | q - i |被最小化。
所以,如果我的矢量("NA", 1, 2, "NA", "NA", 3)
那麼我的結果必須是(1.5, 1, 2, 1.5, 1.5, 3)
我如何寫一個嵌套的for循環來產生這種輸出?
dist_elem <- function(x,pos){
# Function that calculates the distance between pos and all other positions in vector x
d <- rep(0,length(x))
for (i in 1:length(x)){
d[i] <- abs(pos - i)
}
return(d)
}
for (i in 1:length(x)){
if (is.na(x[i])){
# distances between all other elements
distances <- dist_elem(x,i)
# NA for that element
distances[distances == 0] <- NA
# NA for the NAs
distances[is.na(x)] <- NA
# Sort and get the first two lower distances
s <- sort(distances)[1:2]
# The closest element (first of that vector in case of ties)
x1 <- x[which(distances == s[1])[1]]
# The second closest element (first of that vector in case of ties)
x2 <- x[which(distances == s[2])[1]]
out <- (x1 + x2)/2
x[i] <- out
}
}
您可以使用此一:
vect <- c(NA, 1, 2, NA, NA, 3)
flag <- is.na(vect)+0
wh <- which(is.na(vect)==1)
flag[flag==1] <- wh
#flag is a container of all values, however a missing vector position will contain a value of 0 a non missing value will contain the position
k <- 0
#a rolling itertor which changes the mean as per the non missing values in the vector
vect_ <- vect
# Final vector which will have the outcome.
for(i in 1:(length(vect))){
k <- ifelse(flag[i] > 0 , k+1,k)
k <- ifelse(k == length(wh), k-1,k)
vect_[i] <- ifelse(flag[i] > 0,
mean(vect[min(wh):diff(c(1,wh[1+k]))],na.rm=T),vect[i])
}
vect_
> vect_
[1] 1.5 1.0 2.0 1.5 1.5 3.0
歡迎StackOverflow上。在這種情況下,可能'myVec [is.na(myVec)] < - mean(myVec,na.rm = TRUE)'將起作用。不過,請看看這些關於如何生成[最小,完整和可驗證的示例]的技巧(http://stackoverflow.com/help/mcve),以及這篇文章[在R中創建一個很好的示例](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)。也許下面的提示[問一個好問題](http://stackoverflow.com/help/how-to-ask)也值得一讀。 – lmo
這個問題是關於輸入。您可能想要在R中搜索輸入。@Imo提供了一個很好的建議。 –
你能解釋爲什麼位置1,4和5的矢量是1.5嗎?我正在製作腳本,但不知道如何在該位置分配值 –