user1412716
user1412716

Reputation: 109

Error reading from csv file in R

I have a csv file like this one:

time,value,duration,time
2011-02-11 03:00:00,3663,3663,0
2011-02-12 03:00:00,3747,3747,0
2011-02-13 03:00:00,467,467,0

This is my code:

test1 <- read.csv("C:(here goes the path)",header=T,sep="",quote="", row.names = NULL) [,-1] test1[2,2]

The error it says is:

Error in `[.default`(test1, 2, 2) : incorrect number of dimensions

I think the fact that there's a space after the date is messing it up but I don't know how to fix it. Anyone have any suggestions?

Upvotes: 1

Views: 11851

Answers (2)

betabandido
betabandido

Reputation: 19704

This seems to work for me:

test1 <- read.csv(path)[, -1]

That command just uses the default parameters for read.csv:

read.csv(file, header = TRUE, sep = ",", quote="\"", dec=".",
         fill = TRUE, comment.char="", ...)

So, this is the result that I get:

  value duration time.1
1  3663     3663      0
2  3747     3747      0
3   467      467      0

which seems to be what you want, right?

Then test[2, 2] is equivalent to test$duration[2] and equal to 3747.

Upvotes: 3

Justin
Justin

Reputation: 43265

If your file is really a csv, you need to use sep=',' which is the default for read.csv. If you use sep='' that means you're reading each line as a single value, so there is no second column.

you can also use str(test1) to see the structure of your data once you've read it.

test1 <- read.csv(path, header=TRUE)

That should give you what you want.

Upvotes: 3

Related Questions