Replace only first NA in column

4 messages Options
Embed this post
Permalink
bikemike42

Replace only first NA in column

Reply Threaded More More options
Print post
Permalink
Dear All,

I am trying to fill in a blank vector ("a") with one value at a time, with the value of the number of rows in a randomized dataset with rowSums=0.  Below is the code I've got so far, but what I want to be as the last line is " a[1st NA,]=nz"
such that this will run until all of my NAs are replaced with an integer value based on randomizations.

a<-matrix(nrow=1000,ncol=1)
while(sum(is.na(a)>0))
        {
        z<-matrix(rbinom(832, 1, prob = 0.048), nrow = 32)
        rs<-rowSums(z)
        nz<-length(rs[rs==0])
        a[,]=nz
               
}

Thank you for your help!
Mike
Erik Iverson-2

Re: Re place only first NA in column

Reply Threaded More More options
Print post
Permalink
Mike,

Is this what you're trying to do, it avoids the awkward use of a while loop, taking advantage of the ability to vectorize R functions.

fun <- function() {
  z <- matrix(rbinom(832, 1, prob = 0.048), nrow = 32)
  sum(rowSums(z) == 0)
}

a <- as.matrix(replicate(1000, fun()))



> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]]
> On Behalf Of bikemike42
> Sent: Tuesday, November 03, 2009 2:58 PM
> To: [hidden email]
> Subject: [R] Re place only first NA in column
>
>
> Dear All,
>
> I am trying to fill in a blank vector ("a") with one value at a time, with
> the value of the number of rows in a randomized dataset with rowSums=0.
> Below is the code I've got so far, but what I want to be as the last line
> is
> " a[1st NA,]=nz"
> such that this will run until all of my NAs are replaced with an integer
> value based on randomizations.
>
> a<-matrix(nrow=1000,ncol=1)
> while(sum(is.na(a)>0))
> {
> z<-matrix(rbinom(832, 1, prob = 0.048), nrow = 32)
> rs<-rowSums(z)
> nz<-length(rs[rs==0])
> a[,]=nz
>
> }
>
> Thank you for your help!
> Mike
> --
> View this message in context: http://old.nabble.com/Replace-only-first-NA-
> in-column-tp26163589p26163589.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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.
Steve Lianoglou-6

Re: Re place only first NA in column

Reply Threaded More More options
Print post
Permalink
In reply to this post by bikemike42
Hi,

On Nov 3, 2009, at 3:58 PM, bikemike42 wrote:

>
> Dear All,
>
> I am trying to fill in a blank vector ("a") with one value at a  
> time, with
> the value of the number of rows in a randomized dataset with  
> rowSums=0.
> Below is the code I've got so far, but what I want to be as the last  
> line is
> " a[1st NA,]=nz"
> such that this will run until all of my NAs are replaced with an  
> integer
> value based on randomizations.
>
> a<-matrix(nrow=1000,ncol=1)
> while(sum(is.na(a)>0))
> {
> z<-matrix(rbinom(832, 1, prob = 0.048), nrow = 32)
> rs<-rowSums(z)
> nz<-length(rs[rs==0])
> a[,]=nz
>
> }
>
> Thank you for your help!

It's not really clear to me what you want to actually replace each NA  
value with. But it sounds like your 'general' question is how to find  
the index of the first value of a vector that is NA.

Let's say your `a` vector has NA values you want to replace, one by one.

any.na <- any(is.na(a))
while (any.na) {
   first.na <- which(is.na(a))[1]
   a[first.na] <- your.NA.replacing.function()
   any.na <- any(is.na(a))
}

HTH,
-steve

--
Steve Lianoglou
Graduate Student: Computational Systems Biology
   |  Memorial Sloan-Kettering Cancer Center
   |  Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact

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

Re: Re place only first NA in column

Reply Threaded More More options
Print post
Permalink
Erik and Steve,
Thanks again for your help, both solutions do exactly what I need.
Cheers,
Mike

Steve Lianoglou wrote:

> Hi,
>
> On Nov 3, 2009, at 3:58 PM, bikemike42 wrote:
>
>>
>> Dear All,
>>
>> I am trying to fill in a blank vector ("a") with one value at a time,
>> with
>> the value of the number of rows in a randomized dataset with rowSums=0.
>> Below is the code I've got so far, but what I want to be as the last
>> line is
>> " a[1st NA,]=nz"
>> such that this will run until all of my NAs are replaced with an integer
>> value based on randomizations.
>>
>> a<-matrix(nrow=1000,ncol=1)
>> while(sum(is.na(a)>0))
>>     {
>>     z<-matrix(rbinom(832, 1, prob = 0.048), nrow = 32)
>>     rs<-rowSums(z)
>>     nz<-length(rs[rs==0])
>>     a[,]=nz
>>        
>> }
>>
>> Thank you for your help!
>
> It's not really clear to me what you want to actually replace each NA
> value with. But it sounds like your 'general' question is how to find
> the index of the first value of a vector that is NA.
>
> Let's say your `a` vector has NA values you want to replace, one by one.
>
> any.na <- any(is.na(a))
> while (any.na) {
>   first.na <- which(is.na(a))[1]
>   a[first.na] <- your.NA.replacing.function()
>   any.na <- any(is.na(a))
> }
>
> HTH,
> -steve
>
> --
> Steve Lianoglou
> Graduate Student: Computational Systems Biology
>   |  Memorial Sloan-Kettering Cancer Center
>   |  Weill Medical College of Cornell University
> Contact Info: http://cbio.mskcc.org/~lianos/contact
>
>

--
Michael L. Treglia
Graduate Student
Wildlife and Fisheries Sciences
Texas A&M University
Lab: (979)862-7245
Cell: (917)841-5603
[hidden email]
http://people.tamu.edu/~mlt35

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