cherrypi / Science-Fair_2019

Vernal Pond graphing and data, as well as data analysis.
1 stars 0 forks source link

Dealing with dates #9

Closed VCF closed 5 years ago

VCF commented 5 years ago

read.table will not automatically handle dates for you, you're going to need to do that on your own. Fortunately there is a function to help us:

Pond_Data$Date # character
as.Date( Pond_Data$Date ) # The date format you've chosen will be auto-recognized
plot(as.Date( Pond_Data$Date ), Pond_Data$TemperatureMax) # Yay! First plot

Now you should figure out how to get your converted date "back into" your data.frame (Pond_Data). See if you can work out how to do that; that is, update the $Date column so it's a proper R date instead of character.

cherrypi commented 5 years ago

Would this work?

Pond_Data[, $Date := as.Date( Pond_Data$Date )]

VCF commented 5 years ago

You should try it? Don't be afraid to try things out. It's possible to exhaust all your computer's memory with malformed R code, but otherwise you really can't harm anything by "noodling around".

I think that might be specialized notation for data.table objects, which are specialized data storage structures.

Also, once you find something that works, if you did so with the help of an online resource, be sure to acknowledge it in your code with a comment. If one of the StackOverflow sites helped you, the answers have compact URLs under the "Share" links, so you could have code like this:

## Sort the data.table
##   https://stackoverflow.com/a/12354022
x <- x[order(-rank(weight), height)]
cherrypi commented 5 years ago

I've tried multiple approaches and this seems the closest, I have tried many options but I can't seem to puzzle out his error though when I input Pond_Data[as.Date( Pond_Data$Date ), Pond_Data$Date]:

Error in[.data.frame(Pond_Data, as.Date(Pond_Data$Date), Pond_Data$Date) : undefined columns selected

cherrypi commented 5 years ago

Same when I do this:

Pond_Data[as.Date( Pond_Data$Date )]

cherrypi commented 5 years ago

This seems to be similar: https://stackoverflow.com/questions/19205806/undefined-columns-selected-when-subsetting-data-frame

VCF commented 5 years ago

You're exploring something that I think is generally called functional programming. But there's a much simpler paradigm where the "left side" is updated with "output" from the "right side".

Here's an example with a different kind of data structure called a list. I set up the list initally, then decide to change it:

myData <- list(weight=7, cost=2.32, colors=c("blue", "red"))
myData # Just shows you the list
myData$colors # Shows you specifically the "colors" part of it
myData$colors <- c("green", "waffle") # Updates the colors part
myData # Again, shows the whole object

See if this helps you with what you need to do.

VCF commented 5 years ago

Good! Commit 6915012 is now converting the date strings into true Date R objects. This will make plotting and analysis a lot easier for you.