I have a data frame. It has lots of patient information, their age, their gender, etc etc. I need to keep all this information whilst selecting relevant rows. So, in the example of code I provided I want to remove all those patients who have entry N in the column with.Wcode. The dimension of the data is 378 i.e. 378 patients and currently I am replacing any entries in column with.Wcode with the letter O as this is another level of the same column. Does that make more sense? nep <- function(data) { dummy <- rep(0,378) for(i in 1:378){ if(is.na(data$with.Wcode)[i]) data$with.Wcode[i] <- "O" } for(i in 1:378){ if(data$with.Wcode[i]=="N") dummy[i] <- i } return(data[-dummy,]) }
How can I therefore not replace NA with level O but instead, ignore the NAs and effectively gloss over them? Thank you, Laura On Mon, Oct 13, 2008 at 12:42 PM, jim holtman <[EMAIL PROTECTED]> wrote: > Not sure exactly what you are trying to do since you did not provide > commented, minimal, self-contained, reproducible code. Let me take a > guess in that you also have to test for NAs: > > > x <- sample(c("N", "A", "B", NA), 20, TRUE) > > x > [1] "A" "A" "B" NA "N" NA NA "B" "B" "N" "N" "N" "B" "A" NA "A" > "B" NA "A" NA > > x != "N" > [1] TRUE TRUE TRUE NA FALSE NA NA TRUE TRUE FALSE FALSE > FALSE TRUE TRUE NA TRUE TRUE NA > [19] TRUE NA > > x[x != "N"] > [1] "A" "A" "B" NA NA NA "B" "B" "B" "A" NA "A" "B" NA "A" NA > > (!is.na(x)) & (x != "N") > [1] TRUE TRUE TRUE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE > FALSE TRUE TRUE FALSE TRUE TRUE FALSE > [19] TRUE FALSE > > x[(!is.na(x)) & (x != "N")] > [1] "A" "A" "B" "B" "B" "B" "A" "A" "B" "A" > > > > > On Mon, Oct 13, 2008 at 7:15 AM, Laura Bonnett > <[EMAIL PROTECTED]> wrote: > > Hi All, > > > > I have a data frame which has columns comprised mainly of "NA"s. I know > > there are functions na.pass and na.omit etc which can be used in these > > situations however I can't them to work in this case. I have a function > > which returns the data according to some rule i.e. removal of N in this > > code: > > > > nep <- function(data) > > { > > dummy <- rep(0,378) > > for(i in 1:378){ > > if(is.na(data$with.Wcode)[i]) > > data$with.Wcode[i] <- "O" > > } > > for(i in 1:378){ > > if(data$with.Wcode[i]=="N") > > dummy[i] <- i > > } > > return(data[-dummy,]) > > } > > > > However, I really don't want to replace the NAs with "O". I'd just like > to > > gloss over them. I can't just delete them because the structure of the > data > > frame needs to be maintained. Can anyone suggest how I can write in a > line > > or two to ignore the NAs instead of replacing them? I've tried this code > > but it doesn't work! > > > > nep <- function(data) > > { > > dummy <- rep(0,378) > > for(i in 1:378){ > > na.pass(data$with.Wcode[i]) > > if(data$with.Wcode[i]=="N") > > dummy[i] <- i > > } > > return(data[-dummy,]) > > } > > > > > > Thank you, > > > > Laura > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help@r-project.org mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > > > > > > -- > Jim Holtman > Cincinnati, OH > +1 513 646 9390 > > What is the problem that you are trying to solve? > [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.