Export a list into a CSV or TXT file in R
So essentially you have a list of lists, with mylist being the name of the main list and the first element being $f10010_1
which is printed out (and which contains 4 more lists).
I think the easiest way to do this is to use lapply
with the addition of dataframe
(assuming that each list inside each element of the main list (like the lists in $f10010_1
) has the same length):
lapply(mylist, function(x) write.table( data.frame(x), 'test.csv' , append= T, sep=',' ))
The above will convert $f10010_1
into a dataframe then do the same with every other element and append one below the other in 'test.csv'
You can also type ?write.table
on your console to check what other arguments you need to pass when you write the table to a csv file e.g. whether you need row names or column names etc.
R: How to save lists into csv?
Not tested, but from what I've read online, it seems like the following should work:
Convert the
list
to adata.frame
library(plyr)
tweets.df = ldply(tweets, function(t) t$toDataFrame())Use
write.csv
as before, but just on thetweets.df
object instead of thetweets
object.write.csv(tweets.df, file = "newfile.csv")
Sources: Here and here. See also: ?"status-class"
.
Save a data.frame of a list as a csv file in R
If we can create the data.frame
after transposing the 'd', then it should also work
output <- data.frame(t(d), dap = c(TRUE, FALSE))
row.names(output) <- NULL
The lapply
in the second line of code is looping over column and then applying the as.list
i.e. it creating a list
of lists
lapply(d, as.list)
#$A
#$A[[1]]
#[1] 2
#$A[[2]]
#[1] 3
#$B
#$B[[1]]
#[1] 1
#$B[[2]]
#[1] 4
When we assign it to 'd' with keeping the same structure ([]
), it would remain as a data.frame
with list
columns
d[] <- lapply(d, as.list)
str(d)
#'data.frame': 2 obs. of 2 variables:
# $ A:List of 2 # each column is a `list`
# ..$ : num 2
# ..$ : num 3
# $ B:List of 2
# ..$ : num 1
# ..$ : num 4
Now, we are doing the rbind
rbind(d, dap = c(T, F))
# A B
#kap 2 1
#sap 3 4
#dap TRUE FALSE
and its transpose,
output <- t(rbind(d, dap = c(T, F)))
output
# kap sap dap
#A 2 3 TRUE
#B 1 4 FALSE
which may look like a regular data.frame output, but it is not
str(output)
#List of 6
# $ : num 2
# $ : num 1
# $ : num 3
# $ : num 4
# $ : logi TRUE
# $ : logi FALSE
# - attr(*, "dim")= int [1:2] 2 3
# - attr(*, "dimnames")=List of 2
# ..$ : chr [1:2] "A" "B"
# ..$ : chr [1:3] "kap" "sap" "dap"
which is again wrappedd with data.frame
str(data.frame(output))
#'data.frame': 2 obs. of 3 variables:
# $ kap:List of 2
# ..$ A: num 2
# ..$ B: num 1
# $ sap:List of 2
# ..$ A: num 3
# ..$ B: num 4
# $ dap:List of 2
# ..$ A: logi TRUE
# ..$ B: logi FALSE
still a data.frame
with list
columns. One option is to loop over the list
, unlist
the list
columns and wrap with data.frame
data
d <- data.frame(list(A = c(kap = 2, sap = 3), B = c(kap = 1, sap = 4)))
How to save an R list as csv or Excel file?
From what I see, your list seems to consist of one single value for each element. You can use Jonathan V. Solórzano's way to get a very wide table. Alternatively you can use unlist()
to collapse your list into a vector, and create a dataframe and export it.
mylist <- list(Embase.1 = 1, Embase.2 = 0, Embase.3 = 2)
v <- unlist(mylist)
df <- data.frame(Embase = v)
df
# Embase
#Embase.1 1
#Embase.2 0
#Embase.3 2
write.csv(df, row.names = F)
How to write a list of lists into a single CSV file in R?
Hang on for dear life...here comes the "never use plyr
" crowd... BUT...
> new_list <- plyr::adply(myList,1,unlist,.id = NULL)
> new_list
V1 V2 V3
1 aa bb cc
2 xx yy zz
> write.csv(new_list, "mycsv.csv")
How to save a list which has files into csv in R?
With base R lapply
try
lapply(seq_along(x), function(i) write.csv(x[[i]],
paste0("/path/of/the/file/", names(x)[i], ".csv"), row.names = FALSE))
Or with imap
purrr::imap(x, write.csv(.x,
paste0("/path/of/the/file/", .y, ".csv"), row.names = FALSE))
Converting a list to csv file using R
library(data.table)
input<-fread(input = "https://www.cs.utah.edu/~lifeifei/research/tpq/cal.cnode")
Check that it was imported OK.
>dim(input)
[1] 21048 3
Finally export it as csv with comma delimiter (by default):
write.csv(input,"output.csv",row.names = F)
How to create a CSV-File for a dataframe with multiple lists in R?
I guess the best way to solve your problem is switching to a more apropriate file format. I recomend using write_rds() from the readr
package, which creates .rds files. The files you create with readr::write_rds('your_file_path') can be read in with readr::read_rds('your_file_path').
The base R functions are saveRDS() and readRDS() and the functions mentioned earlier form the readr
are just wrappers with some convience features.
How to save a dataframe with list to csv file in R
Since it's not a data.frame , and more specifically cannot be coerced to one with as.data.frame which is where that message came from, you need to think of a different method of saving the data. Probably this simplest would be with dput
, which writes an ASCII representation of the list structure:
dput(operacions, file="out.txt")
To bring it back into R:
new <- source("out.txt")
Another method would be to convert to JSON format, which would also preserve the key-value information rather than just writing the values:
library(jsonlite)
toJSON(new)
# value---------
{"value":[{"Nom":["Victor"],"Bolis":["Negro","azul","verde"]},{"Nom":["Dani"],"Lapices":[1,2,3,4]}],"visible":[true]}
You can use the cat function to direct this to a text file:
cat( toJSON(new), file="test.3.txt")
Related Topics
Convert Accented Characters into Ascii Character
Assigning Null to a List Element in R
Extract the Coefficients for the Best Tuning Parameters of a Glmnet Model in Caret
Using R to Read Out Excel-Colorinfo
Select Rows of a Data.Frame That Contain Only Numbers in a Certain Column
Annotate Values Above Bars (Ggplot Faceted)
Weird Characters Added to First Column Name After Reading a Toad-Exported CSV File
How to Use a Graphic Imported with Grimport as Axis Tick Labels in Ggplot2 (Using Grid Functions)
How to One-Hot-Encode Factor Variables with Data.Table
Update a Column of Nas in One Data Table with the Value from a Column in Another Data Table