문제

I have x:

x = structure(c(12, 24, NA, 25), .Dim = c(2L, 2L))

> x
     [,1] [,2]
[1,]   12   NA
[2,]   24   25

and would like to return y, where y equals the value in the second column of x if the value is available, and the value in the first column if not.

so:

> y
[1] 12 25

I want to use the solution for a large array, so I am looking for a vectorized solution if that makes sense.

도움이 되었습니까?

해결책

ifelse does exactly what you want:

> ifelse(is.na(x[,2]), x[,1], x[,2])
[1] 12 25

If speed is paramount (and you don't want to mess with C), you can try:

y <- x[,2]
y[is.na(y)] <- x[is.na(y), 1]

This effectively shortcircuits some of the overhead of ifelse. Consider:

set.seed(1)
x <- cbind(sample(1:1e5), sample(c(1:95000, rep(NA, 5000))))
library(microbenchmark)
microbenchmark(
  z <- ifelse(is.na(x[,2]), x[,1], x[,2]),
  {y <- x[,2]; y[is.na(y)] <- x[is.na(y), 1]},
  times=10
)
# Unit: milliseconds
#                                       expr        min  median
# z <- ifelse(is.na(x[, 2]), x[, 1], x[, 2])      30.46   33.06
# y <- x[, 2]; y[is.na(y)] <- x[is.na(y), 1]       5.48    5.77 
identical(y, z)    
# [1] TRUE
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top