-1

I am trying to figure out a problem I'm having with converting a date column from factor to posixct. I have a script which I have run several times with no issue. When I read my .csv file, it has a date column which comes as a factor. But I convert it into POSIXct using as.POSIXct() so I can create a plot and do some calculations.

Recently, I had to reinstall R. Now when I try to convert from factor to posixct, my dataframe length and size become zero. I have searched for a solution, but could not find a solution. Why would as.POSIXct() erase my dataframe?

   date     Julian   Hour   MINUTE   date1              v1

2004-04-25   116     18      0     2004-04-25 18:00      0.0000001

2004-04-25   116     18     30     2004-04-25 18:30      0.0000001

2004-04-25   116     19      0     2004-04-25 19:00      0.0000002

2004-04-25   116     19     30     2004-04-25 19:30      0.0000003

2004-04-25   116     21      0     2004-04-25 21:00      0.0000001
GordonShumway
  • 1,980
  • 13
  • 19
  • Try `read_csv()` instead of `read.csv()`. This will read in your strings as character variables rather than factors. – James Martherus Sep 12 '19 at 19:28
  • 1
    @MathNewbie can you share a sample of your data please? Even a 4x4 df would be great. – Gainz Sep 12 '19 at 19:30
  • Can you be more specific? What error did you get? – James Martherus Sep 12 '19 at 19:30
  • I am trying to do read_csv(), it did not work, i dont know if my version works with read_csv. – MathNewbie Sep 12 '19 at 19:31
  • 2
    paste a sample of your data from your R console. You can do something like head(dt) in the console and simply copy and paste it into code block in your original question! – Gainz Sep 12 '19 at 19:40
  • @JamesMartherus i did not get any error. Only thing, in the environment window of Rstudio, it is not longer showing as a data.frame. As i will have to do some major calculation, i wonder if this might cause a problem afterwards. – MathNewbie Sep 12 '19 at 19:40
  • 1
    Impossible to answer unless you post your code. – Bill O'Brien Sep 12 '19 at 19:45
  • @BillO'Brien well, this is the code i used, which works perfectly before. metHre <- read.csv(file="metHre_vpd.csv",sep=",") date_info <- with(metHre,paste(yr,mon,day,Hr,Minute)) metHre$date1 <- as.POSIXct(strptime(date_info,"%Y %m %d %H %M")) The problem is not with the code, i think its the ggplot version i am using or the posixct, as when i use the posixlt, the dataframe remains as a dataframe. – MathNewbie Sep 12 '19 at 19:56
  • 1
    @JamesMartherus thank you so much. I checked online, and i found that read_csv need the tidyverse library, which i load, then the read_csv() command works. I used the read_csv2() command as my data had semi-colon instead of comma and it imports the file perfectly, with the date column as Date format and my datetime column as posixct. And the dataframe remains as a dataframe in the environment window of my Rstudio. Thank you so much. – MathNewbie Sep 12 '19 at 20:10
  • @JamesMartherus I have another issue now. The read_csv2 reads my other numeric column as character. Its like a limitation using read_csv2 instead of read.csv. – MathNewbie Sep 12 '19 at 21:10
  • Post this as a new question. – James Martherus Sep 12 '19 at 21:54

1 Answers1

0

You may try to include stringsAsFactors = F in the read.csv() so that read.csv will read your strings as character instead of factors.

yzh1206
  • 28
  • 4