Remove all lines with specific text in column in a dataframe

Hello everybody,

First of all, thanks for your help an other once, and I hope I will understandable (I'm French PhD Student). I hope I ask the question in the correct section.

I give you my R code and I explain my problem :

a9g_test = a9g

numLine = 1

for (line in a9g_test$Differency.Time) {
  if (is.character(a9g_test[numLine, 2]) == "N/A") {
            a9g_test = a9g_test[-c(numLine), ]
  }
  numLine = numLine + 1
}

This above code doesn't works. When I run it, it don't remove lines with "N/A". I don't understand why.

My objective : I want to remove all lines with "N/A" in my dataframe (the text "N/A" is in my second column).

Thank you an other once for your help.

Yours sincerely,

GIOVANNANGELI Cyril

Try

na.omit(dataframe)

Does na.omit require NA, not N/A?

a9g_test <- data.frame(col1=seq(1:9), col2=c(
"Yesterday",
"All my troubles seemed so far away",
"Now it looks as though they're here to stay",
"Oh, I believe in yesterday",
"Verse 2, N/A",
"Suddenly",
"I'm not half the man I used to be",
"There's a shadow hanging over me",
"Oh, yesterday came suddenly")
)

a9g_test <- a9g_test[!grepl("N/A", a9g_test$col2),]
a9g_test

You're right. I misread the question.

Since Cyril says all the N/As are in the second column we could use the nanair
package to replace the N?A's with NA
and then use na.omit()

library(tidyverse)
library(naniar)

dat1  <- structure(list(cat = c(1, 3, 2), dog = c("2", "N/A", "N/A"), 
    rabbit = c(9, 9, 5)), row.names = c(NA, -3L), spec = structure(list(
    cols = list(cat = structure(list(), class = c("collector_double", 
    "collector")), dog = structure(list(), class = c("collector_character", 
    "collector")), rabbit = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), delim = ","), class = "col_spec"), problems = <pointer: 0x5645710ddd10>, class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"))

dat2  <- dat1  %>%  replace_with_na(replace = list(dog = "N/A"))

Hello fcas80,

Thank you very much for your answer, it works perfectly !!!! Thanks to you I can continue my analyses !!!

Thank you also jrkrideau for your answer !

Yours sincerely,

GIOVANNANGELI Cyril

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.