0

Is R having ageing problems? The frequency of problems with my packages loading etc has become so bad that I had to just do a clean re-install recently and only re-load packages as I need them. My latest issue is with read.csv - This was my preferred method for importing data to R for years but suddenly it no longer works. It turns any numeric data into a factor and the only solution seems to be:

dat = read.csv2(file.choose(), header = T, stringsAsFactors=F)
dat$LAI1 = as.numeric(dat$LAI1)
dat$LAI2 = as.numeric(dat$LAI2)
dat$LAI3 = as.numeric(dat$LAI3)

As the number of columns with numeric data increases, this becomes a very onerous task before I can even start analysing my data. Dont get me started on importing time and date data! Its becoming a nightmare. Any suggestions?

user3390486
  • 149
  • 2
  • 8
  • 1
    What does your raw data look like? `read.csv2` uses comma as the decimal and semi-colon as delimiter. Is this what you expected? – mickey Nov 09 '18 at 16:45
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Nov 09 '18 at 16:49

1 Answers1

0

You can use readr::read_csv and col_types

dat <- read_csv(file, col_types = list(LAI1 = col_number(),
                                     LAI2 = col_number(),
                                     LAI3 = col_number())
Henry Cyranka
  • 2,970
  • 1
  • 16
  • 21