0

To learn R, I tried importing data (about spam) contained in a plain text file.

I used the table function, and then tried to convert the corresponding object to a data frame, using two answers to this question.

Here is the code.

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

Why is it that in both case, typeof() returns list? Why not dataframe?

Thanks

Community
  • 1
  • 1
hartmut
  • 934
  • 12
  • 25
  • 2
    You need to learn more about data structures in R. Check `class(spamd)`. Note that `read.table` is documented to return a data.frame. No conversion necessary, – Roland Mar 09 '17 at 11:58

1 Answers1

2

Data frames are really just lists of vectors. From Advanced R by Hadley:

Because a data.frame is an S3 class, its type reflects the underlying vector used to build it: the list. To check if an object is a data frame, use class() or test explicitly with is.data.frame().

You should note that calling length() on a data frame therefore returns the number of columns, not the number of rows. Try:

length(spamd)
ncol(spamd)
nrow(spamd)
coletl
  • 803
  • 5
  • 17