如何根据另一个变量的值有条件地更改变量的值?

时间:2019-10-22 13:14:02

标签: r if-statement conditional-statements

我有两个变量; MARKSV1201和MARKSVA1201。 MARKSVA1201仅在缺少MARKSV1201时才有意义,否则只会使我的分析混乱。

我尝试编写一个脚本,每当记录MARKSV1201的值时,便将MARKSVA1201设置为“ 0”

if(!is.na(test$`MARKSV1201     `)){test$`MARKSVA1201    `=0}

但这似乎不起作用,程序抱怨说“条件是> 1,并且只会使用第一个元素”

由于我正在处理向量,因此我尝试使用ifelse语句:

ifelse(!is.na(test$`MARKSV1201     `),test$`MARKSVA1201    `,test$`MARKSVA1201    `==test$'MARKSVA1201    ')

这似乎可行,但是我只得到一个逻辑向量。

我该如何有条件地更改我的实际值?

数据快照:

    structure(list(`MARKSV1201     ` = structure(c(NA, NA, 8L, 8L, 
NA, 8L, NA, 6L, 8L, 6L, 6L, 6L, 8L, 6L, 8L, 6L, 6L, 8L, 6L, 6L, 
NA, 8L, 8L, 7L, 7L, 8L, NA, 8L, 6L, 8L, NA, 6L, 8L, 6L, 8L, 8L, 
NA, NA, NA, NA, NA, NA, NA, 6L, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA), .Label = c("A  ", "B  ", "C  ", 
"D  ", "E  ", "G  ", "MVG", "VG "), class = "factor"), `MARKSVA1201    ` = structure(c(NA, 
NA, NA, NA, NA, NA, 6L, NA, NA, NA, NA, NA, NA, 6L, NA, NA, NA, 
NA, NA, NA, 6L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), .Label = c("A  ", 
"B  ", "C  ", "D  ", "E  ", "G  ", "MVG", "VG "), class = "factor")), row.names = c(1L, 
5L, 9L, 12L, 15L, 18L, 21L, 24L, 27L, 30L, 34L, 37L, 43L, 46L, 
50L, 53L, 59L, 62L, 65L, 68L, 71L, 74L, 80L, 83L, 86L, 89L, 92L, 
98L, 101L, 104L, 107L, 110L, 113L, 116L, 119L, 122L, 125L, 128L, 
134L, 137L, 140L, 146L, 149L, 155L, 161L, 167L, 170L, 173L, 176L, 
182L, 185L, 188L, 191L, 195L, 198L, 201L, 204L, 207L, 213L, 216L, 
219L, 225L, 228L, 231L, 237L, 243L, 249L, 252L, 255L, 258L, 261L, 
264L, 267L, 276L, 282L, 285L, 288L, 291L, 294L, 297L, 300L, 303L, 
306L, 309L, 312L, 315L, 321L, 324L, 327L, 330L, 333L, 336L, 339L, 
342L, 345L, 348L, 354L, 357L, 360L, 363L, 366L, 372L, 375L, 381L, 
384L, 387L, 390L, 393L, 396L, 399L, 402L, 405L, 408L, 411L, 414L, 
417L, 420L, 423L, 426L, 429L, 435L, 438L, 441L, 444L, 447L, 450L, 
453L, 456L, 459L, 462L, 465L, 468L, 471L, 474L, 477L, 480L, 483L, 
486L, 489L, 492L), reshapeWide = list(v.names = "QUAL_RATING", 
    timevar = "SEL_CRITERION", idvar = "PNR", times = structure(3:1, .Label = c("BI   ", 
    "BII  ", "HP   "), class = "factor"), varying = structure(c("QUAL_RATING.HP   ", 
    "QUAL_RATING.BII  ", "QUAL_RATING.BI   "), .Dim = c(1L, 3L
    ))), class = "data.frame")

2 个答案:

答案 0 :(得分:3)

现在应该这样做。

#rename the columns for convenience 
names(df) <- c("MARKSV1201", "MARKSVA1201")

# coerce the df to char 
df[] <- lapply(df, as.character)

# Use the ifelse
df$MARKSVA1201 <- ifelse(!is.na(df$MARKSV1201), 0, df$MARKSVA1201)

# coerce it back to its original factor 
df[] <- lapply(df, as.factor)

#output
#   Marksv1201 MARKSVA1201
# 1        <NA>          NA
# 5        <NA>          NA
# 9         VG            0
# 12        VG            0
# 15       <NA>          NA
# 18        VG            0

输入

# df
#   MARKSV1201      MARKSVA1201    
# 1             <NA>            <NA>
# 5             <NA>            <NA>
# 9              VG             <NA>
# 12             VG             <NA>
# 15            <NA>            <NA>
# 18             VG             <NA>

您可以使用str(df)检查结构,以检查变量的类并根据需要来回强制。

答案 1 :(得分:0)

存在问题,因为您的列是factor类型。您可以尝试

zz$`MARKSVA1201    ` = as.character(zz$`MARKSVA1201    `)
zz$`MARKSVA1201    `[is.na(zz$`MARKSV1201     `)] = 0
df$MARKSVA1201=as.factor(df$MARKSVA1201)