Skip to content

re ad.table

3 messages · threshold, jim holtman, Simon Pickett

#
Hi , I want to use the read.table to the following example 'data.txt' format:
a       b      c      d      e      f
SPX	LSZ	100	C	0	34.4
SPX	LSZ	100	P	0	1.3
SPX	LSZ	105	C	0	30.3
SPX	LSZ	105	P	0	1.85
SPX	LSZ	110	C	0	26.4

and I want to have the columns a,b,d read as the 'character' data, a the
remaining columns as 'numeric'. What I did was:
read.table("data.txt",header=T,
colClasses=c("character","character","numeric","character",
"numeric","numeric"), but I could not get what I want, since all values were
in the 'character' format. I will apprecaite any suggestions.
best, robert
#
Seems to work for me:
+ SPX     LSZ     100     C       0       34.4
+ SPX     LSZ     100     P       0       1.3
+ SPX     LSZ     105     C       0       30.3
+ SPX     LSZ     105     P       0       1.85
+ SPX     LSZ     110     C       0       26.4"),
+     header=T, colClasses=c("character","character","numeric","character",
+     "numeric","numeric"))
a   b   c d e     f
1 SPX LSZ 100 C 0 34.40
2 SPX LSZ 100 P 0  1.30
3 SPX LSZ 105 C 0 30.30
4 SPX LSZ 105 P 0  1.85
5 SPX LSZ 110 C 0 26.40
'data.frame':   5 obs. of  6 variables:
 $ a: chr  "SPX" "SPX" "SPX" "SPX" ...
 $ b: chr  "LSZ" "LSZ" "LSZ" "LSZ" ...
 $ c: num  100 100 105 105 110
 $ d: chr  "C" "P" "C" "P" ...
 $ e: num  0 0 0 0 0
 $ f: num  34.4 1.3 30.3 1.85 26.4
R version 2.8.0 (2008-10-20)
i386-pc-mingw32

locale:
LC_COLLATE=English_United States.1252;LC_CTYPE=English_United
States.1252;LC_MONETARY=English_United
States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252

attached base packages:
[1] stats     grDevices utils     datasets  graphics  methods   base

other attached packages:
[1] MASS_7.2-44
On Fri, Dec 19, 2008 at 7:35 AM, threshold <r.kozarski at gmail.com> wrote:

  
    
#
probably best just to change each variable one by one to the correct 
classification after you have read the data frame in.

variable<-as.character(variable)


----- Original Message ----- 
From: "threshold" <r.kozarski at gmail.com>
To: <r-help at r-project.org>
Sent: Friday, December 19, 2008 12:35 PM
Subject: [R] re ad.table