4

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"))

1 Answer 1

5
sample[is.na(sample)] <- ""
sample[sample == 88888] <- 0
Sign up to request clarification or add additional context in comments.

2 Comments

Note, however, that by converting the NA to "", you also convert your column from integer to character. NA is the best way to represent "nothing" for a column of integer.
This also doesn't modify the data.table in place.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.