I’m trying to learn R, but I’m stuck on something that seems simple. I know SQL, and the easiest way for me to communicate my question is with that language. Can someone help me with a translation from SQL to R?
I’ve figured out that this:
SELECT col1, sum(col2) FROM table1 GROUP BY col1
translates into this:
aggregate(x=table1$col2, by=list(table1$col1), FUN=sum)
And I’ve figured out that this:
SELECT col1, col2 FROM table1 GROUP BY col1, col2
translates into this:
unique(table1[,c("col1","col2")])
But what is the translation for this?
SELECT col1 FROM table1 GROUP BY col1
For some reason, the “unique” function seems to switch to a different return type when working on only one column, so it doesn’t work as I would expect.
-TC
I’m guessing that you are referring to the fact that calling
uniqueon a vector will return a vector, rather than a data frame. Here are a couple of examples that may help: