Quantcast

How to replace all <NA> values in a data.frame with another ( not 0) value

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to replace all <NA> values in a data.frame with another ( not 0) value

Nevil Amos
I need to replace <NA> occurrences in multiple columns  in a data.frame
with "000/000"

how do I achieve this?

Thanks

Nevil Amos

______________________________________________
[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
|  
Report Content as Inappropriate

Re: How to replace all <NA> values in a data.frame with another ( not 0) value

Lanna Jin
Try: "x[which(is.na(x)),] <- 000/000", where is  x is your data frame
Lanna
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to replace all <NA> values in a data.frame with another ( not 0) value

Lanna Jin
In reply to this post by Nevil Amos
Whoops, my bad.  Maybe try using "gsub"
Lanna
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to replace all <NA> values in a data.frame with another ( not 0) value

Muhammad Rahiz
In reply to this post by Nevil Amos
Hi Nevil,

You can try a method like this

x <- c(rnorm(5),rep(NA,3),rnorm(5)) # sample data
dat <- data.frame(x,x)                       # make sample dataframe
dat2 <- as.matrix(dat)                     # conver to matrix
y <- which(is.na(dat)==TRUE)         # get index of NA values
dat2[y] <- "000/000"                         # replace all na values
with "000/000"

Muhammad






Nevil Amos wrote:

> I need to replace <NA> occurrences in multiple columns  in a data.frame
> with "000/000"
>
> how do I achieve this?
>
> Thanks
>
> Nevil Amos
>
> ______________________________________________
> [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
|  
Report Content as Inappropriate

Re: How to replace all <NA> values in a data.frame with another ( not 0) value

John Kane-2
In reply to this post by Nevil Amos
?replace


Something like this should work

replace(df1, is.na(df1), "000/000")

--- On Tue, 5/4/10, Nevil Amos <[hidden email]> wrote:

> From: Nevil Amos <[hidden email]>
> Subject: [R] How to replace all <NA> values in a data.frame with another ( not 0) value
> To: [hidden email]
> Received: Tuesday, May 4, 2010, 8:54 AM
> I need to replace <NA>
> occurrences in multiple columnsĀ  in a data.frame with
> "000/000"
>
> how do I achieve this?
>
> Thanks
>
> Nevil Amos
>
> ______________________________________________
> [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
|  
Report Content as Inappropriate

Re: How to replace all <NA> values in a data.frame with another ( not 0) value

Muhammad Rahiz
In reply to this post by Lanna Jin
000/000 returns NaN, which is no different than NA unless you want it as
string i.e. "000/000"

Muhammad





Lanna Jin wrote:
> Try: "x[which(is.na(x)),] <- 000/000", where is  x is your data frame
>
> -----
> Lanna Jin
>
> [hidden email]
> 510-898-8525
>

______________________________________________
[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
|  
Report Content as Inappropriate

Re: How to replace all <NA> values in a data.frame with another ( not 0) value

Bart Joosen
In reply to this post by Nevil Amos
try x[is.na(x)] <- "000/000"

Bart
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Odp: How to replace all <NA> values in a data.frame with another ( not 0) value

PIKAL Petr
In reply to this post by Nevil Amos
Hi

[hidden email] napsal dne 04.05.2010 14:54:14:

> I need to replace <NA> occurrences in multiple columns  in a data.frame
> with "000/000"

Be careful if you replace NA in numeric columns.

> str(test)
'data.frame':   10 obs. of  3 variables:
 $ mp: num  20.9 19.9 20.1 20.2 18.9 ...
 $ so: num  18.8 18.6 18.2 17.9 18.1 ...
 $ le: num  48 49.1 48.8 42.6 46.1 ...
> test[2,2] <- NA
> test[is.na(test)] <- "000/000"
> str(test)
'data.frame':   10 obs. of  3 variables:
 $ mp: num  20.9 19.9 20.1 20.2 18.9 ...
 $ so: chr  "18.75" "000/000" "18.25" "17.89" ...
 $ le: num  48 49.1 48.8 42.6 46.1 ...
>

numeric column is now character and you can not use it for further
analysis without some fiddling around.

Regards
Petr



>
> how do I achieve this?
>
> Thanks
>
> Nevil Amos
>
> ______________________________________________
> [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
|  
Report Content as Inappropriate

Re: How to replace all <NA> values in a data.frame with another ( not 0) value

Nevil Amos
In reply to this post by Muhammad Rahiz
Thanks, this works, replacing the NA values with the "000/000' string.

On 4/05/2010 11:20 PM, Muhammad Rahiz wrote:

> Hi Nevil,
>
> You can try a method like this
>
> x <- c(rnorm(5),rep(NA,3),rnorm(5)) # sample data
> dat <- data.frame(x,x)                       # make sample dataframe
> dat2 <- as.matrix(dat)                     # conver to matrix
> y <- which(is.na(dat)==TRUE)         # get index of NA values
> dat2[y] <- "000/000"                         # replace all na values
> with "000/000"
>
> Muhammad
>
>
>
>
>
>
> Nevil Amos wrote:
>> I need to replace <NA> occurrences in multiple columns  in a
>> data.frame with "000/000"
>>
>> how do I achieve this?
>>
>> Thanks
>>
>> Nevil Amos
>>
>> ______________________________________________
>> [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
|  
Report Content as Inappropriate

Re: How to replace all <NA> values in a data.frame with another ( not 0) value

asbidyarthy
This post has NOT been accepted by the mailing list yet.
In reply to this post by Muhammad Rahiz
Thanks Rahiz, It helped. Converting a dataframe into matrix and then deal with <NA> is tricky.

--
Ajay
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to replace all <NA> values in a data.frame with another ( not 0) value

asbidyarthy
This post has NOT been accepted by the mailing list yet.
If you are looking for replacing <NA> in a particular column and use this:
Let you want to replace <NA> from third column of the dataframe then-

data <- read.csv('data.csv')
data = as.matrix(data)
indx <- which(is.na(data[, 3])==TRUE)
data[indx, 3] = "NAReplaced"
data = data.frame(data)

--
Ajay
Loading...