0

I'm currently trying to read two .csv files, edit the data, and then write it into a new .csv.

here the code :

data <- read.csv("file.csv"), fill=TRUE, header=TRUE, row.names=NULL, stringAsFactors=FALSE, sep=",", quote="")
write.csv(data, file="out.csv")

Here's the problem : Everything is fine with the first file (20 columns, 572 observations)

However, the other file has 163 columns and 1578 lines but when I read it with read.csv, R displays "2301 observations of 163 variables".

I tried to write this dataframe into a new csv file, and it is a total mess :

  1. the rows have not been written entirely, the last values are written on a new row
  2. there is a new column with integers from 1 to 2301
  3. some data which is supposed to be in file$n is written in file$(n-1) or file$(n-2)

I'm a newbie, and I must admit I'm kind of lost : any help would be highly appreciated!

Thanks

Clément

Clément F
  • 3,535
  • 6
  • 18
  • 26
  • 5
    Difficult to say without the file. –  Jul 08 '14 at 10:28
  • 1
    2. The new column is the row names, use `row.names=FALSE` in the `write.csv` call. – James Jul 08 '14 at 10:35
  • Unfortunately the file is confidential... I cannot upload it without modifying it. However : I deleted a few columns, and now R reads it propely : 1578 lines and the new number of columns. Is there a maximum number of columns read.csv can actually read? – Clément F Jul 08 '14 at 10:35
  • You know what we want...your code that produced the problem! – Rich Scriven Jul 08 '14 at 10:35
  • How do you know the csv has 1578 lines? Is that just 1578 rows in your spreadsheet, or a line-count in an editor or something? Some spreadsheets write line-breaks and that might mess up the way R reads it back in. Find out where the R input starts to go wrong and match that up with the CSV. – Spacedman Jul 08 '14 at 10:38
  • Basically, it's 1578 in the spreadsheet. I wil perform a line count on the raw file. – Clément F Jul 08 '14 at 10:40
  • 3
    Does `data[1,1:10]` look okay? Does `data[10,1:10]` look okay? Does `data[2301,1:10]` look okay? Repeat until first problem row found, find corresponding line in CSV file, figure out problem. – Spacedman Jul 08 '14 at 10:42
  • 2
    You should avoid blaming the function: if your title read "Trouble reading *MY* file with "read.csv" it'd be more accurate. – Carl Witthoft Jul 08 '14 at 11:15
  • Just a wild guess: in one column you have character strings with apostrophes or quotes. – plannapus Jul 08 '14 at 11:24
  • Thank you plannapus, it seems you found the nature of the problem. Thank you all for your help. – Clément F Jul 08 '14 at 16:50

0 Answers0