Wednesday, 4 September 2013

How do I read a .csv into R as a dataframe, and not a list?

How do I read a .csv into R as a dataframe, and not a list?

I have a large (~500MB) csv file with 6 columns, and I want to read it
into R as a dataframe. The csv file looks like (first two rows of data):
Date,Field,Category,Entity,Other Entity,Other Entity Type,Hits
d2007.07,Electronics,Consumer Electronics,Home Router,Home
Router,Technology,4
d2007.07,Computing,Graphics Hardware,Display Adapter,NVIDIA,Company,4
I expected the data to be read into a dataframe, "data", using just
read.csv(). However, read.csv(my_file.csv) reads it as a list.
data.frame(read.csv(my_file.csv)) also reads it as a list. Here's how it
looks:
data<-data.frame(read.csv("my_file.csv",header=TRUE,stringsAsFactors=FALSE))
typeof(data)
[1] "list"
I then try to convert it to a data frame using data.frame():
df<-data.frame(data)
typeof(df)
[1] "list"
And again:
library (plyr)
df <- ldply (data, data.frame)
R(1468) malloc: *** mmap(size=198414336) failed (error code=12)
*** error: can't allocate region
How do I read it in as a data frame? Or how do I read in as a list and
then convert it into a data frame?

No comments:

Post a Comment