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