Question

I select some values from a Database in R with RODBC like

library(RODBC)
dbhandle <- odbcDriverConnect('driver={SQL Server};server=mydatabase, ...')
res <- sqlQuery("select id, class, param1, param2 from table1 ..."

For Analysis of the Data I need to select a subset of the data. I got the column class which is a varchar and defines some subclasses like set1 or set2.

For example, I need summary() for both sets, and then for each set. I would say that this is done by:

summary(res) # works fine
summary(res[res["class"] == 'set1']) # does not work
summary(res[res["class"] == 'set2']) # does not work

Because I get this instead:

Length     Class      Mode 
    10788 character character 

After filtering I have the data as a long list and not as matrix. What is wrong there?

Was it helpful?

Solution 2

Try this:

summary(res[res[,"class"] == "set1",])

Update:

res[row,column] - genrally 1st value is row index, 2nd value is column index, so:

res[,"class"] - select "class" column from res.

res[,"class"] == "set1" - compare "class" column values with string "set1", this will give TRUE, FALSE values.

res[res[,"class"] == "set1",] - TRUE, FALSE values define which rows to return.

OTHER TIPS

zx8754 answer shows you what wrong in your code. Another way of getting it done is to use subset function:

summary(subset(res, class == 'set2'))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top