I'm a new R user so this is possibly a naive question. I'm trying to load an external CSV file into a dataframe using: df_name<-read.table("myfile.csv") myfile.csv should have 5 elements per row, though a percentage are missing the last two elements (the commas are present as placemarkers). However, R does not create the dataset but returns the message: line 1 did not have 2 elements but examining the file shows that it does. These are the first 5 rows: DARWIN,NT,800,-12.461334,130.841904 ALAWA,NT,810,-12.37896,130.874226 BRINKIN,NT,810,-12.364533,130.869394 CASUARINA,NT,810,-12.373112,130.883619 COCONUT GROVE,NT,810,-12.397685,130.852324 Is R complaining because the first two elements of each row are not numeric, or am I doing something else wrong? Thanks for your suggestions regards Norman Jessup
Tena koe Norman The default separator for read.table is '' so you need to specify it as a comma: dfName <- read.table("myfile.csv", sep=',') or use read.csv(). HTH .... Peter Alspach> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Norman Jessup > Sent: Friday, 2 July 2010 1:18 p.m. > To: r-help at r-project.org > Subject: [R] Files with Missing Data > > I'm a new R user so this is possibly a naive question. I'm trying to > load an external CSV file into a dataframe using: > > df_name<-read.table("myfile.csv") > > myfile.csv should have 5 elements per row, though a percentage are > missing the last two elements (the commas are present as placemarkers). > However, R does not create the dataset but returns the message: > > line 1 did not have 2 elements > > but examining the file shows that it does. These are the first 5 rows: > > DARWIN,NT,800,-12.461334,130.841904 > ALAWA,NT,810,-12.37896,130.874226 > BRINKIN,NT,810,-12.364533,130.869394 > CASUARINA,NT,810,-12.373112,130.883619 > COCONUT GROVE,NT,810,-12.397685,130.852324 > > Is R complaining because the first two elements of each row are not > numeric, or am I doing something else wrong? > Thanks for your suggestions > > regards > > Norman Jessup > > ______________________________________________ > 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.
Norman Jessup wrote:> I'm a new R user so this is possibly a naive question. I'm trying to > load an external CSV file into a dataframe using: > > df_name<-read.table("myfile.csv") > > myfile.csv should have 5 elements per row, though a percentage are > missing the last two elements (the commas are present as placemarkers). > However, R does not create the dataset but returns the message: > > line 1 did not have 2 elements > > but examining the file shows that it does. These are the first 5 rows: > > DARWIN,NT,800,-12.461334,130.841904 > ALAWA,NT,810,-12.37896,130.874226 > BRINKIN,NT,810,-12.364533,130.869394 > CASUARINA,NT,810,-12.373112,130.883619 > COCONUT GROVE,NT,810,-12.397685,130.852324 > > Is R complaining because the first two elements of each row are not > numeric, or am I doing something else wrong? > Thanks for your suggestionsYou can use Peter's advice; also note there is a read.csv function. A thorough reading of the ?read.table help page is very useful I've found. There is also an R data import/export manual on CRAN.