[R] R equivalent for SQL query

David Winsemius dwinsemius at comcast.net
Tue Apr 3 22:30:00 CEST 2012


On Apr 3, 2012, at 3:26 PM, Steven Raemaekers wrote:

> I'm sorry, but I think that's just plain rude. This is exactly the  
> reason why people do not like to ask for help on mailing lists and  
> internet forums.

The usual expectation on this list is for a questioner to demonstrate  
what was tried after advice is offered.  To this point you have been  
given two earlier answers and do not seem to acknowledge those  
efforts. Admittedly both of those solutions did require that you  
install and load a non-base package to get the resutls you wanted.   
However,  did you show any further effort.

>
> The help file is full of valuable information, if you know exactly  
> where to look. For someone who doesn't know this, it is rather more  
> complicated.

I would have expected the 'table' function to have been demonstrated  
in "Introduction to R".

> There are several options in the help file of table, and I do not  
> know exactly how to apply this function or which options to choose.
>

 > dput(dat)
structure(list(A = c(1L, 1L, 1L, 1L, 1L, 1L), B = c(1L, 1L, 1L,
2L, 2L, 3L), C = 3:8), .Names = c("A", "B", "C"), class =  
"data.frame", row.names = c(NA,
-6L))


 > table(A=dat$A, B=dat$B)
    B
A   1 2 3
   1 3 2 1

Now I happen to know (after reading the help pages and following links  
and running examples) that 'as.data.frame' is a useful way of coercing  
a contingency table to a dataframe, and that function _is_ mentioned  
in the Value section on the ?table help page ....  which is actually  
the function called when the following code is executed:

You are expected to read
as.data.frame(table(A=dat$A, B=dat$B))
   A B Freq
1 1 1    3
2 1 2    2
3 1 3    1

?ftable is also a useful function (and it was listed on ?table)

-- 
David.

> So, again, can anyone help me to translate this query to R? Thanks!
>
> Regards,
>
> Steven
>
> On 3 apr. 2012, at 21:07, Bert Gunter wrote:
>
>> If reading the help file for the function that does what I think you
>> want is not helpful enough, you'll have to seek elsewhere. I expect
>> some minimal learning effort.
>>
>> (?table means type this at the prompt in the GUI).
>>
>> -- Bert
>>
>> On Tue, Apr 3, 2012 at 12:04 PM, Steven Raemaekers <s.raemaekers at sig.eu 
>> > wrote:
>>> Thanks. Excuse me for saying this, but that is not very helpful.  
>>> Can you provide **one**, since that would be enough…:P
>>>
>>> On 3 apr. 2012, at 21:01, Bert Gunter wrote:
>>>
>>>> ?table
>>>>
>>>> There are **many** other ways.
>>>>
>>>> -- Bert
>>>>
>>>> On Tue, Apr 3, 2012 at 11:26 AM, Steven Raemaekers <s.raemaekers at sig.eu 
>>>> > wrote:
>>>>> Hi,
>>>>>
>>>>> I have a query which I would like to translate into R, but I do  
>>>>> not know how to do it in an easy way.
>>>>> Assume a data frame has columns A, B and C:
>>>>>
>>>>> A       B       C
>>>>> 1       1       3
>>>>> 1       1       4
>>>>> 1       1       5
>>>>> 1       2       6
>>>>> 1       2       7
>>>>> 1       3       8
>>>>>
>>>>> The query is as follows:
>>>>>
>>>>> select A, B, count(*)
>>>>> from data.frame
>>>>> group by A, B
>>>>> order by count(*) desc
>>>>>
>>>>> How do I translate this into R statements in such way that the  
>>>>> result is a data frame structured as follows:
>>>>>
>>>>> A       B       count(*)
>>>>> 1       1       3
>>>>> 1       2       2
>>>>> 1       3       1
>>>>>
>>>>> Thanks,
>>>>>
>>>>> Steven
>>>>> _____________

>

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list