我有包含唯一標識符,類別和說明的數據。 下面是一個玩具數據集。如何用R中以前行的樣本數據填充非相鄰行?
prjnumber <- c(1,2,3,4,5,6,7,8,9,10)
category <- c("based","trill","lit","cold",NA,"epic", NA,NA,NA,NA)
description <- c("skip class",
"dunk on brayden",
"record deal",
"fame and fortune",
NA,
"female attention",
NA,NA,NA,NA)
toy.df <- data.frame(prjnumber, category, description)
> toy.df
prjnumber category description
1 1 based skip class
2 2 trill dunk on brayden
3 3 lit record deal
4 4 cold fame and fortune
5 5 <NA> <NA>
6 6 epic female attention
7 7 <NA> <NA>
8 8 <NA> <NA>
9 9 <NA> <NA>
10 10 <NA> <NA>
我想從填充的行中隨機取樣'category'和'description'列作爲缺失數據行的填充。 最終的數據框將會完整,只會依賴包含數據的最初5行。該解決方案將保持列間相關性。 預期的輸出是:
> toy.df
prjnumber category description
1 1 based skip class
2 2 trill dunk on brayden
3 3 lit record deal
4 4 cold fame and fortune
5 5 lit record deal
6 6 epic female attention
7 7 based skip class
8 8 based skip class
9 9 lit record deal
10 10 trill dunk on brayden
確切的說,這就是爲什麼它非常棘手。我有非連續行中沒有數據的標識符插槽。 –