Wednesday, 1 April 2015

Turning negative values to zero....

I am working with Jo Welton to explore her proteomics data.
Our first step is to turn the negative values from her data into zeros. The negative values are there because she has subtracted backgrounds.

As ever, StackOverflow is our friend. I found this link:
http://stackoverflow.com/questions/11275187/r-replacing-negative-values-by-zero

Steps we followed:

  1. Convert the column of our data frame into a vector
  2. Go through the vector, find the values less than zero and make them zero
  3. Convert our vector back to a data frame. 
  4. Bind our data frames together. 
Here is the code:

> mUr.zero <- (dataframe$col) #makes the vector
> mUr.zero[mUr.zero <0] <- 0 # loops through the vector, finds values less than zero and replaces them with zero. 
> mUr.zero <- as.data.frame(mUr.zero) # converts into a data frame
> newdataframe <- cbind(dataframe, mUr.zero) # binds this into the data frame

Happy days - this seems to work :-)


No comments:

Post a Comment