我在R中尝试使用函数、类和方法。为了有一个"手动"练习,也可以有用,我决定创建一个管理家庭预算的"包"。简而言之,我想要一系列的函数、类和方法来计算各种东西,绘制不同类型的图表等。我想做的第一件事是创建一个"Budget"类:它应该接收具有特定列的csv,并返回一个继承数据框相同方法的对象"Budget",但我可以对其应用一组"Budgets"方法。下面是我的想法。
现在,这将返回一个带有所有必要修改的数据框,并且作为函数总体上运行良好,但如果我采取以下csv文件,结果将不会如预期。
输出只是"data.frame"。但如果我从终端再次运行函数倒数第二行的代码,则可以得到正确结果。
我得到了"data.frame"和"Budget"作为输出。 我做错了什么?
prepareData = function (csv, type=1) {
if (type == 1) {
Data = read.csv(csv,dec = ".")}
else if (type == 2) {
Data = read.csv2(csv,dec = ",")}
else {stop ("Accetable value for type are 1 and 2")}
NamesToHave = c("Date","Title","Amount","Category")
if (sum(as.numeric(colnames(Data) %in% NamesToHave)) < 4) {
stop ("The csv file has not the mandatory columns (Data, Title, Amount, Category)")}
if (class(try(tolower(Data$Title),silent = T)) == "try-error" | class(try(tolower(Data$Category),silent = T)) == "try-error") {
stop("Are you sure there are no special character in your csv file ?")}
Data$Day = sapply(strsplit(as.character(Data$Date), "/"),"[[",1)
Data$Month = month.abb[as.numeric(sapply(strsplit(as.character(Data$Date), "/"),"[[",2))]
Data$Year = sapply(strsplit(as.character(Data$Date), "/"),"[[",3)
Data = Data[with(Data, order(Year, Month, Day)), ]
Data$Amount = as.character(Data$Amount)
Data$Amount = as.numeric(as.character(Data$Amount))
class(Data) <- append(class(Data),"Budget")
return(Data)
}
现在,这将返回一个带有所有必要修改的数据框,并且作为函数总体上运行良好,但如果我采取以下csv文件,结果将不会如预期。
structure(list(Date = structure(c(22L, 1L, 1L, 1L, 1L, 1L), .Label = c("01/10/2016",
"01/11/2016", "02/10/2016", "04/10/2016", "04/11/2016", "05/10/2016",
"05/11/2016", "06/10/2016", "06/11/2016", "07/10/2016", "08/10/2016",
"08/11/2016", "09/10/2016", "09/11/2016", "10/10/2016", "10/11/2016",
"11/10/2016", "12/11/2016", "14/10/2016", "16/10/2016", "18/10/2016",
"20/09/2016", "20/10/2016", "21/10/2016", "22/09/2016", "22/10/2016",
"23/09/2016", "23/10/2016", "25/09/2016", "25/10/2016", "26/09/2016",
"26/10/2016", "27/10/2016", "28/10/2016", "29/10/2016", "30/10/2016"
), class = "factor"), Title = structure(c(20L, 6L, 36L, 29L,
30L, 11L), .Label = c("Bagpiper", "beer debaser", "Br", "brewdog",
"Burger King", "Clas", "coop", "Coop", "Eriksdalbadet", "etc",
"ETC", "Flippin", "Fotografiska", "Gateau Agneta", "Grekisk fastfood",
"Grill", "Gunnarson", "Gunnarsson", "hemkop", "HK", "Hotorhallen",
"ICA", "ICA Skinnskat", "Igor Sport", "Intersport", "Kak", "klattercentret",
"LullesFagel", "Mae Thai", "MamaWolf", "Material", "Matrerial",
"Oriental Supermarket", "Paradiset", "Pendeltag Uppsala", "PGW",
"Pressbyran", "Primeburger", "Primo Ciao ciao", "R Asia", "Systembolaget",
"taxi Skinnskat", "The Cure drinks", "Udden pensionat", "Ugglan",
"Wentzels hobby"), class = "factor"), Amount = c(167.27, 331,
971, 99, 192, 3289), Category = structure(c(10L, 3L, 3L, 6L,
6L, 3L), .Label = c("Drink", "extra", "Extra", "Extra_Fede",
"extra_food", "Extra_food", "extra_laure", "Extra_Laure", "food",
"Food"), class = "factor")), .Names = c("Date", "Title", "Amount",
"Category"), row.names = c(NA, 6L), class = "data.frame")
我运行
Data = prepareData("name.csv")
class(Data)
输出只是"data.frame"。但如果我从终端再次运行函数倒数第二行的代码,则可以得到正确结果。
class(Data) <- append(class(Data),"Budget")
class(Data)
我得到了"data.frame"和"Budget"作为输出。 我做错了什么?