Home > OS >  How can one write a list in R to csv or txt file where each element of the list is a character strin
How can one write a list in R to csv or txt file where each element of the list is a character strin

Time:08-25

I have a long (47,000 elements) and large character list (20 MB), IVs_selected_by_LASSO, where each entry/element is a character string of a varying length. They are all of varying lengths because each one stores the names of all the IVs/factors/predictors (out of a total of 30 candidate factors) which have been 'selected' by the LASSO Regression ran on that dataset, and there were 47k datasets that I ran them on. The final output I am looking for should be a csv or txt file with 47k rows where each row is formatted in the following manner: csv_file_name, n_i, n_i, n_i, n_i, etc. where i ranges from 1:31. So for example: 0.4-3-1-1, 1, 2, 3 0.4-3-1-2, 2, 3, 6, 13, 21, 22, 23, 28, 31 0.4-3-1-3, 1, 4 0.4-3-1-1, 3, 8, 9, 10, 11 etc.

However, for now at least, I would be okay with the same as the above but with an X in front of all the factors selected by each LASSO Regression.

The following outputs are included to show you the form & state of the IVs_selected_by_LASSO list in precise detail:

> head(IVs_selected_by_LASSO, n = 4)
[[1]]
[1] "X1" "X2" "X4" "X7" "X8" "X9"

[[2]]
[1] "X1" "X2" "X4" "X6" "X7" "X8" "X9"

[[3]]
[1] "X1" "X2" "X3" "X6" "X7" "X8"

[[4]]
[1] "X1"  "X2"  "X3"  "X5"  "X7"  "X8"  "X9"  "X10"

> str = str(IVs_selected_by_LASSO)
List of 47500
 $ : chr [1:6] "X1" "X2" "X4" "X7" ...
 $ : chr [1:7] "X1" "X2" "X4" "X6" ...
 $ : chr [1:6] "X1" "X2" "X3" "X6" ...
 $ : chr [1:8] "X1" "X2" "X3" "X5" ...
  [list output truncated]
> str(IVs_selected_by_LASSO[1])
List of 1
 $ : chr [1:6] "X1" "X2" "X4" "X7" ...
> str(IVs_selected_by_LASSO[2])
List of 1
 $ : chr [1:7] "X1" "X2" "X4" "X6" ...

The names of each of the datasets have already been stores a character string called DS_names_list which has the following structure:

> str(DS_names_list)
 chr [1:47500] "0-10-1-1" "0-10-1-10" "0-10-1-100" "0-10-1-101" "0-10-1-102" ...

The 2nd one is easy to write to a list by itself, I successfully did so by running:

write.csv(DS_names_list, file = 'list_of_dataset_names.csv')

But my problem is when trying to do the same for the results of my LASSOs, I tried the following and always get the following error message back:

write.csv(IVs_selected_by_LASSO, file = 'IVs_selected_by_LASSO.csv')
> write.csv(IVs_selected_by_LASSO, file = 'IVs_selected_by_LASSO.csv')
Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE,  : 
  arguments imply differing number of rows: 6, 7, 8, 9, 10, 5, 11, 12, 13, 14, 15, 3, 2, 4, 1, 0

p.s. I have also tried the following alternative, with the same error message:

write.table(IVs_selected_by_LASSO, file = 'LASSO_output.txt')
> write.table(IVs_selected_by_LASSO, file = 'LASSO_output.txt')
Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE,  : 
  arguments imply differing number of rows: 6, 7, 8, 9, 10, 5, 11, 12, 13, 14, 15, 3, 2, 4, 1, 0

CodePudding user response:

cat(sapply(IVs_selected_by_LASSO, toString), sep="\n", file = 'IVs_selected_by_LASSO.csv')
  • Related