D.A. Ragland
D.A. Ragland

Reputation: 103

Defining and handling specific Na values for a function R

I'm currently in the process of writing a function in R. Apart of which I will be needing to define certain character's as NA.

Starting from the beginning

file <- readLines("/Users/debraragland/file.txt")
relines <- gsub("\\Frame.+$|\\Chain.+$|\\ATOM-WISE.+$", "", file)

Which leaves "" and " ". If I do each step at the prompt, I have to define and omit these as NAs twice

relines[relines==""] <- NA
relines <-na.omit(relines)

relines[relines==" "] <- NA
relines <- na.omit(relines)

Is there any way I can combine these into one, possible into an if statement?

Upvotes: 0

Views: 33

Answers (2)

ila
ila

Reputation: 766

relines <- relines[relines != "" | relines != " "]

Upvotes: 0

blakeoft
blakeoft

Reputation: 2400

I think this will do what you want:

relines[relines == "" | relines == " "] <- NA
relines <- na.omit(relines[!(relines == "" | relines == " ")])

Upvotes: 3

Related Questions