2

How do I select empty cells from column D1 of my database (image attached)

I tried as follows:

df<-read_excel('C:/Users/Antonio/Desktop/test.xlsx')
x<-subset(df,df$D1=="")

But it did not work.

enter image description here

2 Answers2

2

We could use complete_cases from base R:

To select empty rows:

df
x <- subset(df, !complete.cases(df))

Output:

    D1 D2 D3 A1 A2 A3
2 <NA>  s  r  1  5  3
4 <NA> as  3  2  4  5
7 <NA> da  r  3  6  3

OR

To select non empty rows:

x <- subset(df, complete.cases(df))

Output:

  D1 D2 D3 A1 A2 A3
1  a  f  a  0  2  4
3  b da  d 12  3  5
5  c ad  f  4  6  7
6  d da  d  3  5  6
TarJae
  • 72,363
  • 6
  • 19
  • 66
1

Maybe it was read as NA, then use is.na to detect the NA elements

subset(df, is.na(D1))

Or if there are whitespaces, i.e. " ", use trimws to remove those and use ==

subset(df, trimws(D1) == "")

Or if it is both (just to generalize), then do a | condition

subset(df, is.na(D1)|trimws(D1) == "")
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thanks for the answer! From what I can see she's deleting the information that contains empty cells in D1, isn't it? In case, I wanted to select only those values that contain empty cells in D1. –  Aug 13 '21 at 18:07
  • @AntonioPedro You are right. Can you try with the update. I assume either this as `NA` elements or it is not completely blank, i.e. there are some whitespace like `" "`, in that case do `subset(df, nzchar(trimws(D1)))` – akrun Aug 13 '21 at 18:08