Q:

(R) How to append each item of a list to CSV preceded by the list item's name?

I have a list that contains some automatically generated writeups of some data I scraped off TripAdvisor. The name of each item on the list is the name of the hotel. So a sample list item would look like:

$"Holiday Inn Express & Suites Dayton South Franklin"
[1] "The Holiday Inn Express & Suites Dayton South Franklin is located
at 851 Commerce Center Dr,  ..."

I then have a lapply function that takes each writeup from the list and appends it to a CSV file.
lapply(writeups, function(x) write.table( data.frame(x), 'writeups.csv', append= T, sep=',', row.names = F ))

The problem is that when it appends each list item to the CSV, it prints out an "x" on the line above the writeup.

x
writeup1
x
writeup2
x

I can't quite figure out a way to change the x to the name of the list item. Does anybody have any ideas? Thank you.

A:

For loops aren't nice looking but I think it could help you in this case.
for(i in 1:length(your.list)){
  d<-paste0('\n\n',names(your.list)[i],'\n\n')
  capture.output(cat(d),file='writeups.csv',append=T)
  write.table(your.list[i],file='writeups.csv',append= T, sep=',', row.names = F)
}

The first command line inside the loop get the name of item[i] in the list, storing it at the beginning.
I hope it helps you,
Regards.

