Как экспортировать и сохранить вектор из R - PullRequest
1 голос
/ 29 мая 2020

Я изо всех сил пытаюсь получить какой-либо код для запуска, чтобы сохранить вектор a для сохранения как файл csv. Я очень хочу посмотреть, как это будет выглядеть после объединения текстовых файлов @ 65-70 вместе. Мой код для чтения в файлах и слияния:

# 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)

Я могу запустить head(a,n), чтобы увидеть некоторые данные, но попытался запустить другой код, чтобы сохранить его, чтобы я мог проверить, есть ли какие-либо шаблоны.

Это не сработало:

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

вызывает эту ошибку:

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

Любая помощь приветствуется.

Обновление - использовал код для свертывания и пытался сохранить, но все еще ошибки.

# 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)

Заглянул, чтобы увидеть часть заголовка данных (корпус, 10)

Элементы xxxxx являются ответами респондентов так отредактировано. Я хочу сохранить эту группу текстовых файлов, чтобы проверить закономерности в данных. Другой оставленный текст - это стандартные запросы / текст в файлах, которые я хочу исключить.

[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] "  "                                                                                
...