1

I have a file which looks like this:

Time,Open,High,Low,Close,Volume
01.02.1995 00:00:00.000,1.57920,1.58400,1.57700,1.58240,0.00
02.02.1995 00:00:00.000,1.58200,1.58620,1.57820,1.58130,0.00
03.02.1995 00:00:00.000,1.58150,1.58280,1.56050,1.56180,0.00
04.02.1995 00:00:00.000,1.56180,1.56180,1.56180,1.56180,0.00

I want a dataframe where the first column is character (changing it to a date is the next step) and the rest numeric, and so I am doing this:

myData <- read.table("C:\\Users\\Adam\\Desktop\\GU95.csv", colClasses=c("character",rep("numeric",5)), header=TRUE, sep = ','))

To check the class of the second column (Open), I am doing this:

myData$Open <- apply(myData, 1, function(row) print(class(row[2])))

However, the output is "character" and not "numeric".

What have i done wrong?

Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138
  • 1
    after loading the data you've shown here, doing `sapply(df, class)` gives me the class of `open` to be `numeric`. there seems to be no issue with your test data – Arun Mar 02 '13 at 10:48
  • 2
    welcome to SO :) following up on @Arun's comment, please carefully [read this document](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) -- providing `C:\\Users\\Adam\\Desktop\\GU95.csv` as your example data isn't particularly helpful. pay attention to the `dput( yourdata )` section – Anthony Damico Mar 02 '13 at 11:07

1 Answers1

3

apply coerce dataframes into matrices or arrays. See ?apply:

If X is not an array but an object of a class with a non-null dim value (such as a data frame), apply attempts to coerce it to an array via as.matrix if it is two-dimensional (e.g., a data frame) or via as.array.

Arrays and matrices can only hold one type of data and since your first column contains characters, everything is coerced to characters.

As @Arun said in his comment you can check the class of each column using sapply(myData, class) or quite simply with str(myData).

plannapus
  • 18,529
  • 4
  • 72
  • 94