在R中将多长度列表导出到excel
我是一个初学者,已经搜索了整个存储库,我希望将以下代码的结果导出到xlsx中。你能帮我吗在R中将多长度列表导出到excel,r,list,export-to-excel,R,List,Export To Excel,我是一个初学者,已经搜索了整个存储库,我希望将以下代码的结果导出到xlsx中。你能帮我吗 y<-seq(1980,1987,1) Out <- apply(expand.grid(y),1,function(x){ i<-"http://en.wikipedia.org/wiki/List_of_Bollywood_films_of" j<-paste(i,x[1],sep="_") url<-j # extract all tables on the page
y<-seq(1980,1987,1)
Out <- apply(expand.grid(y),1,function(x){
i<-"http://en.wikipedia.org/wiki/List_of_Bollywood_films_of"
j<-paste(i,x[1],sep="_")
url<-j
# extract all tables on the page
tabs = readHTMLTable(url, stringsAsFactors = F)
# locate tables containing call and put information
top_10 = tabs[[2]]
Allmov = tabs[[3]]
Topgenre = merge(top_10,Allmov,by.x="Title",by.y="Title")
year<-paste(x[1],"Rank",sep="")
myvars = c("Title",year,"Genre")
Top = Topgenre[,myvars]
Top[,2]<-x[1]
data = Top
rm(Allmov,Topgenre,top_10,i,j,year,myvars,tabs,url)
return(data)
})
y您可以使用write.csv
为列表中的每个元素创建不同的.csv
文件。然后这些文件可以很容易地读入Excel电子表格
> lapply(seq(Out), function(i){
write.csv(Out[[i]], file = paste0("Out", i, ".csv"), row.names = FALSE)
})
如果希望将整个列表发送到单个文件:
> file.create("myfile.txt")
> lapply(seq(Out), function(i){
capture.output(print(Out[[i]]), file = "myfile.txt", append = TRUE)
})
如果实际列表很长,您可能希望使用invisible()
包装这些调用中的任何一个,以抑制lappy
输出
旁注:您有几行不必要的代码。例如,url阅读write.table
或write.csv
的帮助。然后,您可以在excel中打开这些文件。输出时,列表的长度是可变的。我尝试过write.table或write.csv,但对我不起作用。请你把准确的代码发给我好吗?但是我希望所有的代码都在一张纸上,一张一张地贴在另一张纸上。请记住,你会想在代码中的某个地方重新创建文件,否则如果你重新运行,你只会继续追加,而不会重新开始。