[R] NA Replacement by lowest value?

Jim Lemon jim at bitwrit.com.au
Thu Jan 28 10:57:57 CET 2010


On 01/28/2010 08:35 PM, Joel Fürstenberg-Hägg wrote:
>
> Hi all,
>
>
>
> I need to replace missing values in a matrix by 10 % of the lowest available value in the matrix. I've got a function I've used earlier to replace negative values by the lowest value, in a data frame, but I'm not sure how to modify it...
>
>
>
> nonNeg = as.data.frame(apply(orig.df, 2, function(col) # Change negative values to a small value, close to zero
> {
>     min.val = min(col[col>  0])
>
>     col[col<  0] = (min.val / 10)
>     col # Column index
> }))
>
>
>
> I think this is how to start, but the NA replacement part doesn't work...
>
>
>
> newMatrix = as.matrix(apply(oldMatrix, 2, function(col)
>
> {
>
>     min.val = min(mData, na.rm = T) # Find the smallest value in the dataset
>
>     col[col == NA] = (min.val / 10) # Doesn't work...
>     col # Column index
>
> }
>
>
>
> Does any of you have any suggestions?
>
Hi Joel,

You probably want to use:

col[is.na(col)]<-min.val/10

Jim



More information about the R-help mailing list