R 修改 class DIFFTIME 为 hist
R Modify the class DIFFTIME to hist
在我的脚本中,我已将此 data.frame 转换为如下所示:
df.modified<-transform(df, new.col= Payment.date-Selectionfinal$Invoice.Date)
这是结果:
Client.Code. Invoice.Date Payment.date new.col
1: 1004850 2016-01-31 2016-11-22 296 days
2: 1004850 2016-06-30 2016-11-22 145 days
3: 1004850 2016-06-30 2016-11-22 145 days
4: 1004850 2016-06-30 2016-11-22 145 days
5: 1004850 2016-09-30 2016-11-22 53 days
然后,我需要将它绘制成直方图,但我遇到了这个问题:
> hist(df.modified[,c(4)])
Error in hist.default(Completo[, c(4)]) : 'x' must be numeric
这是我 df 的 str:
$ Client.Code.: int 1004850 1004850 1004850 1004850 1004850 1004850 1004850 1004874 1004874 1005133 ...
$ Invoice.Date: Date, format: "2016-01-31" "2016-06-30" "2016-06-30" "2016-06-30" ...
$ Payment.date: Date, format: "2016-11-22" "2016-11-22" "2016-11-22" "2016-11-22" ...
$ new.col :Class 'difftime' atomic [1:4430] 296 145 145 145 53 53 53 102 72 71 ...
拜托,我想知道这个技巧。谢谢。
正如 Kristofersen 已经指出的那样,问题在于您作为数据传递给 hist
函数的类型。数据预计为numeric
,不接受difftime
.
# some data mimicking your data
Client.Code. <- c(1004850, 1004849, 1004850, 1004850, 1004851)
Invoice.Date <- as.Date(c("2016-01-31", "2016-03-30", "2016-06-30", "2016-06-30", "2016-04-30"))
Payment.date <- as.Date(c("2016-11-22", "2016-10-22", "2016-09-22", "2016-08-22", "2016-10-09"))
# creating the new column in a similar way to your way
df <- data.frame(Client.Code., Invoice.Date, Payment.date)
df$new.col <- df$Payment.date - df$Invoice.Date
## the following 3 do not work because they pass difftime to hist()
hist(df[,c(4)])
hist(df[,4])
hist(df$new.col)
# class of difftime
class(df$new.col)
## these 3 do work: numeric is passed to hist()
hist(as.numeric(df$new.col))
hist(as.numeric(df[,4]))
hist(as.numeric(df[,c(4)]))
在我的脚本中,我已将此 data.frame 转换为如下所示:
df.modified<-transform(df, new.col= Payment.date-Selectionfinal$Invoice.Date)
这是结果:
Client.Code. Invoice.Date Payment.date new.col
1: 1004850 2016-01-31 2016-11-22 296 days
2: 1004850 2016-06-30 2016-11-22 145 days
3: 1004850 2016-06-30 2016-11-22 145 days
4: 1004850 2016-06-30 2016-11-22 145 days
5: 1004850 2016-09-30 2016-11-22 53 days
然后,我需要将它绘制成直方图,但我遇到了这个问题:
> hist(df.modified[,c(4)])
Error in hist.default(Completo[, c(4)]) : 'x' must be numeric
这是我 df 的 str:
$ Client.Code.: int 1004850 1004850 1004850 1004850 1004850 1004850 1004850 1004874 1004874 1005133 ...
$ Invoice.Date: Date, format: "2016-01-31" "2016-06-30" "2016-06-30" "2016-06-30" ...
$ Payment.date: Date, format: "2016-11-22" "2016-11-22" "2016-11-22" "2016-11-22" ...
$ new.col :Class 'difftime' atomic [1:4430] 296 145 145 145 53 53 53 102 72 71 ...
拜托,我想知道这个技巧。谢谢。
正如 Kristofersen 已经指出的那样,问题在于您作为数据传递给 hist
函数的类型。数据预计为numeric
,不接受difftime
.
# some data mimicking your data
Client.Code. <- c(1004850, 1004849, 1004850, 1004850, 1004851)
Invoice.Date <- as.Date(c("2016-01-31", "2016-03-30", "2016-06-30", "2016-06-30", "2016-04-30"))
Payment.date <- as.Date(c("2016-11-22", "2016-10-22", "2016-09-22", "2016-08-22", "2016-10-09"))
# creating the new column in a similar way to your way
df <- data.frame(Client.Code., Invoice.Date, Payment.date)
df$new.col <- df$Payment.date - df$Invoice.Date
## the following 3 do not work because they pass difftime to hist()
hist(df[,c(4)])
hist(df[,4])
hist(df$new.col)
# class of difftime
class(df$new.col)
## these 3 do work: numeric is passed to hist()
hist(as.numeric(df$new.col))
hist(as.numeric(df[,4]))
hist(as.numeric(df[,c(4)]))