Your verification ID is: guDlT7MCuIOFFHSbB3jPFN5QLaQ Big Computing: Simple Problems in R cause big headaches....Always check your variable class

Monday, March 2, 2015

Simple Problems in R cause big headaches....Always check your variable class

Recently a frined of mine who is a newer user to R was having some problems getting his data to graph properly. He went around and around with it, but simply could not figure out what was going on. This is one of the tough things about R. It will usually run your code even if there is a little problem with what your are doing. I have been stuck for hours because of something I forgot to do. The most common reason I end up having a problem is that the data in not of the class I thought is was. So the code was doing everything it was supposed to do correctly, but I had not either changed the class of the data or verified that it was correct anywhere in my code. When you are developing I highly reccomend that you do that. It will save you a ton of time and stress.
I will show this issue because it does a great job of showing the problem.
First, let me read in the data.
data<-read.csv("/Users/kmettler/Downloads/Kirk.csv")
head(data)
##    Time
## 1 13:10
## 2 12:00
## 3 11:50
## 4 10:30
## 5  8:40
## 6  9:00
Now he wanted to plot these times.
plot(table(data$Time),main="December Incident Reports by Time of Day", xlab="Time of Day in Hours",ylab="Number of Reports",lwd=4,col=8)
plot of chunk unnamed-chunk-2
It runs. However, you will notice it does not look right. The problems is that time is not of the right class and needs to be converted to the right one
class(data[,1])
## [1] "factor"
data2<-as.data.frame(strptime(data[,1], format="%H:%M"))
hist(as.numeric(data2[,1]),main="December Incident Reports by Time of Day", xlab="Time of Day in Hours",ylab="Number of Reports")
plot of chunk unnamed-chunk-3
Now that look way better. Now all that has to be done is the analysis.

No comments:

Post a Comment