【发布时间】:2019-09-02 15:54:37
【问题描述】:
我正在尝试替换特定的 NA 值,即当单词包含“缺失”时。我想用 NA 值替换它。
下面的代码没有按我的意愿做。我也想对数据中的所有列执行此操作。
data %>%
mutate(tmax_na = na_if(tmax, str_detect(tmax, tolower("missing"))))
数据:
structure(list(yyyy = c(1908L, 1908L, 1908L, 1908L, 1908L, 1908L,
1908L, 1908L, 1908L, 1908L, 1908L, 1908L, 1909L, 1909L, 1909L,
1909L, 1909L, 1909L, 1909L, 1909L), month = structure(c(5L, 4L,
8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L, 9L,
7L, 6L, 2L), .Label = c("April", "August", "December", "February",
"January", "July", "June", "March", "May", "November", "October",
"September"), class = "factor"), tmax = structure(c(10L, 15L,
14L, 17L, 18L, 8L, 19L, 7L, 5L, 2L, 16L, 13L, 10L, 11L, 12L,
1L, 3L, 4L, 6L, 9L), .Label = c("12.2", "14.6", "14.7", "15.0",
"16.3", "17.3", "17.5", "17.7", "18.8", "5.0", "5.5", "5.6",
"5.8", "6.2", "7.3", "9.6", "Missing_1", "Missing_2", "Missing_3"
), class = "factor"), tmin = structure(c(2L, 5L, 4L, 9L, 14L,
17L, 8L, 18L, 16L, 15L, 11L, 19L, 3L, 1L, 1L, 10L, 12L, 13L,
7L, 6L), .Label = c("-0.3", "-1.4", "0.1", "0.3", "1.9", "10.7",
"10.8", "11.0", "2.1", "3.3", "3.4", "4.8", "7.5", "7.7", "8.0",
"8.4", "8.7", "9.7", "Missing_4"), class = "factor")), class = "data.frame", row.names = c(NA,
-20L))
【问题讨论】:
标签: r