[英]R loop to query all tables within a database
I am new to R and have a database, DatabaseX
which contains multiple tables a,b,c,d,etc
. 我是R的新手,有一个数据库
DatabaseX
,其中包含多个表a,b,c,d,etc
。 I want to use R to count number of rows for a common attribute message_id
among all these tables and store it separately. 我想使用R来计算所有这些表之间的公共属性
message_id
的行数,并将其分别存储。
I can count message_id
for all tables using below code: 我可以使用以下代码为所有表计算
message_id
:
list<-dbListTables(con)
# print list of tables for testing
print(list)
for (i in 1:length(list)){
query <- paste("SELECT COUNT(message_id) FROM ",list[i], sep = "")
t <- dbGetQuery(con,query)
}
print(t)
This prints : 打印:
### COUNT(message_id)
## 1 21519
but I want to keep record of count(message_id)
for each individual table . 但是我想保留每个表的
count(message_id)
记录。 So for example table a = 200, b = 300, c = 500, and etc. 因此,例如表a = 200,b = 300,c = 500等。
any suggestions how to do this? 任何建议如何做到这一点?
As @kaiten65 suggested, one option would be to create a helper function which executes the COUNT
query. 正如@ kaiten65建议的那样,一种选择是创建一个执行
COUNT
查询的帮助函数。 Outside of your loop I have defined a numeric vector counts
which will store the number of records for each table in your database. 在循环之外,我定义了一个数字向量
counts
,该counts
将存储数据库中每个表的记录数。 You can then perform descriptive stats on the tables along with this vector of record counts. 然后,您可以在表上执行描述性统计信息以及此记录计数向量。
doCountQuery <- function(con, table) {
query <- paste("SELECT COUNT(message_id) FROM ", table, sep = "")
t <- dbGetQuery(con, query)
return(t)
}
list <- dbListTables(con)
counts <- numeric(0) # this will store the counts for all tables
for (i in 1:length(list)) {
count <- doCountQuery(con, list[i])
counts[i] <- count[[1]]
}
You can use functions to achieve what you need. 您可以使用函数来实现所需的功能。 For instance
例如
readDB <- function(db, i){
query <- paste("SELECT COUNT(message_id) FROM ",db, sep = "")
t <- dbGetQuery(con,query)
return(print(paste("Table ", i, " count:", t)
}
list<-dbListTables(con)
for (i in 1:length(list)){
readDB(list[i]);
}
This should print your list recursively but the actual code is in a nice editable function. 这应该递归打印列表,但是实际代码在一个不错的可编辑函数中。 Your output will be
您的输出将是
"Table 1 count: 2519"
"Table 2 count: ---- "
More information on R functions here: http://www.statmethods.net/management/userfunctions.html 有关R函数的更多信息,请访问: http : //www.statmethods.net/management/userfunctions.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.