[R] Why isn't R recognising integers as numbers?

Marc Schwartz marc_schwartz at comcast.net
Mon Sep 22 04:49:14 CEST 2008


on 09/21/2008 09:09 PM Ted Byers wrote:
> Thanks Marc,
> 
> That was it. 
> 
> For the last 30 years, I'd write my own code, in FORTRAN, C++, or even Java,
> to do whatever statistical analysis I needed.  When at the office, sometimes
> I could use SAS, but that hasn't been an option for me in years.
> 
> This is the first time I have had to load real data into R (instead of
> generating random data to use while playing with some of the stats
> functions, or manually typing dummy data).
> 
> I take it, then, that the result of loading data is a data frame, and not
> just a matrix or array.  Using something like "refdata18[, 1]" feels rather
> alien, but I'm sure I'll quickly get used to it.  I'd seen it before in the
> R docs, but it didn't register that I had to use it to get the functions of
> most interest to me to recognise my data as a vector of numbers, given I'd
> provided only a vector of integers as input.

<snip>

Ted,

If you read the 'Value' section of ?read.csv, it indicates that the
function returns a data frame. It is important to fully read the help
page for new functions so that you understand both how they are used and
the result(s) of their actions, including the 'Notes' section, which can
include further details, including gotchas and idiosyncrasies.

A data frame will be the result of read.csv() even if the data source is
a single column. Think of a data frame in the same way as a spreadsheet
or database table with one or more columns and one or more rows. The
unique aspect of a data frame is that each column can be a different
data type, though that need not be the case.

Thus, you still need to identify the column within the data frame that
you wish to manipulate/analyze further. There are various ways of doing
this, which are covered in Chapter 6 of "An Introduction to R" on Lists
and Data Frames. Some involve the use of indices, others using a column
name, as appropriate. There will be situations where they can be
interchangeable and others where one method will be superior to the
other. Time and experience will provide insight and intuition.

There are a myriad of ways of reading data into R and these are covered
in the Data Import/Export manual. Not all result in a data frame, but in
general and perhaps most commonly, that will be the result.

HTH,

Marc



More information about the R-help mailing list