Error When Exporting Dataframe to Text File in R

Error when exporting dataframe to text file in R

The solution by agstudy provides a great quick fix, but there is a simple alternative/general solution for which you do not have to specify the element(s) in your data.frame that was(were) nested:

The following bit is just copied from agstudy's solution to obtain the nested data.frame dd:

Month=data.frame(Dates= as.Date("2003-02-01") + 1:15,
Month=gl(12,2,15))
dd <- data.frame(Age=1:15)
dd$Month <- Month

You can use akhilsbehl's LinearizeNestedList() function (which mrdwab made available here) to flatten (or linearize) the nested levels:

library(devtools)
source_gist(4205477) #loads the function

ddf <- LinearizeNestedList(dd, LinearizeDataFrames = TRUE)
# ddf is now a list with two elements (Age and Month)

ddf <- LinearizeNestedList(ddf, LinearizeDataFrames = TRUE)
# ddf is now a list with 3 elements (Age, `Month/Dates` and `Month/Month`)

ddf <- as.data.frame.list(ddf)
# transforms the flattened/linearized list into a data.frame

ddf is now a data.frame without nesting. However, it's column names still reflect the nested structure:

names(ddf)
[1] "Age" "Month.Dates" "Month.Month"

If you want to change this (in this case it seems redundant to have Month. written before Dates, for example) you can use gsub and some regular expression that I copied from Sacha Epskamp to remove all text in the column names before the ..

names(ddf) <- gsub(".*\\.","",names(ddf))  
names(ddf)
[1] "Age" "Dates" "Month"

The only thing left now is exporting the data.frame as usual:

write.table(ddf, file="test.txt")

Exporting non-dataframe data to a text file in R

Give this a try -

capture.output(summary(phylo.signal), file = "test1.txt")

Cannot export data to a file in R (write.csv)

First part is to check the working directory and ensure that you have write access to that directory. You can check this with getwd(). I can reproduce your error by trying to write to a directory which is read only.

To set the working directory to something else with read access you can type setwd("H:/foo").
Once you have write access the write.csv(x,file='whatever.csv') should work.

Exporting dataframe via write.csv returns error

Change this:

write.csv <- (CensusAll, row.names=FALSE, file="c:/CensusAll_Apr19.csv")

to this:

write.csv(CensusAll, row.names=FALSE, file="c:/CensusAll_Apr19.csv")

R - Error when writing to csv

I am assuming here you don't want nested data.frames. Then just do something like this:

new_dataset <- data.frame(subset(dataset, select = -c(SentimentScore)), 
dataset$SentimentScore)
write.csv(new_dataset,
file = "dataset.csv",
quote = FALSE)

Error in exporting data.frame in excel/csv using R

Reason for error is your end result dataframe contains nested dataframes for the nested subrequest portion in the json file. You can see with str(final1). So even base functions write.table() and write.csv() including the xlsx package's write.xlsx() will fail in outputting to flat formats.

Consider flattening by binding the subrequests data frame and merging them to larger final1 columns using the row's id variable. Ultimately, you will obtain a dataframe of 13 observations (not the 9 elements from json, because one of them contained 5 nested subrequests: id = 71a09900-1c13).

# SUBREQUEST BINDING (PULLING CORRESPONDING ID)
dfList <- lapply(1:nrow(final1), function(i){
cbind(id = final1$`_id`[[i]],
final1$uiSearchRequest.subRequests[[i]])
})

# USE DPLYR'S bind_rows() IF dfs DIFFER IN NUMBER OF COLUMNS
subdf <- bind_rows(dfList)
# subdf <- data.frame(do.call(rbind, dfList))

# FINAL1 EXTRACTION
fdf <- data.frame(
id = final1$`_id`,
travelDate = final1$uiSearchRequest.travelDate,
travelDuration = final1$uiSearchRequest.travelDuration,
shopperDuration = final1$uiSearchRequest.shopperDuration,
oneway = final1$uiSearchRequest.oneWay,
userId = final1$uiSearchRequest.userId,
queryId = final1$uiSearchRequest.queryId,
downloadCount = final1$downloadCount,
requestDate = final1$requestDate,
totalRecords = final1$totalRecords,
status = final1$status,

stringsAsFactors = FALSE,
row.names = NULL
)

# MERGE
finaldf <- merge(fdf, subdf, by="id")

Alternatively, you can bind iteratively by rows:

dfList <- lapply(1:nrow(final1), function(i){      
data.frame(
id = final1$`_id`[[i]],
travelDate = final1$uiSearchRequest.travelDate[[i]],
travelDuration = final1$uiSearchRequest.travelDuration[[i]],
shopperDuration = final1$uiSearchRequest.shopperDuration[[i]],
oneway = final1$uiSearchRequest.oneWay[[i]],
userId = final1$uiSearchRequest.userId[[i]],
queryId = final1$uiSearchRequest.queryId[[i]],
final1$uiSearchRequest.subRequests[[i]],
downloadCount = final1$downloadCount[[i]],
requestDate = final1$requestDate[[i]],
totalRecords = final1$totalRecords[[i]],
status = final1$status[[i]],

stringsAsFactors = FALSE,
row.names = NULL
)
})

finaldf <- do.call(rbind_rows, dfList)

Why is R not reading a specific text file when it can read others in the same directory?

Looking at your file it is not really CSV (comma separated) but probably TSV (tab-separated). Because of that, you should rather use read_tsv() function.

Moreover, the file has probably BOM so the first column will get 3 extra symbols at the beginning of the name of the first column. I don't know any better with tidyverse than using rename():

library(tidyverse)

read_tsv('filename.csv') %>%
rename(userid.ID = colnames(.)[1])


Related Topics



Leave a reply



Submit