Benjamin Telkamp
Benjamin Telkamp

Reputation: 1561

importing csv file in R

I'm having troubles with reading in a csv file. When I open the csv file in notepad it looks like this:

`USER` `USER_TYPE` `V1` `V2` `V3` `V4` `V5` `V6` `V7` `V8` `V9` `V10`
508 `Gemandateerde zonder werk` 8 4 1 2 `` `` `` `` 1 1
510 `Gemandateerde zonder werk` 8 4 2 `` `` `` `` `` 1 1
511 `Gemandateerde met werk` 8 3 1 2 `` `` `` `` 1 1
512 `Kind` 8 4 1 2 2 2 2 1 1 1
513 `Kind` 5 4 1 1 2 3 6 2 1 1
514 `Kind` 2 3 1 2 `` `` `` `` 1 2
515 `Gemandateerde zonder werk` 8 4 1 1 2 6 2 1 1 1
516 `Gemandateerde met werk` 8 2 1 1 2 4 1 2 1 2
517 `Kind` 8 2 1 2 `` `` `` `` 1 1
519 `Kind` 8 4 1 1 2 2 6 2 1 1
520 `Kind` 8 3 1 1 2 4 2 1 1 1

I used the following call (and tried different things):

df <- read.csv("file.csv", header = TRUE, sep = "")

R returns:

Error in read.table(file = file, header = header, sep = sep, quote = quote,  : 
more columns than column names

I don't see what i'm doing wrong, there are as many variable names in the first line as data points in the second line for the first case, can someone help me?

Upvotes: 1

Views: 83

Answers (1)

Spacedman
Spacedman

Reputation: 94277

You seem to have backquotes quoting your data. Try:

> read.csv("~/Downloads/file.csv",sep="",head=TRUE,quote="`")
   USER                 USER_TYPE V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
1   508 Gemandateerde zonder werk  8  4  1  2 NA NA NA NA  1   1
2   510 Gemandateerde zonder werk  8  4  2 NA NA NA NA NA  1   1
3   511    Gemandateerde met werk  8  3  1  2 NA NA NA NA  1   1
4   512                      Kind  8  4  1  2  2  2  2  1  1   1
5   513                      Kind  5  4  1  1  2  3  6  2  1   1
6   514                      Kind  2  3  1  2 NA NA NA NA  1   2

CSVs usually have single quote or double quote characters. Without telling R that ` is your quote character, it sees `Gemandateerde zonder werk` as three fields separated by spaces, and that explains the error message.

Upvotes: 3

Related Questions