R输入数据表模式和鼠标 #输入值 库(数据表) 种子集(1337) mydt=q
如果我正确理解了您的请求(您希望用x类列的模式替换缺失),这可能是一个R输入数据表模式和鼠标 #输入值 库(数据表) 种子集(1337) mydt=q,r,data.table,imputation,r-mice,R,Data.table,Imputation,R Mice,如果我正确理解了您的请求(您希望用x类列的模式替换缺失),这可能是一个数据。表解决方案: # IMPUTING VALUES library(data.table) set.seed(1337) mydt = q <- data.table(Year = rep(2000:2005, each = 10), Type = c("A","B"), Class = sample(1:5,rep=T), Car = sa
数据。表解决方案:
# IMPUTING VALUES
library(data.table)
set.seed(1337)
mydt = q <- data.table(Year = rep(2000:2005, each = 10),
Type = c("A","B"),
Class = sample(1:5,rep=T),
Car = sample(0:1, rep=T),
Boat = sample(1:4, rep=T)
)
naRows <- sample(nrow(mydt),15)
mydt[ naRows, Car := NA]
naRows <- sample(nrow(mydt),15)
mydt[ naRows, Boat := NA]
setkey(mydt,Year,Type)
计算模式的函数
统计模式
# function to calculate mode
stats_mode <- function(x) {
ux <- unique(x[!is.na(x)])
ux[which.max(tabulate(match(x, ux)))]
}
# Generate new column with mode per group
mydt[, `:=`(mCar = stats_mode(Car),
mBoat = stats_mode(Boat)), by = .(Type, Class)]
# Replace missings
mydt[is.na(Car), Car := mCar]
mydt[is.na(Boat), Boat := mBoat]
# Cleansing
mydt[, c("mBoat", "mCar") := NULL]