Jochem
Jochem

Reputation: 3396

Replacing value in multiple columns in data.table

I have a large table with time series data (not in time series format) and I would like to replace certain values with a simple line of code. Below you will find a sample of the table. I would like to replace NA with nothing and 88888 with 0. Of course this can be done column by column with the help of a while-loop. However, I guess there is a simpler solution to this issue. Any suggestions?

Sample dataset:

sample <- structure(list(Period = c("01-12-08", "01-01-09", "01-02-09", "01-03-09",
 "01-04-09", "01-05-09", "01-06-09", "01-07-09", "01-08-09", "01-09-09", "01-10-09",
 "01-11-09", "01-12-09", "01-01-10", "01-02-10"), Serie1 = c(NA, NA, NA, NA, NA, NA,
 NA, NA, NA, NA, NA, 6L, 88888L, 88888L, 88888L), Serie2 = c(NA, NA, NA, NA, NA, NA,
 20L, 88888L, 88888L, 88888L, 88888L, 88888L, 88888L, NA, NA), Serie3 = c(NA, NA,
 NA, NA, NA, NA, NA, NA, NA, 10L, 10L, 88888L, 88888L, 88888L, 88888L)), .Names =
 c("Period", "Serie1", "Serie2", "Serie3"), row.names = c(NA, -15L), class =
 c("data.table", "data.frame"))

Upvotes: 4

Views: 1215

Answers (1)

lukeA
lukeA

Reputation: 54287

sample[is.na(sample)] <- ""
sample[sample == 88888] <- 0

Upvotes: 5

Related Questions