gpt4 book ai didi

R:xtable 和日期

转载 作者:行者123 更新时间:2023-12-02 03:28:45 24 4
gpt4 key购买 nike

我有以下数据:

transaction <- c(1,2,3);
date <- c("2010-01-31","2010-02-28","2010-03-31");
type <- c("debit", "debit", "credit");
amount <- c(-500, -1000.97, 12500.81);
oldbalance <- c(5000, 4500, 17000.81)
evolution <- data.frame(transaction, date, type, amount, oldbalance, row.names=transaction, stringsAsFactors=FALSE);
evolution <- transform(evolution, newbalance = oldbalance + amount);
evolution

正在运行

> library(xtable)
> xtable(evolution)

工作正常。但如果我添加这一行

evolution$date <- as.Date(evolution$date, "%Y-%m-%d");

给予

transaction <- c(1,2,3);
date <- c("2010-01-31","2010-02-28","2010-03-31");
type <- c("debit", "debit", "credit");
amount <- c(-500, -1000.97, 12500.81);
oldbalance <- c(5000, 4500, 17000.81)
evolution <- data.frame(transaction, date, type, amount, oldbalance, row.names=transaction, stringsAsFactors=FALSE);
evolution$date <- as.Date(evolution$date, "%Y-%m-%d");
evolution <- transform(evolution, newbalance = oldbalance + amount);
evolution

然后运行xtable给出

xtable(evolution) Error in Math.Date(x + ifelse(x == 0, 1, 0)) : abs not defined for Date objects

但是在这种情况下使用 xtable 来对日期进行一些过滤可能会很有用

evolution$date <- as.Date(evolution$date, "%Y-%m-%d")
startdate <-as.Date("2010-02-01");
enddate <-as.Date("2010-03-30");
newdate <-evolution[which (evolution$date >= startdate & evolution$date <= enddate),]
newdate


> newdate
transaction date type amount oldbalance newbalance
2 2 2010-02-28 debit -1000.97 4500 3499.03
> xtable(newdate)
Error in Math.Date(x + ifelse(x == 0, 1, 0)) :
abs not defined for Date objects

最佳答案

这可以说是 xtable 中的一个错误 - 您可能需要将其报告给维护者。

临时的解决方法是在 xtable 错误解释的类上调用 as.character() (除了“Date”,我可以想到“POSIXt”,但是可能还有其他),例如:

xtable <- function(x, ...) {
for (i in which(sapply(x, function(y) !all(is.na(match(c("POSIXt","Date"),class(y))))))) x[[i]] <- as.character(x[[i]])
xtable::xtable(x, ...)
}

关于R:xtable 和日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8652674/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com