An embarrassing admission; Copy pasting tables with text containing spaces from Excel to R
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
I can’t believe I didn’t learn how to do it earlier, but I never knew how to accurately copy tables from excel that had text with spaces in them, and paste into a data frame in R without generating confusion around spaces representing different variables.
Say you have a column title in a table in excel like “Group Size”. You then copy the table and try to load it into R using: my.table = read.table(“clipboard”, header=TRUE). R then gives you an error like this:
Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, : line 1 did not have 2 elements
That’s where I stopped most times and made sure that my column titles had no spaces in them (replacing spaces with dots), or just did something else entirely. Finally, I learned that if you add one teeny tiny argument to the read.table command… read.table(“clipboard”, header=TRUE, sep=”\t”) … then all text values with spaces in them actually get imported into R without a fuss! Spaces in column names get converted into dots automatically, but actual text values in the columns with spaces are untouched and accessible as they should be in R. Yay!
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.