Home > Enterprise >  How to count empty value and print out that observation?
How to count empty value and print out that observation?

Time:11-24

I have a data frame like this:

enter image description here

The problem is: Print out the name of the object that have the most empty value. As we can see, c has 3 empty value so the answer to that problem is c

I have used

count() 

and

table()

but it's obviously wrong. Is there any way in R that I can solve that problem? I'm sorry, but I don't have enough reputation to post image.

CodePudding user response:

I think you may be looking for something like this:

test <- cbind.data.frame( # dummy df after picture 
  name = letters[1:5], 
  color = c("B", "R", "" , "W", "R"), 
  shape = c("R", "" , "O", "R", "R"),
  price = c(10 , "" , 30 , 40 , 50),
  weight =c(1  , "" , "" , 3  , 4))

which.max(rowSums(test=="")) # the idea is to count empty spaces ("") per row

[1] 2

#####

test[which.max(rowSums(test=="")),]

  name color shape price weight
2    b     R          

The test condition is set to =="" (empty string) and so the whole data frame is converted to string. If your missing values are stored with a different character/NA then switch the ""with the correct missing value/NA.

In the future, please add data via dput() instead of images.

  • Related