条件表达式if和or

条件表达式if和or,r,R,我有下面的表达 motherW 其结果可能只有以下列表中的一个元素 s2,s4,s6,s8,s10,s12,s14,s16,haar,d2,d4,d6,d8,d10,d12,d14,d16,d18,c6,c12,c18,c24, c30,l4,l6,l14,l18 我希望有一个条件表达式,如下所示(这会产生一个错误。我相信是使用了or/ifesle) B以下是使用if和else的正确方法: B <- if (motherW %in% c("s2", "s4", "s6", "s8",

我有下面的表达

motherW 
其结果可能只有以下列表中的一个元素

s2,s4,s6,s8,s10,s12,s14,s16,haar,d2,d4,d6,d8,d10,d12,d14,d16,d18,c6,c12,c18,c24,
c30,l4,l6,l14,l18
我希望有一个条件表达式,如下所示(这会产生一个错误。我相信是使用了or/ifesle)


B以下是使用
if
else
的正确方法:

B <- if (motherW %in% c("s2", "s4", "s6", "s8", "s10", "s12", "s14", "s16")) {
  8
} else {
  if (motherW %in% c("haar", "d2", "d4", "d6", "d8", "d10", "d12", "d14", "d16", "d18")) {
    9
  } else {
    if (motherW %in% c("c6", "c12", "c18", "c24", "c30", "l2", "l4", "l6", "l14", "l18")) {
      5
    } else {
      0
    }
  }  
}

B以下是使用
if
else
的正确方法:

B <- if (motherW %in% c("s2", "s4", "s6", "s8", "s10", "s12", "s14", "s16")) {
  8
} else {
  if (motherW %in% c("haar", "d2", "d4", "d6", "d8", "d10", "d12", "d14", "d16", "d18")) {
    9
  } else {
    if (motherW %in% c("c6", "c12", "c18", "c24", "c30", "l2", "l4", "l6", "l14", "l18")) {
      5
    } else {
      0
    }
  }  
}
B您可以尝试以下方法:

B <- 8*(motherW %in% c("s2","s4","s6","s8","s10","s12","s14","s16"))+
     9*(motherW %in% c("haar","d2","d4","d6","d8","d10","d12","d14","d16","d18"))+
     5*(motherW %in% c("c6","c12","c18","c24","c30"))+
     5*(motherW %in% c("l2","l4","l6","l14","l18"))
B您可以尝试以下方法:

B <- 8*(motherW %in% c("s2","s4","s6","s8","s10","s12","s14","s16"))+
     9*(motherW %in% c("haar","d2","d4","d6","d8","d10","d12","d14","d16","d18"))+
     5*(motherW %in% c("c6","c12","c18","c24","c30"))+
     5*(motherW %in% c("l2","l4","l6","l14","l18"))

B您的语法不正确。首先,你需要写一些类似的条件

motherW=="s2" | motherW=="s4" | ...
您可以在%

使用嵌套的
ifelse
条件的一种可能的解决方案是

B <- ifelse(motherW %in% c("s2","s4","s6","s8","s10","s12","s14","s16"),
    8,
    ifelse(motherW %in% c("haar","d2","d4","d6","d8","d10","d12","d14","d16","d18"),
        9,
        ifelse(motherW %in% c("c6","c12","c18","c24","c30"),
            5,
            ifelse(motherW %in% c("l2","l4","l6","l14","l18"), 5, 0))))

B您的语法不正确。首先,你需要写一些类似的条件

motherW=="s2" | motherW=="s4" | ...
您可以在%

使用嵌套的
ifelse
条件的一种可能的解决方案是

B <- ifelse(motherW %in% c("s2","s4","s6","s8","s10","s12","s14","s16"),
    8,
    ifelse(motherW %in% c("haar","d2","d4","d6","d8","d10","d12","d14","d16","d18"),
        9,
        ifelse(motherW %in% c("c6","c12","c18","c24","c30"),
            5,
            ifelse(motherW %in% c("l2","l4","l6","l14","l18"), 5, 0))))

B+1如果“d”列表中不包含“haar”,则较短的解决方案有效。您可以通过在
setNames()
中添加值为9的“h”来修复它:
tab我认为
tab+1如果“d”列表中不包含“haar”,则较短的解决方案会有效。您可以通过在
setNames()
中添加值为9的“h”来修复它:
tab我想
tab