NA values

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

NA values

Arun.stat
Dear all R users,

I am wondering whether there is any way to replace all "NA" values in a data
frame by some numerical value, suppose 1000?

Thanks and Regards

        [[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
Reply | Threaded
Open this post in threaded view
|

Re: NA values

PIKAL Petr
Hi

yes, but I ****do not**** recommend you to do it

> df<-data.frame(a=1:10, b=11:20)
> df[5,2]<-NA
> df
    a  b
1   1 11
2   2 12
3   3 13
4   4 14
5   5 NA
6   6 16
7   7 17
8   8 18
9   9 19
10 10 20
> df[is.na(df)]<-1000
> df
    a    b
1   1   11
2   2   12
3   3   13
4   4   14
5   5 1000
6   6   16
7   7   17
8   8   18
9   9   19
10 10   20
>

but in that case you can not simply compute means, sums and other
values just by e.g.

colSums(df, na.rm=T)

HTH
Petr





On 12 Jun 2006 at 18:18, Arun Kumar Saha wrote:

Date sent:       Mon, 12 Jun 2006 18:18:33 +0530
From:           "Arun Kumar Saha" <[hidden email]>
To:             "[hidden email]" <[hidden email]>
Subject:         [R] NA values

> Dear all R users,
>
> I am wondering whether there is any way to replace all "NA" values in
> a data frame by some numerical value, suppose 1000?
>
> Thanks and Regards
>
>  [[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

Petr Pikal
[hidden email]

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

Re: NA values

Duncan Murdoch
In reply to this post by Arun.stat
On 6/12/2006 8:48 AM, Arun Kumar Saha wrote:
> Dear all R users,
>
> I am wondering whether there is any way to replace all "NA" values in a data
> frame by some numerical value, suppose 1000?

In a vector it is easy, e.g. x[is.na(x)] <- 1000.  A dataframe is a list
of vectors, so you could iterate through the list, using one of the
apply functions (or even a for loop):

apply(x, 2, function(col) {col[is.na(col)] <- 1000; col} )

which is essentially a short form for

for (i in 1:ncol(x)) {
   col <- x[,i]
   col[is.na(col)] <- 1000
   x[,i] <- col
}

Duncan Murdoch

>
> Thanks and Regards
>
> [[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

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

Re: NA values

Gavin Simpson
In reply to this post by Arun.stat
On Mon, 2006-06-12 at 18:18 +0530, Arun Kumar Saha wrote:
> Dear all R users,
>
> I am wondering whether there is any way to replace all "NA" values in a data
> frame by some numerical value, suppose 1000?
>
> Thanks and Regards

Hi, Arun,

sapply(dat, function(x) {x[is.na(x)] <- 1000; x})

e.g.:

## some dummy data
dat <- as.data.frame(matrix(rnorm(100), ncol = 10))
## add some NAs
samp <- sample(1:10, 4)
dat[samp, -samp] <- NA
dat
## replace NAs with 1000
dat <- sapply(dat, function(x) {x[is.na(x)] <- 1000; x})
dat

HTH

G
--
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
*  Note new Address, Telephone & Fax numbers from 6th April 2006  *
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Gavin Simpson
ECRC & ENSIS                  [t] +44 (0)20 7679 0522
UCL Department of Geography   [f] +44 (0)20 7679 0565
Pearson Building              [e] gavin.simpsonATNOSPAMucl.ac.uk
Gower Street                  [w] http://www.ucl.ac.uk/~ucfagls/cv/
London, UK.                   [w] http://www.ucl.ac.uk/~ucfagls/
WC1E 6BT.
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%

______________________________________________
[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