[R] How to read a *.csv file in R?

peter dalgaard pdalgd at gmail.com
Wed Mar 13 17:08:19 CET 2013


On Mar 13, 2013, at 16:54 , Daniel Nordlund wrote:

>> -----Original Message-----
>> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
>> On Behalf Of Maximus
>> Sent: Wednesday, March 13, 2013 12:15 AM
>> To: r-help at r-project.org
>> Subject: [R] How to read a *.csv file in R?
>> 
>> Hey guys,
>> 
>> I am dealing with this kind of data. To read the file in R I have nulled
>> all
>> empty fields and tried:
>> 
>> date	BRENT	BRENTchg	HWWI	HWWIchg
>> Jan. 86	22,5	NULL	68,1	-15,6
>> Feb.86	17	NULL	64,9	-21,6
>> Mar. 86	13,7	NULL	66,6	-19,5
>> Apr.86	12,3	NULL	63,6	-19,1
>> May 86	14	NULL	61,5	-20,9
>> June 86	11,8	NULL	59,8	-20,7
>> July 86	9,4	NULL	57,2	-19,3
>> Aug.86	13,2	NULL	55,5	-18,3
>> Sep.86	14,2	NULL	57,5	-15,1
>> Oct. 86	13,7	NULL	55,5	-14,1
>> Nov.86	14,4	NULL	54,9	-14,9
>> Dec. 86	15,7	NULL	52,9	-26,4
>> Jan. 87	18,3	-18,67	49,8	-26,87
>> Feb.87	17,3	1,76	49,9	-23,11
>> Mar. 87	17,8	29,93	49,7	-25,38
>> Apr.87	18	46,34	50,5	-20,6
>> May 87	18,6	32,86	52,3	-14,96
>> June 87	18,8	59,32	53,5	-10,54
>> July 87	19,8	110,64	54,5	-4,72
>> Aug.87	18,9	43,18	55,3	-0,36
>> Sep.87	18,2	28,17	55,1	-4,17
>> Oct. 87	18,6	35,77	57,8	4,14
>> Nov.87	17,7	22,92	55,5	1,09
>> Dec. 87	16,8	7,01	56,5	6,81
>> Jan. 88	16,7	-8,74	58,4	17,27
>> Feb.88	15,7	-9,25	59,5	19,24
>> 
>>> heisenberg <- read.csv(file="comprice.csv",head=TRUE,sep="")
>> Error in read.table(file = file, header = header, sep = sep, quote =
>> quote,
>> :
>>  duplicate 'row.names' are not allowed
>> 
>> However, my row names are not duplicated. When I try:
>> 
>>> heisenberg <- read.csv(file="comprice.csv",head=TRUE,sep=",")
>> Error in read.table(file = file, header = header, sep = sep, quote =
>> quote,
>> :
>>  more columns than column names
>> 
>> I have saved the file with excel under *.csv(MSDOS).
>> 
>> How to read this file?
>> 
>> Thank you in advance for your help?
>> 
>> 
> 
> The data appear to be tab delimited with the decimal point being a comma (',').  So, try read.csv2()
> 
> heisenberg <- read.csv2(file="comprice.csv", header=TRUE, sep="\t")
> 
> 

read.delim2() would be more to the point. (M: Are you _sure_ Excel calls this a csv file? Those are usually semicolon-separated in German locales.)

It's usually easier to leave blank fields blank in delimited formats. If you code NULL for missing, you'll need the na.strings= argument.

-pd


> Hope this is helpful,
> 
> Dan
> 
> Daniel Nordlund
> Bothell, WA USA
> 
> 
> ______________________________________________
> 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