如何根据R中的条件替换行中的某些值

时间:2016-06-26 16:41:16

标签: r

我是R的新手,因此会提出一个不那么难的问题。我有一个大约200列和1000行的df。看起来有点像这样

enter image description here

我需要计算每一行中2s的数量是否大于0,反之亦然。如果2超过将同一行中的0转换为-1或者如果0大于将2转换为-1。

我试过了:

ifelse((rowSums(b=="0") > rowSums(b=="2")) , apply(b, 1 , function(b) b[b== "2" , ] <- "-1"), apply(b, 1 , function(b) b[b== "0" , ] <- "-1"))

但它给出了错误:

  

b [b ==&#34; 2&#34;,]&lt; - &#34; -1&#34; :不正确的下标数量   基质

欢迎任何帮助和建议!

2 个答案:

答案 0 :(得分:2)

我们可以使用apply

t(apply(b, 1, FUN = function(x) {
            if(sum(x==2) > sum(x==0)) replace(x, x==0, -1)
            else if (sum(x==0) > sum(x==2)) replace( x, x==2, -1) 
            else x}))
#  ind1 ind2 ind3 ind4 ind5 ind6 ind7 ind8 ind9 ind10 ind11 ind12 ind13 ind14 ind15 ind16 ind17 ind18 ind19 ind20
#M8    -1    2    2    2   -1    2    2    1    1    -1     1     1     1     1     1     1     1     1     1     2
#M9     2    2    2    2    2    2    2   -1   -1     2     1     1     1     1     1     1     1     1    -1     1
#M17    1    1   -1    1    1    1    1    1    1     1     2     2     2     2     2    -1    -1    -1    -1     2
#M19    0   -1    0    0    0    0   -1    0    0     0     1    -1     1    -1    -1     1     1     1     1     1

或者我们可以根据rowSums

执行此操作
 i1 <- rowSums(b == 0) > rowSums(b == 2)
 b[b==0 & !i1] <- -1
 b[b==2 & i1] <- -1
 b
 #   ind1 ind2 ind3 ind4 ind5 ind6 ind7 ind8 ind9 ind10 ind11 ind12 ind13 ind14 ind15 ind16 ind17 ind18 ind19 ind20
 #M8    -1    2    2    2   -1    2    2    1    1    -1     1     1     1     1     1     1     1     1     1     2
 #M9     2    2    2    2    2    2    2   -1   -1     2     1     1     1     1     1     1     1     1    -1     1
 #M17    1    1   -1    1    1    1    1    1    1     1     2     2     2     2     2    -1    -1    -1    -1     2
 #M19    0   -1    0    0    0    0   -1    0    0     0     1    -1     1    -1    -1     1     1     1     1     1

数据

 b <- structure(list(ind1 = c(0, 2, 1, 0), ind2 = c(2, 2, 1, 2), 
 ind3 = c(2, 
 2, -1, 0), ind4 = c(2, 2, 1, 0), ind5 = c(0, 2, 1, 0), ind6 = c(2, 
 2, 1, 0), ind7 = c(2, 2, 1, -1), ind8 = c(1, 0, 1, 0), ind9 = c(1, 
 0, 1, 0), ind10 = c(0, 2, 1, 0), ind11 = c(1, 1, 2, 1), ind12 = c(1, 
 1, 2, -1), ind13 = c(1, 1, 2, 1), ind14 = c(1, 1, 2, -1), ind15 = c(1, 
 1, 2, -1), ind16 = c(1, 1, 0, 1), ind17 = c(1, 1, -1, 1), ind18 = c(1, 
 1, -1, 1), ind19 = c(1, 0, 0, 1), ind20 = c(2, 1, 2, 1)), 
 .Names = c("ind1", 
 "ind2", "ind3", "ind4", "ind5", "ind6", "ind7", "ind8", "ind9", 
 "ind10", "ind11", "ind12", "ind13", "ind14", "ind15", "ind16", 
 "ind17", "ind18", "ind19", "ind20"), row.names = c("M8", "M9", 
 "M17", "M19"), class = "data.frame")

答案 1 :(得分:0)

apply(data, 1, function(x) {
  if (sum(x == 2, na.rm = TRUE) > sum(x == 0, na.rm = TRUE)) {
    x[x == 0] <- -1
  } else if {sum(x == 0, na.rm = TRUE) > sum(x == 0, na.rm = TRUE)) {
    x[x == 2] <- -1
  } 
  x
})