4 ms·
The biggest "gotcha" for learning R as a programmer is that R interprets character vectors of data frames as factor vectors by default, which will usually break
by minimaxir 12y ago
The biggest "gotcha" for learning R as a programmer is that R interprets character vectors of data frames as factor vectors by default, which will usually break something in your code.
If you're learning R, learn to use dplyr for data manipulation and ggplot2 for plotting. Both will save you a lot of time.
- claytonjy 12y agoAgreed, this was a huge pain point for me early on; I started setting the necessary option to force stringsAsCharacters rather than specifying that arg in read.csv everytime. Edit: I had mentioned data.table as being better than dplyr for performance reasons, but it has a unique learning curve and isn't really good for beginners
- ggrothendieck 12y agoTo be clear this applies to data frames, not the language as a whole. If you are creating a data frame by reading in data then `read.table(...whatever..., as.is = TRUE)` will read in character columns as character strings and not as factors. `data.frame()` has a `stringsAsFactors` argument which can be set to `FALSE` and one can change the global default with: `options(stringsAsFactors = FALSE)` .
- craigching 12y ago> If you're learning R, learn to use dplyr for data manipulation I had been learning data.table, but I really like dplyr's % operator and the compositional functions better. I think I'm going to make the move to dplyr.
- hadley 12y agoFWIW The performance difference is insignificant unless your working with >>10 of millions of rows.
- craigching 12y agoI thought dplyr (note, not plyr, dplyr is the next iteration of plyr implemented mostly in native code) was pretty much as fast as data.table.
- arun_sriniv 12y agoWe're in the process of adding more detailed vignettes illustrating more clearly the philosophy behind data.table's `i, j, by`. Should make things lot easier for beginners - https://github.com/Rdatatable/data.table/issues/944 https://github.com/Rdatatable/data.table/issues/944 data.table is not just fast, but is also more memory efficient - we want to highlight in the vignettes as well. And timings are quite relevant even on 10 million rows: https://gist.github.com/arunsrinivasan/db6e1ce05227f120a2c9 https://gist.github.com/arunsrinivasan/db6e1ce05227f120a2c9 For much larger data, check the project page: https://github.com/Rdatatable/data.table/wiki/Benchmarks-%3A-Grouping https://github.com/Rdatatable/data.table/wiki/Benchmarks-%3A...
- bglazer 12y agoI've heard all good things about ggplot2 and I sincerely believe they're true. However, my only experience has been trying to plot 2 overlapping timeseries that didn't have the same length to the same plot area. I found that to be nearly impossible in ggplot2 and almost trivial with the builtin plot and lines commands in R. Like a lot of good tools, it seems that ggplot2 makes the common case extremely easy, but it can be a struggle to make it work in uncommon situations.
- alaiacano 12y ago> s1 <- data.frame(x=c(1,2,3), y=c(7,8,7),series=rep("s1", 3)) > s2 <- data.frame(x=c(1,2,3,4), y=c(6,7,6,7), series=rep("s2", 4)) > s <- rbind(s1, s2) > qplot(x, y, data=s, geom=c('point', 'line'), color=series)
- bglazer 12y agoThanks. I guess this speaks more to my lack of lack of experience.