code123
code123

Reputation: 2146

Find and replace missing values in List in R

I need answers on this topic. I have several files in a folder which I have imported to R using:

temp = list.files(pattern="*.txt") 

myfiles = lapply(temp, read.delim)

The resulting files are on the workspace stored as List[110]. So they are 110 files in the list. Each files has several different columns and rows.

My question: I would like to find and replace -999.99M with NA; Find 'T','C','A','F' and 'Y', delete them from all the 110 files.

Upvotes: 2

Views: 5310

Answers (1)

Hong Ooi
Hong Ooi

Reputation: 57686

+1 for using lapply to put all your datasets in a list.

If I've understood you correctly, you can do the following for the replacement:

myfiles <- lapply(myfiles, function(df)
                  data.frame(lapply(df, function(x) {
                             if(is.numeric(x))
                                 x[x == -999.99] <- NA
                             else x[x %in% c('T','C','A','F','Y')] <- NA
                             x
                  })
           )

Upvotes: 2

Related Questions