mercredi 31 janvier 2018

How to replace a number with missing values all over the dataset?

I have written a user defined function that should get a dataset and some number of symbol, scan the entire dataset and replace all instances of this number with missing values. The code worked just fine until I put it in a user defined function (the loop and condition worked). I can't figure out why it's not working now. There are no warnings or errors, it just doesn't work. In addition, I wanted to ask if there is a way to choose which columns to do this operation on ?

My code is:

repMissing = function(ds,x)
{
  for(i in 1:nrow(ds))
  {
    for(j in 1:ncol(ds))
    {
      if (!is.na(ds[i,j]) && ds[i,j] == x)
      {
        ds[i,j] = NA
      }
    }
  }
}

and I ran the following code:

repMissing(diet2,-99)

For some reason, the -99's are still not NA's.

Any advice will be most appreciated. Thank you !

Aucun commentaire:

Enregistrer un commentaire