On writing

“That is because he knows, deep down, with an awful clarity, that there are limitless ways to fill a page with words, and that he will never, ever, do it perfectly. On some level, that knowledge haunts him all the time. He will always be juggling words in his head, trying to get them closer to a tantalising, unreachable ideal.” – Gene Weingarten

via http://www.wisdenindia.com/cricket-blog/wodehouse-nineteenth-hole/123623


Tiananmen Square Massacre

Today is the 25th anniversary of the Tiananmen Square massacre that left thousands dead, mostly young pro-democracy protesters. One of the most powerful images from that time is that of “Tank Man”, the unknown man who stood in front of a line of tanks and became the instant symbol of the pro-democracy movement. I love Pico Iyer’s description in his TIME Magazine article from 1998:

The meaning of his moment–it was no more than that–was instantly decipherable in any tongue, to any age: even the billions who cannot read and those who have never heard of Mao Zedong could follow what the “tank man” did. A small, unexceptional figure in slacks and white shirt, carrying what looks to be his shopping, posts himself before an approaching tank, with a line of 17 more tanks behind it. The tank swerves right; he, to block it, moves left. The tank swerves left; he moves right. Then this anonymous bystander clambers up onto the vehicle of war and says something to its driver, which comes down to us as: “Why are you here? My city is in chaos because of you.” One lone Everyman standing up to machinery, to force, to all the massed weight of the People’s Republic–the largest nation in the world, comprising more than 1 billion people–while its all powerful leaders remain, as ever, in hiding somewhere within the bowels of the Great Hall of the People.

So here’s to you, Tank Man. I hope Chairman Mao was right when he said “I think never killed”. May you continue to be remembered and honoured through blog posts, tattoos and LEGOs.

Behind the scenes: Tank Man of Tiananmen, NYT 3 June 2009.

Tank man tattoos:

Tank man LEGO:

When your write.csv won’t write

Today I learned that:

  • A column in a data frame can actually be a list even if it looks nice when you peek at it with head() and tail()
  • If you take this data frame and try to write it to a csv file using write.csv, you’re probably going to get an error like this in mean red letters:

Error in .External2(C_writetable, x, file, nrow(x), p, rnames, sep, eol, :
unimplemented type ‘list’ in ‘EncodeElement’

To avoid this, you should first check the data type for each column:

sapply(df, class)

Use unlist() or similar to fix the offending columns. This should fix the problem and write.csv() will happily dump your data as requested.

Exporting individual sheets from an Excel workbook

I received an Excel file containing several sheets that I needed to process individually in R and I was looking for a way to automate the process (as opposed to File -> Save As.. ~25 times). I came across this page and the method worked like a charm! I modified the script slightly to save as csv rather than xls, thanks to this link.

Sub Splitbook()
MyPath = ThisWorkbook.Path
For Each sht In ThisWorkbook.Sheets
ActiveSheet.Cells.PasteSpecial Paste:=xlPasteValues
ActiveSheet.Cells.PasteSpecial Paste:=xlPasteFormats
ActiveWorkbook.SaveAs _
Filename:= sht.Name & “.csv”, FileFormat:=xlCSV
ActiveWorkbook.Close savechanges:=False
Next sht
End Sub

I also found some posts about doing this within R, but one package that does this (xlsReadWrite — Windows only) has been removed from CRAN and the other (gdata) crashed my R session when I tried it 😦 As CRAN and many other sources advise, the best way to work with Excel files in R is to not work with them.


Note: In Excel for Mac 2011, the steps to run the script are:

– Go to Tools -> Macro -> VB editor

– Insert -> Module; paste the code above into the editor

– Select the code, hit the Run button.


Bookends to Endnote

I recently made the switch from Bookends to Endnote and while the transfer of references was relatively smooth, converting documents hasn’t been as straightforward. Quick tip if you’re in the same boat: Before uninstalling Bookends, be sure to unformat your citations so that they’re no longer field codes within Word. Once that’s done, you can change the citations to the format that Endnote likes, e.g. {Author, Year, #} or {Author, Year, Pages}, from Bookends’ format {Author, Year, Journal, Volume, Pages}.

If, like me, you suddenly realize *after* uninstalling Bookends or moving to a different computer that you have a manuscript under revision that has Bookends field codes, you may have to go through a slightly annoying process of manually replacing field codes with plain text. Bookends’ field codes are something like {ADDIN BEC {Author, ….}}. So after making the codes visible (Toggle field codes), you can extract the temporary citation, modify it for Endnote’s benefit and delete the {ADDIN BEC }. When Endnote scans the document, it should be able to correctly generate the bibliography.

Here’s a list of keyboard shortcuts I found quite useful: http://word.mvps.org/mac/fieldsinmacword.html

Of course, I might be missing some far easier method. If so, let me know!