r for循环,名称变异

时间:2016-02-23 15:41:08

标签: string loops for-loop dplyr na

我是新的R用户。我也知道之前已经提出并回答了类似的问题,但我无法弄清楚。目标是使用循环函数用一组变量中的0值替换NA。显然,这是一个超级简单的循环函数,但我不知道为什么这不应该做它应该做的事情。

两个额外的首选项,使用变量名称(而不是列号)和使用dplyr的建议是首选。

提前感谢您的时间!

文库

library(plyr)
library(dplyr)

示例数据

y <- structure(list(pid = c(1002L, 1002L, 1002L, 1002L, 1002L, 1002L,1002L, 1002L, 1002L, 1002L), year = 1968:1977, weeks_hd_e = c(3,0, 50, 49, 50, 50, 50, 50, 50, 49), weeks_wf_e = c(4, 6, 0, 0,0, 0, 0, 0, 0, 0), weeks_hd_u = c(NA, NA, 0, 0, 0, 0, 0, 0, 0,0), weeks_hd = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), weeks_wf_u = c(NA,NA, NA, NA, NA, NA, NA, NA, 0, NA), weeks_wf = c(NA_real_, NA_real_,NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,NA_real_)), .Names = c("pid", "year", "weeks_hd_e", "weeks_wf_e","weeks_hd_u", "weeks_hd", "weeks_wf_u", "weeks_wf"), row.names = c(NA,10L), class = "data.frame")

此命令有效

y <- mutate(y, i = ifelse(!is.na(i), i, 0))

此循环不

vars <- c("weeks_hd_e", "weeks_hd_u", "weeks_wf_e", "weeks_wf_u", "weeks_hd", "weeks_wf")
for (i in names(vars)) {
    y <- mutate(y, i = ifelse(!is.na(i), i, 0))
}

View(y)

2 个答案:

答案 0 :(得分:0)

我从朋友那里得到了两个很好的答案:

for (i in 1:length(vars)){
    y[vars[i]][is.na(y[vars[i]])] <- 0
}

y[, vars] <- apply(y[, vars], 2, function(x) ifelse(is.na(x), 0, x))

答案 1 :(得分:0)

tidyr包中的replace_na命令完全符合您的要求。 像这样使用它:

install.packages("tidyr")
library(tidyr)

# your data
y <- structure(list(pid = c(1002L, 1002L, 1002L, 1002L, 1002L, 1002L,1002L, 1002L, 1002L, 1002L), year = 1968:1977, weeks_hd_e = c(3,0, 50, 49, 50, 50, 50, 50, 50, 49), weeks_wf_e = c(4, 6, 0, 0,0, 0, 0, 0, 0, 0), weeks_hd_u = c(NA, NA, 0, 0, 0, 0, 0, 0, 0,0), weeks_hd = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), weeks_wf_u = c(NA,NA, NA, NA, NA, NA, NA, NA, 0, NA), weeks_wf = c(NA_real_, NA_real_,NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,NA_real_)), .Names = c("pid", "year", "weeks_hd_e", "weeks_wf_e","weeks_hd_u", "weeks_hd", "weeks_wf_u", "weeks_wf"), row.names = c(NA,10L), class = "data.frame")

# replacing NAs in your dataframe
# specify the variables you want to replace NAs in and the replacement in the `replace` = list argument
y <- replace_na(y, replace = list(weeks_hd_e = 0, weeks_hd_u = 0, weeks_wf_e = 0, weeks_wf_u = 0, weeks_hd = 0, weeks_wf = 0))

请注意,这符合您的偏好,即按名称指定变量,并且在替换方面更灵活,即您可以在同一命令中替换数字和字符变量中的NA。