替换data.table中多列中的值

时间:2014-02-20 11:09:34

标签: r data.table

我有一个包含时间序列数据的大表(不是时间序列格式),我想用一行简单的代码替换某些值。您将在下面找到该表的示例。我想用0代替NA,用8代替88888。当然,这可以在while循环的帮助下逐列完成。但是,我想这个问题有一个更简单的解决方案。有什么建议吗?

示例数据集:

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 个答案:

答案 0 :(得分:5)

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