rantish
rantish

Reputation: 79

Filtering null values in columns dynamically using functions

I am trying to dynamically filter out null values from a column in my data frame. For this I need to pass the column name as a function parameter and then remove nulls. I have tried the following code and it gives me an error stating "Warning message:In is.na(tableMQproblem$test) : is.na() applied to non-(list or vector) of type 'NULL'" :-

library(dplyr)
tabledata  <- read.csv("data.csv",head=TRUE,sep= ',',check.names=FALSE)
tabledata[tabledata == ""] <- NA
x<-function(test)
{    
    tabledata  <- tabledata[!(is.na(tabledata$`test`)), ]
}

table_finalC <- x("confi Itm")

My Test data -

structure(list(R_co = structure(c(2L, 3L, 1L), .Label = c(" ", 
"A220", "B334"), class = "factor"), confi_Itm = structure(c(2L, 
1L, 1L), .Label = c("", "CFMX1"), class = "factor")), .Names = c("R_co", 
"confi_Itm"), row.names = c(NA, 3L), class = "data.frame")

I am trying to remove nulls from confi_Itm first and the output should be -:

R_co confi_Itm
1 A220     CFMX1

PS - tabledata <- tabledata[!(is.na(tableMQproblem$test)), ] -- here test is enclosed in backticks because my column names have spaces. I was unable to make test data with column name having spaces(I have used underscore instead) but the real data has that in my case.

Upvotes: 0

Views: 163

Answers (1)

akrun
akrun

Reputation: 887118

We can use rowSums of the blank elements '', get a logical index for those rows with '0' blanks, and subset the dataset.

res <- droplevels(df[!rowSums(df==''),])
res
# R_co confi_Itm
#1 A220     CFMX1

We wrap it with droplevels (as the columns are factor class) to remove the unused levels.

Upvotes: 1

Related Questions