[R] factor coercion with read.csv or read.table

peter dalgaard pdalgd at gmail.com
Thu Jun 7 09:36:26 CEST 2012


On Jun 7, 2012, at 09:25 , Rui Barradas wrote:

> Hello,
> 
> Try option stringsAsFactors, see ?read.csv or ?read.table
> As for the thousands separator, see ?format

help(as.Date) should also help. (Hint: there's no dateFormat= argument)

> 
> Hope this helps,
> 
> Rui Barradas
> 
> Em 07-06-2012 03:09, eric escreveu:
>> How do I fix this error ? I tried coercion to a vector but that didn't work.
>> 
>> msci <-read.csv("..MSCIexUS.csv", header=TRUE)
>> 
>> head(msci)
>> 
>>          Date  index
>> 1 Dec 31, 1969    100
>> 2 Jan 30, 1970 97.655
>> 3 Feb 27, 1970 96.154
>> 4 Mar 31, 1970 95.857
>> 5 Apr 30, 1970 85.564
>> 6 May 29, 1970 79.005
>> 
>>> str(msci)
>> 'data.frame':	510 obs. of  2 variables:
>>  $ Date : Factor w/ 510 levels "Apr 28, 1972",..: 98 178 134 311 13 342 268
>> 228 55 481 ...
>>  $ index: Factor w/ 510 levels "100","1,000.302",..: 1 499 493 488 444 412
>> 418 434 441 448 ...
>> 
>> 
>>> msci$Date <-as.Date(msci$Date, dateFormat='%b %d, %Y')
>> Error in charToDate(x) :
>>   character string is not in a standard unambiguous format
>> 
>> 
>> --
>> View this message in context: http://r.789695.n4.nabble.com/factor-coercion-with-read-csv-or-read-table-tp4632622.html
>> Sent from the R help mailing list archive at Nabble.com.
>> 
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>> 
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list