R 未成功(不可见?)转换到数据帧

R 未成功(不可见?)转换到数据帧,r,list,dataframe,data-structures,R,List,Dataframe,Data Structures,为了学习R,我尝试导入纯文本文件中包含的数据(关于垃圾邮件) 我使用了table函数,然后尝试将相应的对象转换为一个数据帧,使用两个问题的答案 这是代码 file <- "./spam.data.txt" spamd <- read.table(file, sep = "" , header = F, stringsAsFactors= F) spamd <- as.data.frame(spamd) typeof(spamd) # list spamd <-

为了学习R,我尝试导入纯文本文件中包含的数据(关于垃圾邮件)

我使用了table函数,然后尝试将相应的对象转换为一个数据帧,使用两个问题的答案

这是代码

file <- "./spam.data.txt"

spamd <- read.table(file, sep = "" , header = F, stringsAsFactors= F)
spamd <- as.data.frame(spamd)
typeof(spamd)    # list

spamd <- read.table(file, sep = "" , header = F, stringsAsFactors= F)
spamd <- as.data.frame.matrix(spamd)
typeof(spamd)    # list

file数据帧实际上只是向量列表。摘自哈德利:

因为
data.frame
是一个S3类,所以它的类型反映了用于构建它的底层向量:列表。要检查对象是否是数据帧,请使用
class()
或使用
is.data.frame()
显式测试

您应该注意,在数据帧上调用
length()
,因此返回的是列数,而不是行数。尝试:

length(spamd)
ncol(spamd)
nrow(spamd)

数据帧实际上只是向量列表。摘自哈德利:

因为
data.frame
是一个S3类,所以它的类型反映了用于构建它的底层向量:列表。要检查对象是否是数据帧,请使用
class()
或使用
is.data.frame()
显式测试

您应该注意,在数据帧上调用
length()
,因此返回的是列数,而不是行数。尝试:

length(spamd)
ncol(spamd)
nrow(spamd)

您需要了解有关R中数据结构的更多信息。请检查
class(spamd)
。请注意,
read.table
被记录为返回data.frame。无需转换,您需要了解有关R中数据结构的更多信息。请检查
class(spamd)
。请注意,
read.table
被记录为返回data.frame。不需要转换,