您的訂閱是我製作影片的動力
訂閱點這裡~
影片程式碼 (延續昨天)
#均值 vs 列入各群權重
final_mean <- final[,c(1:4,10)] %>%
group_by(分群) %>%
summarise(col1 = mean(Sepal.Length),col2 = mean(Sepal.Width),
col3 = mean(Petal.Length),col4 = mean(Petal.Width)) %>%
mutate(總值=col1 + col2 + col3 + col4) %>%
mutate_if(is.numeric, round, digits=1)
#補總差距最小 vs 每個欄位 - 每群值,取最接近的並附上權重
colnms <- colnames(iris_na)
iris_na_fill <- iris_na %>%
mutate(總值=rowSums(iris_na,na.rm = TRUE)) %>%
mutate(c1差=abs(總值-subset(final_mean,分群=="c1")$總值)) %>%
mutate(c2差=abs(總值-subset(final_mean,分群=="c2")$總值)) %>%
mutate(c3差=abs(總值-subset(final_mean,分群=="c3")$總值)) %>%
mutate(c4差=abs(總值-subset(final_mean,分群=="c4")$總值)) %>%
mutate(最小=pmin(c1差,c2差,c3差,c4差)) %>%
mutate(分群=ifelse(最小==c1差,"c1",
ifelse(最小==c2差,"c2",
ifelse(最小==c3差,"c3","c4")))) %>%
mutate_if(is.numeric, round, digits=1)
iris_na_fill <- left_join(iris_na_fill,final_mean,by='分群')
iris_na_fill <- iris_na_fill %>%
mutate(v1 = ifelse(is.na(Sepal.Length),col1,Sepal.Length)) %>%
mutate(v2 = ifelse(is.na(Sepal.Width),col2,Sepal.Width)) %>%
mutate(v3 = ifelse(is.na(Petal.Length),col3,Petal.Length)) %>%
mutate(v4 = ifelse(is.na(Petal.Width),col4,Petal.Width))
done <- iris_na_fill[17:20]
若內容有誤,還請留言指正,謝謝您的指教