dPdms dPdms - 7 months ago 23
SQL Question

select data from mysql (with for loop(?)) in R

What I want is:

m1 <- dbGetQuery(mydb, "select out,in from table where value > 1")
m2 <- dbGetQuery(mydb, "select out,in from table where value > 1.1")
m3 <- dbGetQuery(mydb, "select out,in from table where value > 1.2")
m4 <- dbGetQuery(mydb, "select out,in from table where value > 1.3")
.
.
.
m101 <- dbGetQuery(mydb, "select out,in from table where value > 10")


then

n1 <- degree(graph.data.frame(m))
n2 <- degree(graph.data.frame(m2)
.
.
.


I would like to simplify these codes with apply function but I have no clue :^(

lmo lmo
Answer

Here is a for loop solution that saves the results in a list:

# get list
myList <- list()

for(i in seq(1, 10, 0.1)) {
  myList[[paste0("m",i)]]<- dbGetQuery(mydb, 
                               paste("select out,in from table where value >", i))
}

You can then call the objects out of you list:

n1 <- degree(graph.data.frame(myList[["m1"]]))

and as above, you can put these results in a list. Named lists are a great way to store and organize many objects.