用另一个变量的值替换零[重复]

问题描述 投票:1回答:3

这个问题在这里已有答案:

这篇帖子类似于这篇文章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
r dplyr data.table plyr
3个回答
3
投票

创建一个逻辑vector并使用它来替换替换列和替换列,以便在执行赋值操作时获得相等的长度

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

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


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
投票

你可以使用?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语法上最“美丽”。

© www.soinside.com 2019 - 2024. All rights reserved.