Home > other >  removing blank lines on R
removing blank lines on R

Time:06-23

i am working on a dataset with blanks i would like to remove (i would like to remove the whole line when there is a blank in a column "Code.3"). My dataframe looks like this :

Type                  Code.3             CA

Hospit                CH5                50
Hospit                CH6                55
Hospit                ***                10
Hospit                                   115
Amb                   DH7                30    

i have tried the following code, without success (error message appears) :

library(dplyr)
 df %>%
  filter(!Code.3 == missing)

As well as a for loop :

df_cleaned <- list()
library(dplyr)

 for (i in 1:319){
  if(is_empty(df[i,5]) = TRUE)
   {df <- df[-i,]}
  else{next}
  df_cleaned <- append(df_cleaned, list(df_cleaned, df_cleaned[,5]))
}

df_cleaned

Being a beginner on R, im pretty sure my for loop is a disaster. Could anyone help ?

CodePudding user response:

First we could replace "" to NAand then filter:

library(dplyr)

df %>%
  mutate(Code.3 = na_if(Code.3, "")) %>% 
  filter(!is.na(Code.3))

CodePudding user response:

Try this:

library(dplyr)
df %>%
  filter(Code.3 != "")

CodePudding user response:

base R option:

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