计算R中的比率

时间:2017-12-07 13:34:53

标签: r

如果我有这个数据集:     Name <- c("Bill", "Ben", "Lisa", "Maya") Xnum <- c(5, NA, 10, NA) Ynum <- c(10, 5, NA, NA) MyData <- data.frame(Name, Xnum, Ynum)

这给了我一个数据集:

 `print(MyData)
  Name Xnum Ynum
1 Bill    5   10
2  Ben   NA    5
3 Lisa   10   NA
4 Maya   NA   NA`

将X除以Y会给我一个我想要的比率。这很容易做到     MyData$Ratio <- MyData$Xnum / MyData$Ynum

问题是其中包含NA的值。在这些情况下,我有以下要求

`If Xnum == NA AND Ynum == NA
    Ratio = 1
Else if Xnum == NA AND Ynum >= 0
    Ratio = 1
Else if Xnum >= 0 AND Ynum == NA
    Ratio = 0
Else
    Ratio = Xnum / Ynum`

我尝试将其作为传递Xnum和Ynum并返回比率的函数,但它没有按照我的预期进行。功能是:

`determine_ratios <- function( PrevTotal, AllTotal ) {

    if ( (is.na(PrevTotal)) && (is.na(AllTotal)) ) {
        return(as.double(1.0))
    } else if ((PrevTotal  >= 0) && (is.na(AllTotal)) ) {
        return(as.double(1.0))
    } else if ((is.na(PrevTotal)) && (AllTotal >= 0) ) {
        return(as.double(0.0))
    } else {
        return(as.double(PrevTotal / AllTotal ))
    }
}`

我称之为:

`MyData$Ratios <- determine_ratios(MyData$Xnum, MyData$Ynum)`

我得到的输出是:

`print(MyData)
  Name Xnum Ynum Ratios
1 Bill    5   10    0.5
2  Ben   NA    5     NA
3 Lisa   10   NA     NA
4 Maya   NA   NA     NA`

预期输出为:

`print(MyData)
  Name Xnum Ynum Ratios
1 Bill    5   10    0.5
2  Ben   NA    5     1
3 Lisa   10   NA     0
4 Maya   NA   NA     1`

由于

1 个答案:

答案 0 :(得分:1)

使用case_when包中的dplyr

library(dplyr)

MyData$Ratios <-
case_when(
    is.na(MyData$Xnum) & is.na(MyData$Ynum) ~ 1,
    is.na(MyData$Xnum) & MyData$Ynum >= 0 ~ 1,
    MyData$Xnum >= 0 & is.na(MyData$Ynum) ~ 0,
    TRUE ~ MyData$Xnum / MyData$Ynum
)