Thursday, June 8, 2017

Things I learned about R, Stats, DataScience or Machine Learning Today, 1496931039

R feels like an easy language to just run with. For a while I felt I groked it just about as well as I needed. But any time I get that confident, there is for sure something coming to prove me wrong. And this was no exception. Most days I can knock out code in R no problem, with very little interruption. Mostly I just import data via a csv or some other format and just start rocking. Well, today I had to do something basic I had never done before,  just add a row to a data frame. Should be simple enough, right? Well it wasn't, it was very strange.  I just wanted to add a vector with 31 one Integers, to a data frame. There has to be a simpler way to do this, but I turned the vector into a data frame and used rbind to add it. That wasn't the tricky part that was simple enough, but it kept adding NAs rather than my vector.  It turns out I had the wrong class for my vector, I needed factors. The finished code for adding the row is:

for(i in factorCols) { levLeng <- length(levels(nashHouse[,i])); fLevels[i] <- levLeng; }
fLevels.df <- data.frame(t(as.character(fLevels)), stringsAsFactors = T)
colnames(fLevels.df) <- resCols
rbind(results, fLevels.df)

I used a for loop in my code to build my vector, which I believe is generally frowned upon in R, I will work to create a more functional version moving forward. Also I made this:

pie(c(10,10,1,2), labels = c("Naps", "Cartoons","Research","Coding"), main = "How I solve Machine Learning, and Data Science Problems", col = c("darkslategray1","darkslategray2","darkslategray3","darkslategray4"))

Clearly I'm not a person to trust, on any of the subjects in the post title.

No comments: