Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Beautiful website.

I highly recommend supplementing a course like this (where you learn about the language's ecosystem) with the R Cookbook from O'Reilly. It's been a lifesaver for me, and helped me learn R over the course of a few months of needing it at a new job.

Now I find that I need to learn something else for data munging- R is terrible at data manipulation and querying.

The querying bit is solvable with the incredibly useful sqldf package from Google. The package allows you to use SQL syntax to query your data.frames (by creating, populating, querying and deleting a psql table in the background).

Example: I have a dataframe named dfrm with columns named "id" "height" "name"

If I want the heights of all people whose names start with D, I would need to use:

> dfrm$height[which(substr(dfrm$name,1,1)=='D')]

Terse, but painful. Compare to:

> sqldf("select height from dfrm where name = 'D%'")

Much easier!



I actually find base R excellent for data munging and manipulation, even without using additional packages. Here is a reproducible example that very easily accomplishes what you were trying to do (first two lines just set up a sample data frame)

  set.seed(123)
  dfrm <- data.frame(height=runif(20),
                     name=paste(sample(LETTERS[1:5],20,replace=TRUE),letters[1:20]))
  subset(dfrm, grepl('^D',name), sel=height)
Basic R functions like subset, transform, with(in), reshape, aggregate, (a,ma,ta,sa,va}pply, match, grep(l), by, split, table, etc. allow you to accomplish just about any data frame munging you might want. Add on the plyr, reshape2, data.table, xts/zoo packages and you're ready to tackle just about anything.

I'm not a big fan of sqldf because imo R is not supposed to act like SQL. Using sqldf in practice would require a lot of query string manipulation and takes away from the nice functional features of R.

Nevertheless, it is very easy to write incomprehensible R code. The best way to avoid this is to take one of the existing style guides (Google, Hadley Wickham's) and adopt it seriously.


One drawback with R is that in computations like this, several intermediate data structures with one dimension equal in length to nrow(dfrm) are allocated. Traversing an iterable of tuples is a simple way to think about it, is efficient, and ties in with other technologies e.g. relational databases. R is often people's first language (e.g. science graduates) and those people would be better off learning how to iterate over tuples than learning the obscure bestiary of data structure manipulators you point out.


In your R version, you don't need the call to `which()`, so you could do this instead:

    dfrm$height[substr(dfrm$name,1,1) == "D"]
And here's a much clearer way to do it:

    subset(dfrm, grepl("^D", name), select = height)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: