我在文本编辑器(Geany)上以及Debian Stable Linux中的Libreoffice电子表格中都有以下文本(缩进标签):
Aorta
coronary
brachio cephalic
left common
external carotid
internal carotid
ophthalmic
left subclavian
vertebral
brachial
left iliac
right iliac
我复制它并使用以下命令将其粘贴到R:
中library(psych)
dat = read.table(text = readClipboard(), sep="\t", header=FALSE, fill=TRUE, strip.white=TRUE, stringsAsFactors=FALSE)
从电子表格中复制时效果很好:
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
但是从文本编辑器复制时会出错。注意空行7和' ophthalmic'进入第一个变量:
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
可能是什么原因以及如何纠正?
答案 0 :(得分:2)
这是直接来自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)
因此,请尝试直接将readLines
替换为readClipboard()
,或将readClipboard()
换成textConnection()
。