Je souhaite identifier les valeurs manquantes dans une datable de données R
Afin d'obtenir l'id, la colonne "id" de chaque colonne de votre ensemble de données
J'utilise apply (is. na (dt_tb) 2, qui) ce script m'indique la position, je voudrais remplacer la position par le numéro d'id (colonne id)
dt_tb <- data.table(id = c(5, 6, 7, 15),
coll = c("this", NA,"NA", "text"),
cyy = c(TRUE, FALSE, TRUE, TRUE),
hhh = c(2.5, 4.2, 3.2, NA),
stringsAsFactors = FALSE)
apply(is.na(dt_tb), 2, which)
exemple $ id entier (0)
$ coll [1] 2
$ cyy entier (0)
$ hhh [1] 4
Je veux
id entier (0)
coll 6 7
cyy entier (0)
hhh 15
3 Réponses :
Vous pouvez utiliser unlist pour obtenir id de dt_tb $ id et relist pour revenir à l'origine structure.
i <- apply(is.na(dt_tb) | dt_tb=="NA", 2, which) relist(dt_tb$id[unlist(i)], i) #$id #numeric(0) # #$coll #[1] 6 7 # #$cyy #numeric(0) # #$hhh #[1] 15
Merci comment je peux renvoyer un dataframe ou datatable
Jetez un œil à: Convertir une liste en bloc de données
vous pouvez utiliser complete.cases(dt_tb)
dt_tb[which(!complete.cases(dt_tb)),1] #to return ID's id 1: 6 2: 15
update:
install.packages("devtools")
install.packages("data.table")
library(devtools)
library(data.table)
dt_tb <- data.table(id = c(5, 6, 7, 15),
coll = c("this", NA,"NA", "text"),
cyy = c(TRUE, FALSE, TRUE, TRUE),
hhh = c(2.5, 4.2, 3.2, NA),
stringsAsFactors = FALSE)
complete.cases(dt_tb) # returns: TRUE FALSE TRUE FALSE
which(!complete.cases(dt_tb)) # return row numbers: 2 4
dt_tb[!complete.cases(dt_tb),] # returns: rows with missing data/na's
Vous pouvez utiliser qui avec arr.ind = TRUE pour obtenir l'index des lignes et des colonnes où NA ou "NA" est présent. Vous pouvez ensuite utiliser split pour obtenir une liste nommée. mat <- which(is.na(dt_tb) | dt_tb == 'NA', arr.ind = TRUE)
split(dt_tb$id[mat[, 1]], names(dt_tb)[mat[, 2]])
#$coll
#[1] 6 7
#$hhh
#[1] 15