Reputation: 24535
I have following text (indented by tabs) on a text editor (Geany) and also in Libreoffice spreadsheet in Debian Stable Linux:
Aorta
coronary
brachio cephalic
left common
external carotid
internal carotid
ophthalmic
left subclavian
vertebral
brachial
left iliac
right iliac
I copy it and use following command to paste it in R:
library(psych)
dat = read.table(text = readClipboard(), sep="\t", header=FALSE, fill=TRUE, strip.white=TRUE, stringsAsFactors=FALSE)
It works well when it is copied from spreadsheet:
dat
V1 V2 V3 V4
1 Aorta
2 coronary
3 brachio cephalic
4 left common
5 external carotid
6 internal carotid
7 ophthalmic
8 left subclavian
9 vertebral
10 brachial
11 left iliac
12 right iliac
But when copied from text editor there is an error. Note empty line 7 and 'ophthalmic' coming in first variable:
dat
V1 V2 V3
1 Aorta
2 coronary
3 brachio cephalic
4 left common
5 external carotid
6 internal carotid
7
8 ophthalmic
9 left subclavian
10 vertebral
11 brachial
12 left iliac
13 right iliac
What could be the cause and how can this be corrected?
Upvotes: 0
Views: 77
Reputation: 193517
Here's a demo reading directly from a tempfile
:
x <- tempfile()
cat("1\t2", "1\t2", "1\t2", "1\t2", "1\t2",
"1\t2", "1\t2", "1\t2\t3", "1\t2", "1\t2",
file=x, sep="\n")
## readClipboard() or readLines(textConnection(readClipboard()))
infile <- readLines(x)
fields <- max(count.fields(textConnection(infile), sep = "\t"))
## Will demonstrate the behavior you describe
read.table(text = infile,
sep = "\t",
header = FALSE,
fill = TRUE)
## Will do what you expect
read.table(text = infile,
col.names = paste0("V", sequence(fields)),
sep = "\t",
header = FALSE,
fill = TRUE)
Thus, try replacing readLines
with readClipboard()
directly, or wrap readClipboard()
in textConnection()
.
Upvotes: 2