R c() "combine-function", set class not by first input
R c() "combine-function", set class not by first input
我发现自己编写了一些丑陋的代码来设置 class 组合对象。有 simpler/faster 的方法吗?下面是我的问题示例:
#some S3 class vector
y = structure(c(1577833200, 1577836800, 1577840400, 1577844000, 1577847600
), class = c("POSIXct", "POSIXt"), tzone = "")
#correct class
class(y)
#I want to front pad the vector with missing values, maybe sometimes append something else
#[1], correct class but only because y is first
x_right_class = c(y,NA)
class(x_right_class)
#[2] drops "POSIXct", "POSIXt" class because NA does not have that class
x_wrong_class = c(NA,y)
class(x_wrong_class)
#[3] this one works but is tedious, and looks ugly, maybe also slow
x_also_right_class = c(y[NA_integer_],y)
#is there a simpler/faster way, I can have the c()-function to behave like [1] (and not 2) without doing [3]
#simpler for better looking code, faster to avoid unnecessary reallocation of large vectors. I guess one time is inevitable.
更新:通过 answers/comments 似乎可以将 c() 调整为一个新函数 rc() "reverse combine",它会在第 n 个对象上分派。我猜这个功能可能需要一些额外的安全检查,但看起来很不错。
rc = function(...,dispath_by=NULL) {
l = list(...)
if(is.null(dispath_by)) dispath_by= length(l)
class(l[[1]]) = class(l[[dispath_by]])
do.call(c,l)
}
x_also_right_class = rc(NA, y)
x_also_right_class = rc(NA, y,NA,dispath_by=2)
显式调用该方法似乎有效:
c.POSIXct(NA, y)
虽然我不明白为什么在使用 c() 时不调用此方法。
将 NA
转换为 POSIXct
x_wrong_class = c(as.POSIXct(NA),y)
class(x_wrong_class)
#[1] "POSIXct" "POSIXt"
如果 class 如果 y
未知,我们可以这样做:
add_value <- NA
class(add_value) <- class(y)
c(add_value, y)
我发现自己编写了一些丑陋的代码来设置 class 组合对象。有 simpler/faster 的方法吗?下面是我的问题示例:
#some S3 class vector
y = structure(c(1577833200, 1577836800, 1577840400, 1577844000, 1577847600
), class = c("POSIXct", "POSIXt"), tzone = "")
#correct class
class(y)
#I want to front pad the vector with missing values, maybe sometimes append something else
#[1], correct class but only because y is first
x_right_class = c(y,NA)
class(x_right_class)
#[2] drops "POSIXct", "POSIXt" class because NA does not have that class
x_wrong_class = c(NA,y)
class(x_wrong_class)
#[3] this one works but is tedious, and looks ugly, maybe also slow
x_also_right_class = c(y[NA_integer_],y)
#is there a simpler/faster way, I can have the c()-function to behave like [1] (and not 2) without doing [3]
#simpler for better looking code, faster to avoid unnecessary reallocation of large vectors. I guess one time is inevitable.
更新:通过 answers/comments 似乎可以将 c() 调整为一个新函数 rc() "reverse combine",它会在第 n 个对象上分派。我猜这个功能可能需要一些额外的安全检查,但看起来很不错。
rc = function(...,dispath_by=NULL) {
l = list(...)
if(is.null(dispath_by)) dispath_by= length(l)
class(l[[1]]) = class(l[[dispath_by]])
do.call(c,l)
}
x_also_right_class = rc(NA, y)
x_also_right_class = rc(NA, y,NA,dispath_by=2)
显式调用该方法似乎有效:
c.POSIXct(NA, y)
虽然我不明白为什么在使用 c() 时不调用此方法。
将 NA
转换为 POSIXct
x_wrong_class = c(as.POSIXct(NA),y)
class(x_wrong_class)
#[1] "POSIXct" "POSIXt"
如果 class 如果 y
未知,我们可以这样做:
add_value <- NA
class(add_value) <- class(y)
c(add_value, y)