0

I have a list consisting of a total of 24 dataframes of various row numbers. Some of these rows have empty values, i.e. neither NA or NULL, just "". The number of such rows varies between the dataframes, but I want to remove these rows.

Sample dataframe, but this is pretty much how all the dataframes in the list looks like, with lots of empty values, and a few values I want to keep.

>df <- data.frame(c("","","","A","","B","","","C"))
colnames(df) <- "sn"

> df
   sn
1   
2   
3   
4  A
5   
6  B
7   
8   
9  C

I've tried to delete these rows directly, according to this page or this page, adding NA's to the empty rows from this page, before omitting those rows, and even test[complete.cases(df), ] from here. None of this seems to work, as nothing happens to neither of the dataframes within the list. I've tried for just one of the dataframes within the list as well, like the sample one shown here, but just with more rows, but still no change.

From various pages here on Stack Overflow, these are a few of the codes I've tried to solve the

1) df <- lapply(df, function(x) sapply(df, nrow)>0)

2) lapply(df, function(x){ df[rowSums(is.na(df)) != ncol(df),]})

3) df[!apply(df == "", 1, all),]

4) df[rowSums(df=="")!=ncol(df), ]

5) df[apply(df, 1, function(x) any(x != '')), ]

6a) df[df==""]<-NA

6b) df[complete.cases(df),]

All of these attempts have unfortunately done nothing with the dataframe, leaving it identical as listed above.

Any suggestions? Thank you very much in advance!

OMMJREN
  • 3
  • 4
  • Are your sure you see *NULL* in rows? And do you want to remove where NAs and empties are in at least one or ALL rows? – Parfait Apr 11 '18 at 16:51
  • na.omit(list) should be `lapply(list, na.omit)`. Or just don't use a list; use a single table if all of them have the same columns... Btw, re the PS, you do not need to give us exactly your data, but if you could build an example, it should help. Guidance here: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/28481250#28481250 – Frank Apr 11 '18 at 16:52
  • 1
    And yes you should have included code and a [reproducible data example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). While this may be your first written post, surely it is not your first read post where many R questions contain such components! – Parfait Apr 11 '18 at 16:53
  • @Parfait,I do not see NULL in rows, they're just empty, blanks. And to answer your question; No, it is not my first read post, but reading the posts have not really helped me understand how I do it myself on this page. – OMMJREN Apr 11 '18 at 16:56
  • 1
    https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example, https://stackoverflow.com/help/how-to-ask, and/or https://stackoverflow.com/help/mcve – r2evans Apr 11 '18 at 16:57
  • @Frank Thanks for the link! I'll definitely use it, and hopefully next time I ask a question, I'll do it the proper way. – OMMJREN Apr 11 '18 at 16:58

1 Answers1

0

Consider a combination of those attempted solutions. Because complete.cases() checks for NA in all columns and empty strings values are NOT the same as NA, we reassign values and then run completed.cases. Below data.frame() is called to avoid vector return due to the single column (remove call for more than one column).

df_list <- list(
     data.frame(sn=c("","","","A","","B","","","C")),
     data.frame(sn=c("","","","A","","B","","","C")),
     data.frame(sn=c("","","","A","","B","","","C"))
)

new_df_list <- lapply(df_list, function(df) {
  df[df == ""] <- NA
  data.frame(sn = df[complete.cases(df),])   
})

new_df_list
# [[1]]
#   sn
# 1  A
# 2  B
# 3  C

# [[2]]
#   sn
# 1  A
# 2  B
# 3  C

# [[3]]
#   sn
# 1  A
# 2  B
# 3  C
Parfait
  • 87,576
  • 16
  • 87
  • 105
  • Thank you very much for your help! Although this worked for the sample dataset, this did unfortunately not seem to work with my list of dataframes. I don't know why, but I think there was something wrong with the preparation of my dataset, so I took a second look at it, and now it works perfectly! Thank you again! – OMMJREN Apr 12 '18 at 08:15