用另一个变量的值替换零

时间:2018-09-03 07:45:19

标签: r dplyr data.table plyr

此帖子与此帖子相似 Replace NA in column with value in adjacent column 但是现在,如果x6 = 0,则必须按x5的值返回。 如果我这样做

mydat$X6[0(mydat$X6)] <- mydat$X5[0(mydat$X6)]

我当然有这个错误:attempt to apply non-function

 mydat=structure(list(ItemRelation = c(158200L, 158204L), DocumentNum = c(1715L, 
                                                                         1715L), CalendarYear = c(2018L, 2018L), X1 = c(0L, 0L), X2 = c(0L, 
                                                                                                                                        0L), X3 = c(0L, 0L), X4 = c(NA, NA), X5 = c(107L, 105L), X6 = c(0, 
                                                                                                                                                                                                        0)), .Names = c("ItemRelation", "DocumentNum", "CalendarYear", 
                                                                                                                                                                                                                         "X1", "X2", "X3", "X4", "X5", "X6"), class = "data.frame", row.names = c(NA, 
                                                                                                                                                                                                                                                                                                  -2L))

如何在x5值上用x6替换零以获取受嘲讽的输出

  ItemRelation DocumentNum CalendarYear X1 X2 X3 X4  X5 X6
1       158200        1715         2018  0  0  0 NA 107 107
2       158204        1715         2018  0  0  0 NA 105 105

3 个答案:

答案 0 :(得分:3)

创建一个逻辑vector并使用它来对替换列和替换列进行子集化,以在执行分配操作时获得相等的长度

i1 <- mydat$X6 == 0
mydat$X6[i1] <- mydat$X5[i1]

0(mydat$X6)语法不清楚-可能是伪函数的表示形式

答案 1 :(得分:3)

您还可以使用replace,即

mydat$X6 <- with(mydat, replace(X6, X6 == 0, X5[X6 == 0]))

#  ItemRelation DocumentNum CalendarYear X1 X2 X3 X4  X5  X6
#1       158200        1715         2018  0  0  0 NA 107 107
#2       158204        1715         2018  0  0  0 NA 105 105

答案 2 :(得分:2)

您可以使用?ifelse

mydat$X6 <- ifelse(mydat$X6 == 0, mydat$X5, mydat$X6)

#  ItemRelation DocumentNum CalendarYear X1 X2 X3 X4  X5  X6
#1       158200        1715         2018  0  0  0 NA 107 107
#2       158204        1715         2018  0  0  0 NA 105 105

查看更大数据集的基准。 Ifelse的执行速度似乎慢于其他2。

mydat <- data.frame(X6=1:999999,X5=sample(0:1,999999,replace = T))

akrun <- function(mydat) {
    i1 <- mydat$X6 == 0
mydat$X6[i1] <- mydat$X5[i1]
}

sotos <- function(mydat) {
    mydat$X6 <- with(mydat, replace(X6, X6 == 0, X5[X6 == 0]))
}

elrico <- function(mydat) {
    mydat$X6 <- ifelse(mydat$X6 == 0, mydat$X5, mydat$X6)
}

microbenchmark::microbenchmark(elrico(mydat),akrun(mydat),sotos(mydat), times = 100)

#Unit: milliseconds
#          expr       min        lq      mean    median        uq      max neval cld
# elrico(mydat) 42.809477 47.591964 56.814627 49.750948 51.972969 148.7152   100   c
#  akrun(mydat)  5.068961  5.206103  8.277144  5.399385  9.516853 106.4254   100 a  
#  sotos(mydat)  7.966428  8.199167 16.903062 11.996958 13.774511 110.4206   100  b 

因此,如果您需要提高速度并使用更大的数据集,请使用akrun或sotos解决方案。另外,您可以从语法上讲这是IMO最“美丽”的。