I have found for c and rbind the class of the result is based on the class of the first argument. This has caused a problem for me because the presence of NA as the first argument coerces Date vectors to numeric vectors. Compare the result of these two class calls:

x <- Sys.Date()
y <- NA
class(c(x, y)) # "Date"
class(c(y, x)) # "numeric"

and likewise for rbind:

x <- data.frame(column=Sys.Date())
y <- data.frame(column=NA)
class(rbind(x, y)$column) # "Date"
class(rbind(y, x)$column) # "numeric"

How can I ensure the result of these concatenations is always a Date vector, regardless of the order of the arguments?

有帮助吗?

解决方案

Coerce the first argument to your desired class:

c(as.Date(y), x)
c(as.Date(x), y)

其他提示

Use the method you want explicitly:

c.Date(y,x)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top