Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to shorten a series of colClasses

Tags:

r

At the moment I am reading in a data file like this:

setwd("N:/HH Scallop Growth Project/Ring data by cruise/")

growth <- read.csv("Growth.csv",sep=",",header=TRUE,
                    colClasses=c("character","character","character","numeric",
                                 "character","numeric","numeric","numeric",
                                 "numeric","numeric","numeric","numeric",
                                 "numeric","numeric","character","numeric",
                                 "character","numeric","numeric","numeric",
                                 "numeric","character","numeric","numeric",
                                 "numeric")) 

It works fine but it is a bit long/scruffy, is there a way of shortening/grouping the colClasses?

like image 461
helen.h Avatar asked May 29 '14 15:05

helen.h


1 Answers

Try creating a 25-vector whose entries are all "numeric" and then just replace the few that are not with "character". Also note that header=TRUE and sep="," are the default for read.csv so they can be omitted.

colClasses <- replace(rep("numeric", 25), c(1:3, 5, 15, 17, 22), "character")
growth <- read.csv("Growth.csv", colClasses = colClasses)
like image 99
G. Grothendieck Avatar answered Nov 15 '22 07:11

G. Grothendieck