R: Can't transform a table into a data frame - r

I have been using the R package "Player Rating" and I have worked the examples, however when I try to export the output (the rating tables) to a CSV I get the following message:
write.csv(sobj, file = "output.csv")
Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) :
cannot coerce class ""rating"" to a data.frame
https://cran.r-project.org/web/packages/PlayerRatings/PlayerRatings.pdf

You can only export data.frame/matrix etc using write.csv . The object robj(in your code 'sobj') that you are trying to export is a list. Inside it , it has data.frame "ratings" do as said by #user20650
write.csv(robj$ratings, file = "output.csv")

Related

"replacement has length zero" when exporting data frame to xlsx in R

I am trying to export a dataframe with library(openxlsx) and
openxlsx::write.xlsx(as.data.frame(df), file = "df.xlsx", colNames = TRUE, rowNames = FALSE, append = FALSE)
but I get the following error:
Error in x[is.na(x)] <- na.string : replacement has length zero
I was having a similar problem in a data frame with numeric columns (at least intended to be numeric), but, in my case, I was using the related openxlsx::writeData.
The data frame was generated using sapply, with functions which could deliver errors because of the data. So, I coded to fill with NA when an error were generated. I ended up with NaN and NAs in the same column.
What worked for me is conducting the following treatment before writeData:
df[is.na(df)]<-''
so, for your problem, the following may work:
df[is.na(df)]<-''
openxlsx::write.xlsx(as.data.frame(df), file = "df.xlsx", colNames = TRUE, rowNames = FALSE, append = FALSE)

Exporting a list of dataframes as csv

I have a list of dataframes which I want to export as csv. I tried :
for (i in listofdf){
write.csv(listofdf[i], file = paste(names(listofdf)[i],".csv", sep=""), row.names = FALSE)
}
and got : Error in listofdf[i] : invalid subscript type 'list'. I read that I am feeding a list data type into a process that expects a vector, and tried to apply the given solution : unlist(listofdf), but all I got is a massive list of numeric values that I don't know how to deal with.
Then I tried a solution found here, that works with the given example, but when I try to apply :
sapply(names(listofdf),
function (x) write.table(listofdf[x],
file = paste(names(listofdf)[x],".csv", sep=""),
row.names = FALSE))
but when I try it, it only exports one file named NA.csv. Do you know how to make any of those solutions work?
Your problem is how you're indexing your list object and names(listofdf)[i] isn't doing what you're thinking. Try This:
listofdf <- list(a = iris, b = iris, c = iris)
for (i in seq_along(listofdf)){
write.csv(listofdf[[i]], file = paste0(names(listofdf)[i], ".csv"), row.names = F)
}
Side note: the default separator for paste is a space. So you're putting a space before the ".csv" extension with your code. paste0 does not paste strings together with a space.
Alternatively, as mentioned you can use writexlsx by simply:
library(writexl)
write_xlsx(listofdfs, "output.xlsx")
This will create a file called "output.xlsx" with sheets that match names(listofdfs) and the proper values stored within those sheets.

Exporting a tibble with class "tbl_df/tbl/data.frame" as csv in R

I have a tibble in the format as that of the image. Its an output of the random forest executed on a grouped or nested data-frame. The class of the tibble is "tbl_df/ tbl/data.frame". When I am trying to export, it shoots an error
"Error in write.table(df6_df, "df6_df.csv", row.names = F, col.names = TRUE, : unimplemented type 'list' in 'EncodeElement' ".
The unlist() is extracting only the first list that belongs to one Subcat_ID, but there are many other SubCat_IDs which have such a list.
Thanks in advance!

Export SpectraObjects to csv in ChemoSpec

I am using ChemoSpec to analyse FTIR spectra in R.
I was able to import several csv files using files2SpectraObject, applied some of the data pre-processing procedures, such as normalization and binning, and generated new SpectraObjects with the results.
Is it possible to export data back to csv format from the generated SpectraObjects?
So far I tried this
write.table(ftirbin, "E:/ftirbin.txt", sep="\t")
and got this:
Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) :
cannot coerce class ""Spectra"" to a data.frame
Thanks in advance!
G
If you look at ?Spectra you'll see how a Spectra object is stored. The intensity values are in your_object$data, and the frequency values are in your_object$freq. So you can't export the whole object (it's not a data frame, but rather a list), but you can export the pieces. To export the frequencies in the first column, and the samples in the following columns, you can do this (example uses a built in data set, SrE.IR):
tmp <- cbind(SrE.IR$freq, t(SrE.IR$data))
colnames(tmp) <- c("freq", SrE.IR$names)
tmp <- as.data.frame(tmp) # it was a matrix
Then you can write it out using write.csv or write.table (check the arguments to avoid row numbers).

Print ncvTest to csv file

In R i run a ncvTest for heteroscedasticity. But i can't seem to print the result into a csv file. This is what i have done,
ncvt<-ncvTest(pol_reg)
outss<-file(paste0("hetero_test.csv"))
write.csv(ncvt,outss)
I get the following error message,
Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) :
cannot coerce class ""chisqTest"" to a data.frame
What am I suppose to do in order to save the result into a csv file. The result of ncvt looks as follows,
Non-constant Variance Score Test
Variance formula: ~ fitted.values
Chisquare = 75514.06 Df = 1 p = 0
You can pull out components of your ncvt list and make a dataframe out of it to write to a csv file:
ncvt<-ncvTest(pol_reg)
ds_ncvt <- data.frame(ncvt$formula.name, ncvt$ChiSquare, ncvt$Df, ncvt$p, ncvt$test)
outss<-file(paste0("hetero_test.csv"))
write.csv(ds_ncvt,outss)
Thanks, the following also works
ncvt<-ncvTest(pol_reg)
ds_ncvt <- as.matrix(ncvt)
outss<-file(paste0("hetero_test.csv"))
write.csv(ds_ncvt,outss)

Resources