如何使用变量进行循环

时间:2019-06-20 07:46:48

标签: r

当前,我在4列上有4行用于简单的ifelse。我是R语言的新手,如何单行执行此操作? 谢谢 ! :)

我试图在

之类的函数中做到这一点
my_function <- function(var) {
datamart_apprentissage  <- mutate(datamart_apprentissage, var= ifelse(is.na(var), 0, 1))
}

my_function("VIE_OB3_1")
my_function("VIE_OB3_2")
my_function("VIE_OB3_3")
my_function("VIE_OB3_4")

但是没用

datamart_apprentissage  <- mutate(datamart_apprentissage, VIE_OB3_1 = ifelse(is.na(VIE_OB3_1), 0, 1))
datamart_apprentissage  <- mutate(datamart_apprentissage, VIE_OB3_2 = ifelse(is.na(VIE_OB3_2), 0, 1))
datamart_apprentissage  <- mutate(datamart_apprentissage, VIE_OB3_3 = ifelse(is.na(VIE_OB3_3), 0, 1))
datamart_apprentissage  <- mutate(datamart_apprentissage, VIE_OB3_4 = ifelse(is.na(VIE_OB3_4), 0, 1))

谢谢!

3 个答案:

答案 0 :(得分:1)

您可以按列应用:

# Some data
df <- data.frame(a = c(1,2,NA,4), b = c(NA,NA,7,8))

# Function for replacing NA with value
myfun <- function(col) {
  out <- ifelse(is.na(col), 0, 1)
  return(out)
}

# Applying function column-wise (option = 2)
apply(df, 2, function(col) myfun(col))

# As oneliner
apply(df, 2, function(col) ifelse(is.na(col), 0, 1))

答案 1 :(得分:0)

这是使用dplyr的解决方案,使用mutate_at中的.vars参数来自定义要转换的列:

library("dplyr", quietly = TRUE, warn.conflicts = FALSE)
datamart_apprentissage <- matrix(sample(x = c(NA, 2:3), size = 5*4, replace = TRUE), ncol = 4)
datamart_apprentissage <- as_tibble(datamart_apprentissage )
#> Warning: `as_tibble.matrix()` requires a matrix with column names or a `.name_repair` argument. Using compatibility `.name_repair`.
#> This warning is displayed once per session.
names(datamart_apprentissage) <- paste0("VIE_OB3_", 1:4)

# --- What you wanna do
datamart_apprentissage %>% 
        mutate_at(.vars = paste0("VIE_OB3_", 1:4), .funs = function(x) ifelse(is.na(x), 0, 1))
#> # A tibble: 5 x 4
#>   VIE_OB3_1 VIE_OB3_2 VIE_OB3_3 VIE_OB3_4
#>       <dbl>     <dbl>     <dbl>     <dbl>
#> 1         0         1         1         1
#> 2         1         0         1         1
#> 3         1         1         0         0
#> 4         0         1         0         1
#> 5         1         1         0         1

答案 2 :(得分:0)

您可以使用sapply来保持dataframe的结构。以下是一个示例,您可以在其中将NA替换为"VALUE"

您可以用自己的功能替换function(x)ifelse(我对mutate没有经验,所以我无能为力。

此外,您可以使用[row,column]仅选择特定的行/列:

df <- data.frame(a=c(1,2,NA,NA,5),b=c(NA,7,8,NA,10),c=c(NA,7,8,NA,10),d=c(NA,7,8,NA,10),e=c(NA,7,8,NA,10))
df
df[,c(1,2)] <- sapply(df[,c(1,2)],function(x)ifelse(is.na(x),"VALUE",x))
df