1

I'm struggling to get any code to run to save the vector a to save as a csv file. I'm keen to see what it looks like after I merge @65-70 text files together. My code to read in the files and merge is:

# set pathway to text files
folder<-"C:\\xxxxxx\\Text files"

# lists all files in pathway 
list.files(path=folder)

# filters text files only
list.files(path=folder, pattern="*.txt")

# set vector
filelist<-list.files(path=folder, pattern="*.txt")

# assign pathways to files
paste(folder, "\\", filelist)

# removes separations in pathways by setting as empty
filelist<-paste(folder, "\\", filelist, sep="")
filelist

# apply a function to read in multiple txt files - warnings are OK
a<-lapply(filelist, FUN=readLines)

I can run head(a,n) to see some of the data but tried to run different code to save it so I can examine if there any patterns.

This didn't work:

write.csv(a, file ="myfile.csv", row.names=FALSE)

brings up this error:

Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE,  : 
  arguments imply differing number of rows: 13336, 1004, 641, 114, 66, 1613, 118, 868, 0, 808, 2300, 771, 6096, 1466, 717, 110, 794, 4155, 875, 1139, 574, 43, 867, 970, 3252, 679, 4630, 836, 391, 1304, 219, 865, 1349, 7209, 878, 952, 1611, 16333, 430, 244, 3794, 2849, 1697, 895, 874, 2021, 693, 1537, 119, 2014, 3185, 3282, 2822, 275, 3547, 1517, 1636, 921, 543, 2411

Any help appreciated.

Update - used code to collapse and tried to save but still errors.

# apply a function to collaspe into a single element
corpus<-lapply(a, FUN=paste, collaspe=" ")

#save the file in our current working directory, without row names and save as csv
write.csv(corpus, file="corpus.csv", row.names=T)

Ran head to see some of the data head(corpus, 10)

The xxxxx elements are respondent responses so redacted. I want to save this group of text files to check for patterns in the data. The other text left in are the standard quetsions/text in the files I want to try and exclude.

[698] "c. Carbon Capture and Storage   "                                                                          
[699] "  "                                                                                                        
[700] "  "                                                                                                        
[701] "  "                                                                                                        
[702] "  "                                                                                                        
[703] "  "                                                                                                        
[704] "Q42. What steps, if any, should NI policy-makers consider with regard to the   "                           
[705] "development or implementation of CCUS in NI?   "                                                           
[706] "  "                                                                                                        
[707] "xxx   "                                                                                                    
[708] "  "                                                                                                        
[709] "xxxx xxxxxxxxx xxx xxxxx xxxxxxxxxx xxxxxx xxxxxx xxxxx be xxxxxxxxxxxxx   "                                
[710] "  "                                                                                                        
[711] "  "                                                                                                        
[712] "  "                                                                                                        
[713] "   "                                                                                                       
[714] "  "                                                                                                        
[715] "\f  "                                                                                                      
[716] "d. Energy and the Economy   "                                                                              
[717] "  "                                                                                                        
[718] "  "                                                                                                        
[719] "  "                                                                                                        
[720] "  "                                                                                                        
[721] "  "                                                                                                        
[722] "   "                                                                                                       
[723] "  "                                                                                                        
[724] "Q43. What specific economic opportunities will arise from the decarbonisation of   "                       
[725] "energy?   "                                                                                                
[726] "  "                                                                                                        
[727] "Q44. What skills are needed to realise the potential economic benefits of energy in   "                    
[728] "the future?   "                                                                                            
[729] "  "                                                                                                        
[730] "Q45. What are your views on the future of overall energy demand in NI and how can   "                      
[731] "we ensure that any potential demand growth aligns with our net zero carbon   "                             
[732] "target?   "                                                                                                
[733] "  "                                                                                                        
[734] "xxx   "                                                                                                    
[735] "  "                                                                                                        
[736] "xxxxxxxx xxxxxxxx xxxxxx xxxxx xxxx xxxxxx xxxxxx xxxxxxxx xxx xxxxx xxxxxx   "                   
[737] "xx xxxxx xxx xxxxxxx xx xxxxx xxxxx xx x xxxxxxx xxxxxx   "                                          
[738] "  "                                                                                                        
[739] "  "                                                                                                        
[740] "  "                                                                                                        
[741] "\f  "                                                                                                      
[742] "e. Delivery Framework for an Energy Strategy   "                                                           
[743] "  "                                                                                                        
[744] "  "                                                                                                        
[745] "  "                                                                                                        
[746] "  "                                                                                                        
[747] "  "                                                                                                        
[748] "   "                                                                                                       
[749] "  "                                                                                                        
[750] "   "                                                                                                       
[751] "  "                                                                                                        
[752] "Q46. Do the existing division of responsibilities and powers across government   "                         
[753] "enable the most effective approach to the overall aim of decarbonising energy?   "                         
[754] "If not, what are your suggestions for improvement?   "                                                     
[755] "  "                                                                                                        
[756] "   "                                                                                                       
[757] "  "                                                                                
Mcguns
  • 35
  • 6
  • 1
    What do you mean by 'merge' in your question? In your case `a` is a list, and you need to flatten before writing to a csv. If each file has columns with the same headers, you can try `do.call('rbind', a)` – nniloc May 29 '20 at 19:58
  • 1
    Also try adding `full.names = TRUE` to `list.files` to skip having to paste in the `folder` path – nniloc May 29 '20 at 20:01
  • Thanks nniloc, under the do.call i received this warning, I take it files are not consistent then?Warning message: In rbind(c("Energy Strategy Call for Evidence", "Response Template", : number of columns of result is not a multiple of vector length (arg 1) – Mcguns May 29 '20 at 21:40
  • 1
    Would be helpful if you showed a reproducible example. A little unclear what the inputs look like and what the desired output is. – nniloc May 29 '20 at 22:19
  • 1
    Could either create some fake data or put a subset of your actual data. [Here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) are some tips on how to create a reproducible example. – nniloc May 29 '20 at 22:37
  • You are trying to write a list to a csv format. The list will not have a correct spacing for the csv. Either try using `writeLines`, or first collapse your list into one big textobject first. – phiver May 30 '20 at 07:17
  • I'm trying to see how the data looks after reading in all the text files. I'd like to be able to merge them and check if patterns in the data I need to clean up first. I did later on add this code this to create a corpus but not what I need first. # apply a function to collaspe into a single element corpus<-lapply(a, FUN=paste, collaspe=" ") – Mcguns May 31 '20 at 01:01
  • Ran this to collapse # apply a function to collaspe into a single element corpus<-lapply(a, FUN=paste, collaspe=" ") Tried to save using this but errors again #save the file in our current working directory, without row names and save as csv write.csv(corpus, file="corpus.csv", row.names=T) Ran head(corpus, 10) to have a look at the data and sample added to original question – Mcguns May 31 '20 at 10:31

0 Answers0