rxImport, colClasses and RxTextData

869 Views Asked by At

I am trying to import a csv file with Revolution Analytics.

My code is the following:

rxImport(inData = mycsv, outFile =myXdf,type="text", colClasses=c('character','character','character','character',
'character','character' ,'character','logical','logical','logical','logical', 'logical','logical','logical', 'logical','logical','logical' ,'numeric','numeric', 'numeric')) 

I took care to delete myXdf before to launch the code and used scan on the first 1000 elements to define the vector colClasses.

It gives me the following error:

Error in validObject(.Object) : 
  invalid class “RxTextData” object: 'colClasses' must be a named character vector

Has somebody already met this error?

1

There are 1 best solutions below

0
On

To specify the column classes for a text data object, you need to add the column names as a named vector.

Try something like this:

# Read first few lines of csv to determine column names
x <- read.csv(mycsv, nrow=5)

# Define desired column classes
colClasses=c('character','character','character','character', 'character','character' ,'character','logical','logical', 'logical','logical', 'logical','logical','logical', 'logical','logical','logical' ,'numeric','numeric', 'numeric')

# Assign csv column names
names(colClasses) <- names(x)

# Define input object
input <- RxTextData(mycsv, colClasses = colClasses)

# Import the data
rxImport(input, myxdf)