Hello, All: I'm getting strange errors with write.csv with some objects of class c('findFn', 'data.frame'). Consider the following: df1 <- data.frame(x=1) class(df1) <- c('findFn', 'data.frame') write.csv(df1, 'df1.csv') # Error in x$Package : $ operator is invalid for atomic vectors df2 <- data.frame(a=letters[1:2], b=as.POSIXct('2024-06-28')) class(df2) <- c('findFn', 'data.frame') write.csv(df2, 'df1.csv') # Error in tapply(rep(1, nrow(x)), xP, length) : # arguments must have same length "write.csv" works with some objects of class c('findFn', 'data.frame') but not others. I have 'findFn' object with 5264 rows that fails with the following error: Error in `[<-.data.frame`(`*tmp*`, needconv, value = list(Count = c("83", : replacement element 1 has 526 rows, need 5264 I have NOT yet been able to reproduce this error with a smaller example. However, starting 'write.csv' with something like the following should fix all these problems: if(is.data.frame(x)) class(x) <- 'data.frame' Comments? Thanks for all your work to help improve the quality of statistical software available to the world. Spencer Graves
? Fri, 28 Jun 2024 11:02:12 -0500 Spencer Graves <spencer.graves at prodsyse.com> ?????:> df1 <- data.frame(x=1) > class(df1) <- c('findFn', 'data.frame') > write.csv(df1, 'df1.csv') > # Error in x$Package : $ operator is invalid for atomic vectorsJudging by the traceback, only data frames that have a Package column should have a findFn class: 9: PackageSummary(xi) 8: `[.findFn`(x, needconv) 7: x[needconv] 6: lapply(x[needconv], as.character) 5: utils::write.table(df1, "df1.csv", col.names = NA, sep = ",", dec = ".", qmethod = "double") write.table sees columns that aren't of type character yet and tries to convert them one by one, subsetting the data frame as a list. The call lands in sos:::`[.findFn` if (missing(j)) { xi <- x[i, ] attr(xi, "PackageSummary") <- PackageSummary(xi) class(xi) <- c("findFn", "data.frame") return(xi) } Subsetting methods are hard. For complex structures like data.frames, `[.class` must handle all of x[rows,cols]; x[rows,]; x[,cols]; x[columns]; x[], and also respect the drop argument: https://stat.ethz.ch/pipermail/r-help/2021-December/473207.html I think that the `[.findFn` method mistakes x[needconv] for x[needconv,] when it should instead perform x[,needconv]. -- Best regards, Ivan
?s 17:02 de 28/06/2024, Spencer Graves escreveu:> Hello, All: > > > ????? I'm getting strange errors with write.csv with some objects of > class c('findFn', 'data.frame'). Consider the following: > > > df1 <- data.frame(x=1) > class(df1) <- c('findFn', 'data.frame') > write.csv(df1, 'df1.csv') > # Error in x$Package : $ operator is invalid for atomic vectors > > df2 <- data.frame(a=letters[1:2], > ????? b=as.POSIXct('2024-06-28')) > class(df2) <- c('findFn', 'data.frame') > write.csv(df2, 'df1.csv') > # Error in tapply(rep(1, nrow(x)), xP, length) : > #? arguments must have same length > > > ????? "write.csv" works with some objects of class c('findFn', > 'data.frame') but not others. I have 'findFn' object with 5264 rows that > fails with the following error: > > > Error in `[<-.data.frame`(`*tmp*`, needconv, value = list(Count = > c("83",? : > ? replacement element 1 has 526 rows, need 5264 > > > ????? I have NOT yet been able to reproduce this error with a smaller > example. However, starting 'write.csv' with something like the following > should fix all these problems: > > > if(is.data.frame(x)) class(x) <- 'data.frame' > > > ????? Comments? > ????? Thanks for all your work to help improve the quality of > statistical software available to the world. > > > ????? Spencer Graves > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-develHello, I don't know if this answers to question. I wasn't able to reproduce errors but warnings, yes I was. A way of not giving errors or warnings is to call write.csv at the end of a pipe such as the following. df1 <- findFn("mean") df1 |> as.data.frame() |> write.csv("df1.csv") This solution is equivalent to the code proposed in the OP without the need for a change in base R. Hope this helps, Rui Barradas -- Este e-mail foi analisado pelo software antiv?rus AVG para verificar a presen?a de v?rus. www.avg.com