[Rd] write.csv problems

Ivan Krylov |kry|ov @end|ng |rom d|@root@org
Fri Jun 28 18:19:39 CEST 2024


В Fri, 28 Jun 2024 11:02:12 -0500
Spencer Graves <spencer.graves using 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 vectors

Judging 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



More information about the R-devel mailing list