How to convert blanks to NA

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How to convert blanks to NA

mtb954
Hi R-helpers,

I have imported data from Excel using the following code:

library(xlsReadWrite)
data <- read.xls(data,colClasses=c("character"))

and this results in all of the empty (blank) cells in the imported
Excel file also being empty (blank) in the resulting dataframe.

I am not used to having blanks (rather NAs) and I think these are
caused by the colClasses argument.

I would like to convert all the empty (blank) cells in my dataframe to NA.

Thanks for any help you might provide.

Mark

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to convert blanks to NA

John Kane-2

Does this do what you want

x <- data.frame(1,"" , 5)
x[x==""]  <- NA
x

where x is substituted for your 'data'?




----- Original Message ----
From: Mark Na <[hidden email]>
To: [hidden email]
Sent: Tuesday, June 2, 2009 3:14:00 PM
Subject: [R] How to convert blanks to NA

Hi R-helpers,

I have imported data from Excel using the following code:

library(xlsReadWrite)
data <- read.xls(data,colClasses=c("character"))

and this results in all of the empty (blank) cells in the imported
Excel file also being empty (blank) in the resulting dataframe.

I am not used to having blanks (rather NAs) and I think these are
caused by the colClasses argument.

I would like to convert all the empty (blank) cells in my dataframe to NA.

Thanks for any help you might provide.

Mark

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to convert blanks to NA

Henrique Dallazuanna
In reply to this post by mtb954
Try this:

sapply(x, function(f){is.na(f)<-which(f == '');f})

On Tue, Jun 2, 2009 at 4:14 PM, Mark Na <[hidden email]> wrote:

> Hi R-helpers,
>
> I have imported data from Excel using the following code:
>
> library(xlsReadWrite)
> data <- read.xls(data,colClasses=c("character"))
>
> and this results in all of the empty (blank) cells in the imported
> Excel file also being empty (blank) in the resulting dataframe.
>
> I am not used to having blanks (rather NAs) and I think these are
> caused by the colClasses argument.
>
> I would like to convert all the empty (blank) cells in my dataframe to NA.
>
> Thanks for any help you might provide.
>
> Mark
>
> ______________________________________________
> [hidden email] 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.
>


--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to convert blanks to NA

Hans-Peter Suter
In reply to this post by mtb954
2009/6/2 Mark Na <[hidden email]>:
> library(xlsReadWrite)
> data <- read.xls(data,colClasses=c("character"))
>
> and this results in all of the empty (blank) cells in the imported
> Excel file also being empty (blank) in the resulting dataframe.

The pro version has an 'naStrings' argument. But this is something
that should be supported since longtime also in the normal
xlsReadWrite. Thanks for reminding me  :-)  and it's now - at least -
in my Redmine tracker.

Cheers,
Hans-Peter

______________________________________________
[hidden email] 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.