Judith Flores
2008-Apr-23 23:19 UTC
[R] Replecing empty values with a letter (continuation)
Sorry, I hit the send botton by accident. Here is my code so far: dat<-read.csv('myfile.csv', na.strings="") dat[is.na(dat]<-'N' But it doesn't replace the <NA> for the letter 'N'. I am using R v 2/7/0, running it under Windows XP. Thank you, Judith ____________________________________________________________________________________ Be a better friend, newshound, and
David Winsemius
2008-Apr-24 01:33 UTC
[R] Replecing empty values with a letter (continuation)
Judith Flores <juryef at yahoo.com> wrote in news:32500.45083.qm at web34708.mail.mud.yahoo.com:> Sorry, I hit the send botton by accident. Here is my > code so far: > > dat<-read.csv('myfile.csv', na.strings="") > dat[is.na(dat]<-'N' > > But it doesn't replace the <NA> for the letter 'N'. > >What happens when you just type: dat[is.na(dat)] #? My guess is that you don't have any, since na.strings="" told read.table that there were no values that should be assigned <NA>. It is possible to have a value of "NA" that is not <NA>.> x <- c("NA", 1 , 2)#NA is not <NA>> is.na(x)[1] FALSE FALSE FALSE> txt <- "nnn 1 2 3 4 5" > xt<- read.table(textConnection(txt), header=FALSE) > xtV1 V2 V3 V4 V5 V6 1 nnn 1 2 3 4 5> xt<- read.table(textConnection(txt), header=FALSE,na.strings="nnn") > xtV1 V2 V3 V4 V5 V6 1 NA 1 2 3 4 5 #That is a TRUE >NA>> is.na(xt)V1 V2 V3 V4 V5 V6 [1,] TRUE FALSE FALSE FALSE FALSE FALSE> xt[is.na(xt)] <- "N" > xtV1 V2 V3 V4 V5 V6 1 N 1 2 3 4 5 -- David Winsemius
Peter Alspach
2008-Apr-24 02:00 UTC
[R] Replecing empty values with a letter (continuation)
Judith I think you'll find the issue is that read.csv creates a data.frame and your subsetting syntax dat[is.na(dat)] is assuming it to be a matrix. If this is the case, you have two options: Coerce dat into a matrix with as.matrix(dat); Use apply() to replace the missing data for each column in turn ... apply(dat, 2, function(x) x[is.na(x)] <- 'N') HTH ....... Peter Alspach> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Judith Flores > Sent: Thursday, 24 April 2008 11:20 a.m. > To: RHelp > Subject: [R] Replecing empty values with a letter (continuation) > > Sorry, I hit the send botton by accident. Here is my code so far: > > dat<-read.csv('myfile.csv', na.strings="") dat[is.na(dat]<-'N' > > But it doesn't replace the <NA> for the letter 'N'. > > > I am using R v 2/7/0, running it under Windows XP. > > Thank you, > > Judith > > > > ______________________________________________________________ > ______________________ > Be a better friend, newshound, and > > ______________________________________________ > R-help at 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. >The contents of this e-mail are privileged and/or confidential to the named recipient and are not to be used by any other person and/or organisation. If you have received this e-mail in error, please notify the sender and delete all material pertaining to this e-mail.