啊,现在我明白了。你必须阅读?scan
非常仔细地看看你所做的并不是什么scan()
想要为what
争论。我第一次错过了这个,然后想知道为什么你的代码不起作用。这是关键部分:
what: the type of ‘what’ gives the type of data to be read. The
supported types are ‘logical’, ‘integer’, ‘numeric’,
‘complex’, ‘character’, ‘raw’ and ‘list’.
关键词是type。所以你需要传递一个正确的对象type争论what
.
在你的例子中:
> typeof("logical")
[1] "character"
So scan()
读入类型的对象"character"
.
解决方案很简单,使用what = TRUE
,或者实际上 R 认为合乎逻辑的任何内容(请参阅对此答案的评论),而不是
> typeof(TRUE)
[1] "logical"
> ## or
> typeof(logical())
[1] "logical"
## So now read in with what = TRUE
> a <- scan(file="1.txt", what = TRUE)
Read 5 items
> class(a)
[1] "logical"
> typeof(a)
[1] "logical"
read.table()
在如何告诉它要读取的数据是什么方面更符合逻辑。等效的调用将是:
> b <- read.table("1.txt", colClasses = "logical")[,]
> class(b)
[1] "logical"
> typeof(b)
[1] "logical"
HTH