y=function(station,lat,lon){
temp=cbind(station,lat,lon)
lat_ind=lat!="unknown"
lon_ind=lon!="unknown"
if(all(lat_ind)==0){
hash=unique(temp[lat_ind,])
ind2=hash[,1]==station[!lat_ind]
temp[!lat_ind,]=temp[ind2,]
return(temp)
}else if(all(lon_ind)==0){
hash=unique(temp[lon_ind,])
ind2=hash[,1]==station[!lon_ind]
temp[!lon_ind,]=temp[ind2,]
return(temp)
}else {
return(temp)
}
}
##case1
station <- c("a","b","c","c","c")
lat <- c("1","2","3","3","unknown")
lon <- c("6","7","8","8","unknown")
y(station,lat,lon)
# station lat lon
# [1,] "a" "1" "6"
# [2,] "b" "2" "7"
# [3,] "c" "3" "8"
# [4,] "c" "3" "8"
# [5,] "c" "3" "8"
##case2
station <- c("a","b","c","c","c")
lat <- c("1","2","3","3","3")
lon <- c("6","7","8","8","unknown")
y(station,lat,lon)
# station lat lon
# [1,] "a" "1" "6"
# [2,] "b" "2" "7"
# [3,] "c" "3" "8"
# [4,] "c" "3" "8"
# [5,] "c" "3" "8"
##case3
station <- c("a","b","c","c","c")
lat <- c("1","2","3","3","unknown")
lon <- c("6","7","8","8","8")
y(station,lat,lon)
# station lat lon
# [1,] "a" "1" "6"
# [2,] "b" "2" "7"
# [3,] "c" "3" "8"
# [4,] "c" "3" "8"
# [5,] "c" "3" "8"
這是真正代表你的數據嗎?換句話說,你的數據集中是否真的有「未知」這個詞,或者是否編碼爲「NA」(應該是)? 「lat」和「lon」的'data.frame'中的值實際上是數值,還是因爲它們在這個問題中呢? – A5C1D2H2I1M1N2O1R2T1
它在原始數據集中表示「未知」,這是因素。如果需要,我可以使用as.numeric來表示NA。 – John
您的數據是按車站訂購的嗎?你確定你所有的電臺至少有一個有效值的代表嗎? – agstudy