我将以下数据框命名为 cars
Brand year mpg reputation Luxury
Honda 2010 30 8.5 0.5
Honda 2011 28 8.5 0.6
Dodge 2010 20 6.5 0.6
Dodge 2011 23 7.0 0.7
Mercedes 2010 22 9.5 NA
Mercedes 2011 25 9.0 NA
Run Code Online (Sandbox Code Playgroud)
我想用随机产生的实数替换NA之间 0.9 and 1.0
我正在尝试以下操作,但是它用数字0.9代替了NA。
cars[is.na(cars)] <- sample(0.9:1, sum(is.na(cars)),replace=TRUE)
Run Code Online (Sandbox Code Playgroud)
数据表将如下所示:
Brand year mpg reputation Luxury
Honda 2010 30 8.5 0.5
Honda 2011 28 8.5 0.6
Dodge 2010 20 6.5 0.6
Dodge 2011 23 7.0 0.7
Mercedes 2010 22 9.5 *0.91*
Mercedes 2011 25 9.0 *0.97*
Run Code Online (Sandbox Code Playgroud)
数据结构代码:
cars <- structure(list(Brand = c("Honda","Honda", "Dodge", "Dodge","Mercedes","Mercedes"),
year = c(2010L, 2011L,2010L, 2011L, 2010L, 2011L),
mpg = c(30L, 28L, 20L, 23L, 22L, 25L), reputation = c(8.5, 8.5, 6.5, 7L, 9.5, 9.5), Luxury = c(5L, 5.5, 6L, 6.5)),
class = "data.frame", row.names = c(NA, -4L))
Run Code Online (Sandbox Code Playgroud)
使用runif
代替sample
:
cars[is.na(cars)] <- runif(sum(is.na(cars)), min = 0.9, max = 1)
Run Code Online (Sandbox Code Playgroud)