Problem avec subset

Hello everybody,

I have some of trouble with subset function.

    descrip <- str_replace_all(descrip, "[\r\n\t]" , "")
    
    # Transform on data frame
    descrip <- data.frame(descrip) 
    
    # Erase the empty element in the data frame
    
    for(i in 1:nrow(descrip)){
      
      if(isTRUE(descrip[i,1] == "")){
        descrip = subset(descrip, select = descrip[[i]][1]) 
      } 
      i = i + 1
    }

But i just want to erase one line but the all of my data are erased...

So how can i do it ?

There is no need to use a for loop. Many functions in R are vectorized. Here are three ways to filter out rows where the first column is an empty string.

suppressPackageStartupMessages(library(dplyr))
df <- data.frame(A = c("Q", "W", "", "E"), B = 1:4)
df
#>   A B
#> 1 Q 1
#> 2 W 2
#> 3   3
#> 4 E 4
#filter using dplyr::filter
df2 <- filter(df, A != "")
df2 
#>   A B
#> 1 Q 1
#> 2 W 2
#> 3 E 4

#filter using []
df3 <- df[df$A != "", ]
df3
#>   A B
#> 1 Q 1
#> 2 W 2
#> 4 E 4

#filter with subset
df4 <- subset(df, A != "")
df4
#>   A B
#> 1 Q 1
#> 2 W 2
#> 4 E 4

Created on 2019-06-23 by the reprex package (v0.2.1)
Note that an empty string is not the same as a missing value, represented by NA.

2 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.