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

血红的双手。 提交于 2019-12-02 18:46:04

问题


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 :^(


回答1:


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.



来源:https://stackoverflow.com/questions/36844846/select-data-from-mysql-with-for-loop-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!