Alessandro Guagnelli
Alessandro Guagnelli

Reputation: 33

Import csv column as integer when between quotation marks

I'm (very) new to R and I'm trying to learn by myself, but I can't find an answer for this, or at least, not a optimal one!

I have a CSV containing lines like this: "123112300500","A","395"

As you can see, the first and the last column are integer, but they're inside quotation marks. I don't want to preprocess my input (since I think that R can handle this), and I tried to import the csv like this:

set <- read.csv("set.csv", header = TRUE, sep=",", colClasses=c("integer", "character", "integer")

but it won't work since it doesn't expect the quotation marks!

My solution is for the moment doing set$V1 <- as.integer(set$V1) but it introduces a lot of overhead (I've like 2 millions rows and different columns with integer or numeric values).

How can I use read.csv in order to import values with integer (or float, or everything else) between marks?

Thanks to anyone in advance :)

Upvotes: 1

Views: 362

Answers (1)

cory
cory

Reputation: 6659

Are you sure all of your values are numeric in that column? type.convert seems to handle quoted numbers just fine...

a <- '"a", "b", "1", "2"
      "c", "d", "3", "4"'
df <- read.csv(text=a, stringsAsFactors=F, header=F)
> str(df)
'data.frame':   2 obs. of  4 variables:
 $ V1: chr  "a" "      c"
 $ V2: chr  " b" " d"
 $ V3: int  1 3
 $ V4: int  2 4

Upvotes: 2

Related Questions