gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 06:13:11 28 4
gpt4 key购买 nike

因此,我试图根据数据帧的匹配条件来查看数据帧的两个变量(v1v2)是否在其符号(正数或负数)中匹配变量(ID1==ID2)。

示例数据框 - Trial.df:

    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

我尝试在 R 中使用以下函数来实现这一点——但没有成功。请问有什么建议吗?

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"
}
}

所以输出将是:

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

最佳答案

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

关于r - 查看两个变量的符号是否不同 - 满足 R 中的条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27360231/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com