Note that there are some explanatory texts on larger screens.

plurals
  1. POConvert factor to time in R
    primarykey
    data
    text
    <p>I have a small problem which I cannot seem to figure out. I have searched several topics and websites to find a solution, and I have tried numerous different things but nothing seems to work..<br> I have a dataset with two columns, one for date and one for time:</p> <p>$ StartDate : Date, format: "2013-09-01" "2013-09-01" "2013-09-01" "2013-09-01" ...<br> $ StartTime : Factor w/ 43 levels "00:02:43","00:07:05",..:</p> <p>As you can see, I already converted the column "StartDate to "Date" and I would like to convert the column StartTime to "POSIXct".. I had a big dataset which I splitted, in this big dataset I had the variables date and time set right. I used the following loop to split the file:</p> <pre><code>for (Cow in unique(df$CowID)){ #loop through each level of CowID df &lt;- RawData[df$CowID == Cow,] #extract all rows where CowID equals Cow write.csv(df, paste("CowID_",Cow,".csv",sep = ""),row.names = F)} #write new dataframe to .csv </code></pre> <p>Should I have used an extra piece of code to keep the variables? After using this loop I got 68 different .csv files which I then separately read into R by using the next code:</p> <pre><code>C292 &lt;- read.csv(file = "C:\\...\\CowID_12292.csv", header = T, skip = 0, colClasses = c("StartDate" = "Date", "Date" = "Date", "StartTime" = "character", "Time" = "character")) </code></pre> <p>Ofcourse, now these variables for time are "chr". I have no clue how to convert this.. Can anyone help me? I tried <strong>as.POSIXct()</strong> but that did not seem to work, I only NA in the column..</p> <p>Thanks in advance!</p> <p>ADD > dput(head(C292))</p> <blockquote> <p>structure(list(CowID = c(12292L, 12292L, 12292L, 12292L, 12292L, 12292L), StartDate = structure(c(15949, 15949, 15949, 15949, 15949, 15949), class = "Date"), StartTime = c("02:16:16", "02:16:16", "02:16:16", "02:16:16", "02:16:16", "02:16:16")</p> </blockquote> <p>Do I really need to add the date into the column time? Or is there also a way to leave it out, but still keep the variable POSIXct? I would like to split the file by date and then plot the days on a timeline of 24hours. I do not know if this is even possible, I am quite a noob in R. However, it is a very clever program so I guess it is possible. Just have to figure out a way to do it!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload