查看两个变量的符号是否不同 - 满足R中的条件

时间:2014-12-08 14:31:11

标签: r

所以我试图根据变量的匹配条件({{1})查看数据框的两个变量(v1v2)是否匹配其符号(正数或负数) })。

示例数据框 - ID1==ID2

trial.df

我尝试使用以下功能在R中工作 - 但没有运气。有什么建议吗?

    ID1       v1       ID2        v2
BBN1140740  0.7356  BBN1140740  NA
BBN5688067  1.2996  BBN5688067  NA
BBN6046268  -0.1622 BBN6046268  -0.0173
BBN1321350  -0.162  BBN1321350  -0.0173
BBN1321351  -0.1636 BBN151      -0.0168
BBN927115   -0.1619 BBN115      -0.0171
BBN1923058   0.1609 BBN1923058  0.0254
BBN6081716   0.1608 BBN6081716  0.0171

所以输出结果为:

call(ID1,ID2,v1,v2)

call <- function(a,a1,b,b1){
  if(a==a1&&b&b1<0) {
    trial$sign <- "Negative"
  } else if (a==a1&&b&b1>0) {
    trial$sign <- "Positive"
  } else {
    trial$sign <- "Nomatch"
  }
}

3 个答案:

答案 0 :(得分:2)

DF$direct <- "no match"
DF[DF$ID1 == DF$ID2, "direct"] <- with(DF[DF$ID1 == DF$ID2, ], 
                                       (sign(v1) == sign(v2)) * sign(v1)) 
DF$direct <- factor(DF$direct, 
                    levels =  c("no match", "-1", "1", "0"),
                    labels = c("no match", "negative", "positive", "not equal / zero"))

#         ID1      v1        ID2      v2   direct
#1 BBN1140740  0.7356 BBN1140740      NA     <NA>
#2 BBN5688067  1.2996 BBN5688067      NA     <NA>
#3 BBN6046268 -0.1622 BBN6046268 -0.0173 negative
#4 BBN1321350 -0.1620 BBN1321350 -0.0173 negative
#5 BBN1321351 -0.1636     BBN151 -0.0168 no match
#6  BBN927115 -0.1619     BBN115 -0.0171 no match
#7 BBN1923058  0.1609 BBN1923058  0.0254 positive
#8 BBN6081716  0.1608 BBN6081716  0.0171 positive

答案 1 :(得分:2)

另一种方法:

transform(dat, direct = c("Nomatch", "Negative", "Positive")[
  (as.character(ID1) == ID2) * sign(v1 * v2) + (sign(v1) == 1) + 1])

结果:

         ID1      v1        ID2      v2   direct
1 BBN1140740  0.7356 BBN1140740      NA     <NA>
2 BBN5688067  1.2996 BBN5688067      NA     <NA>
3 BBN6046268 -0.1622 BBN6046268 -0.0173 Negative
4 BBN1321350 -0.1620 BBN1321350 -0.0173 Negative
5 BBN1321351 -0.1636     BBN151 -0.0168  Nomatch
6  BBN927115 -0.1619     BBN115 -0.0171  Nomatch
7 BBN1923058  0.1609 BBN1923058  0.0254 Positive
8 BBN6081716  0.1608 BBN6081716  0.0171 Positive

答案 2 :(得分:1)

可能有帮助

trialdf$id <- 1:nrow(trialdf)
df1 <- cbind(expand.grid(c(-1,1), c(-1,1)),
      direct= c('Negative', 'Nomatch', 'Nomatch', 'Positive'))
df2 <- merge(within(trialdf, {Var1 <- sign(v1)
                           Var2 <- sign(v2)}), 
                     df1, all.x=TRUE)
 df2$direct[with(df2, ID1!=ID2)] <- 'Nomatch'
 res <- df2[order(df2$id),-c(1:2,7)]
 row.names(res) <- NULL
 res
 #        ID1      v1        ID2      v2   direct
 #1 BBN1140740  0.7356 BBN1140740      NA     <NA>
 #2 BBN5688067  1.2996 BBN5688067      NA     <NA>
 #3 BBN6046268 -0.1622 BBN6046268 -0.0173 Negative
 #4 BBN1321350 -0.1620 BBN1321350 -0.0173 Negative
 #5 BBN1321351 -0.1636     BBN151 -0.0168  Nomatch
 #6  BBN927115 -0.1619     BBN115 -0.0171  Nomatch
 #7 BBN1923058  0.1609 BBN1923058  0.0254 Positive
 #8 BBN6081716  0.1608 BBN6081716  0.0171 Positive