Guest User

Untitled

a guest
Apr 20th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. set.seed(12345)
  2. require(data.table)
  3. dt <- data.table(col1 = rnorm(n=10, mean=20, sd=5), col2 = runif(10, 0, 1), col3 = rbinom(10, 3, .5),
  4. col4 = rnorm(n=10, mean=20, sd=5), col5 = runif(10, 0, 1), col6 = rbinom(10, 3, .5))
  5. nas1 <- which(dt[,col1] %in% sample(dt[,col1], 3))
  6. nas2 <- which(dt[,col2] %in% sample(dt[,col2], 4))
  7. nas3 <- which(dt[,col3] %in% sample(dt[,col3], 2))
  8.  
  9. dt[nas1, col1 := NA]
  10. dt[nas2, col2 := NA]
  11. dt[nas3, col3 := NA]
  12.  
  13. cols.tochange <- c("col1", "col2", "col3")
  14. .sdcols <- c("col4", "col5", "col6")
  15.  
  16. dt[, (cols.tochange) := .SD, .SDcols = .sdcols]
Add Comment
Please, Sign In to add comment