0
votes

I have csv file which has 6 column, out of which first four columns are character & other two are numeric.

But while importing this csv file all column get class get converted in factor. I want my first columns in character & other two numeric how to do that?

I tried colClasses, transform but nothing is working

2
Add some code, so someone can help you.GeralexGR
Try readr package in R. Rstudio also has a GUI for the package where you can specify the class of each column of the CSV you are trying to import.TheRimalaya

2 Answers

0
votes

Try read.table(file = "file.csv", stringsAsFactors = FALSE) (also works with read.csv / read.csv2)

0
votes
setClass("num.with.commas")
setAs("character", "num.with.commas", function(from) as.numeric(gsub(",","",from,fixed=TRUE)))

cl <- c(rep("character", 5), rep("num.with.commas",2))
data <- read.csv("Test.csv", colClasses = cl)