I have some import issues with a file, that create an empty column at the end such as :
library(data.table)
library(tidyverse)
MWE <- data.table(var1=c(1,2),var2=c(3,4),var3=c(NA,NA))
Now I can remove it easily as I know the empty column is the last one :
MWE2 <- MWE[,c(length(MWE)):=NULL]
But I wondered how I would do if I just wanted to remove a random empty column without knowing its number. A quick search here and on the datatable page gave me a lot of examples on how to :
na.omit
But I did not find solutions to remove empty columns in datatables. What are the options and which is the fastest ?
We could check if all
values are NA
in a column, get the column name and assign those to NULL
nm1 <- MWE[, names(which(sapply(.SD, function(x) all(is.na(x)))))]
# or
# nm1 <- MWE[, names(which(!colSums(!is.na(.SD))))]
MWE[, (nm1) := NULL]
Or with Filter
MWE[, Filter(function(x) any(!is.na(x)), .SD)]
Or using select
library(dplyr)
MWE %>%
select(where(~ any(!is.na(.))))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With