Change negative values in column

3 messages Options
Embed this post
Permalink
Joel Fürstenberg-Hägg

Change negative values in column

Reply Threaded More More options
Print post
Permalink

Hi all,

 

I'm trying to write a script that changes all negative values in a data frame column to a small positive value, based on the the minimum value of the column.

However, I get the following error:

 

Error in if (x[i] < 0) { : argument is of length zero

 

 

As well, I would "minimum" to be the smallest of the non-negative values...

 

 

Aa_non_neg=(fieldTrial0809$Aa) # Copy column from data frame to manipulate

 

nonNegative = function(x)
{
   minimum=min(x) # Should only use positive minimum!
   for (i in x)
   {

      if(x[i]<0) # Found a negative value
      {
         x[i]=minimum/10 # Change to a new non-negative value
      }
   }
}

 

nonNegative(Aa_non_neg) # Apply function on column
     
_________________________________________________________________
Lagra alla dina foton på Skydrive. Det är enkelt och säkert!
http://www.skydrive.live.com
        [[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.
Steve Lianoglou-6

Re: Change negative values in column

Reply Threaded More More options
Print post
Permalink
Hi,

On Nov 3, 2009, at 9:56 AM, Joel Fürstenberg-Hägg wrote:

> Hi all,
>
> I'm trying to write a script that changes all negative values in a  
> data frame column to a small positive value, based on the the  
> minimum value of the column.
>
> However, I get the following error:
>
> Error in if (x[i] < 0) { : argument is of length zero

This is telling you that x[i] is a zero length object, so you're  
indexing is wrong

> As well, I would "minimum" to be the smallest of the non-negative  
> values...
>
> Aa_non_neg=(fieldTrial0809$Aa) # Copy column from data frame to  
> manipulate
>
> nonNegative = function(x)
> {
>   minimum=min(x) # Should only use positive minimum!

You have to tell R to only use positive numbers, min(x[x > 0]) would  
have worked.

I'm not really sure what you're looping over below.

>   for (i in x)
>   {
>
>      if(x[i]<0) # Found a negative value
>      {
>         x[i]=minimum/10 # Change to a new non-negative value
>      }
>   }
> }

This will work over each column of an "orig.df" data.frame (assumes  
all cols are numeric) and generate the new data.frame you're looking  
for:

new.df <- as.data.frame(apply(orig.df, 2, function(col) {
   min.val <- min(col[col > 0])
   col[col < 0] <- min.val
   col
}))

-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.
Steve Lianoglou-6

Re: Change negative values in column

Reply Threaded More More options
Print post
Permalink
In reply to this post by Joel Fürstenberg-Hägg
Hi Joel,

On Nov 3, 2009, at 11:30 AM, Joel Fürstenberg-Hägg wrote:
> > > However, I get the following error:
> > >
> > > Error in if (x[i] < 0) { : argument is of length zero
> >
> > This is telling you that x[i] is a zero length object, so you're
> > indexing is wrong
> >
>
> Doesn't x[i] means index i in vector x?

Yes.

> Or could it be that there's a missing value at that index?

Don't think so: NA < 0 returns NA, not an error regarding the  
argument's length in your `if` clause.

> How do I deal with them in that case? I need to keep the length of  
> the vector so I cannot remove NAs if that affects the length...

I'm not sure how to answer this. Just try to learn a bit more about  
how to index using integers, vectors of integers, and vectors of  
logical/boolean values.

> > This will work over each column of an "orig.df" data.frame (assumes
> > all cols are numeric) and generate the new data.frame you're looking
> > for:
> >
> > new.df <- as.data.frame(apply(orig.df, 2, function(col) {
> > min.val <- min(col[col > 0])
> > col[col < 0] <- min.val
> > col
> > }))
> >
>
> Ok, that's exactly what I was aming for! Thanks a lot!!

Great. Don't just use that code though, understand what it's doing ..  
it'll help you to slice and dice your data in the future.

If you have any questions regarding the details of that code snippet  
that you can't figure out after careful inspection, feel free to ask.

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