我有一个循环,从每个表中的每个字段,从列表中的每个数据库中获取一些信息(因此3个嵌套用于循环)。因为我很难知道我在这个循环中的位置,而且我的互联网频繁出现,我决定为每个循环周期吐出一条快速线......这就是代码的样子:
for (i in 1:nrow(conns)){
## Connect to each db in the dataframe conns and get a list of the Tables
## FOR EACH TABLE
for (j in 1:length(Tables)){
## FOR EACH COLUMN (or field)
for (k in 1:length(Columns)){
## do stuff, and then:
cat(paste(i," of ",nrow(conns),"; ",round(j/length(Tables)*100,2),"%; ",k,
" of ",length(Columns),"; \n",sep=""))
}
}
close(channel)
}
这就是那只猫的输出结果:
14 of 14; 43.77%; 1 of 5;
14 of 14; 43.77%; 2 of 5;
14 of 14; 43.77%; 3 of 5;
14 of 14; 43.77%; 4 of 5;
14 of 14; 43.77%; 5 of 5;
14 of 14; 44.15%; 1 of 4;
14 of 14; 44.15%; 2 of 4;
14 of 14; 44.15%; 3 of 4;
14 of 14; 44.15%; 4 of 4;
14 of 14; 44.53%; 1 of 4;
14 of 14; 44.53%; 2 of 4;
14 of 14; 44.53%; 3 of 4;
14 of 14; 44.53%; 4 of 4;
>
然后循环结束而没有错误。但为什么它停在44.5%呢?所以我决定检查循环是否已手动更改,并获得以下输出:
> length(Tables)
[1] 265
> j
[1] 265
> cat(paste(i," of ",nrow(conns),"; ",round(j/length(Tables)*100,2),"%; ",k,
+ " of ",length(Columns),"; \n",sep=""))
14 of 14; 100%; 4 of 0;
那么为什么j
百分比停止输出为44.53%,而实际上它正确地为100%?
编辑:以下for循环结构的完整代码:
## FOR EACH PROJECT
for (i in 13:nrow(conns)){
d <- conns[i,1] %>% as.character
p <- conns[i,3] %>% as.character
u <- conns[i,2] %>% as.character
channel <- odbcConnect(d,u,p)
## Find out what tables are available
sqlTables(channel) %>% select(TABLE_NAME) -> Tables
Tables <- as.vector(Tables[,1])
## Throw out long uuid ones
Tables <- Tables[!(substr(Tables,9,9) == "-" & nchar(Tables) == 36)]
## FOR EACH TABLE
for (j in 1:length(Tables)){
## Check that Table name doesn't have questionmarks or starts with sys
if (!grepl("\\?|^sys",Tables[j])){
##GET COLUMNS
Columns <- as.data.frame(colnames(
sqlFetch(channel, Tables[j], rows_at_time = 5,max=1)))
## Check that there's at least 1 column
if (ncol(Columns)!=0){
Columns <- as.vector(Columns[,1])
## FOR EACH COLUMN
for (k in 1:length(Columns)){
if(grepl("\\?\\?\\?\\?|DoB",Columns[k])!=T){
db1[l,1] <- d
db1[l,2] <- Tables[j]
db1[l,3] <- Columns[k]
AC <- paste('"',as.character(Columns[k]),'"',sep="")
Q <- paste('SELECT COUNT(',AC,') AS Count1,
COUNT(DISTINCT (',AC,')) AS Count2 FROM "',Tables[j],'"',sep="")
Result <- sqlQuery(channel, Q, rows_at_time = 5)
db1[l,4] <- Result[1,1]
db1[l,5] <- Result[1,2]
cat(paste(i," of ",nrow(conns),"; ",round(j/length(Tables)*100,2),"%; ",k,
" of ",length(Columns),"; \n",sep=""))
l <- l + 1
}
}
}
}
}
close(channel)
}
答案 0 :(得分:3)
它是三个if语句中的一个,它阻止它在后续循环中到达print语句。
对于44.53%之后的案例,!grepl("\\?|^sys",Tables[j])
,ncol(Columns)!=0
或grepl("\\?\\?\\?\\?|DoB",Columns[k])!=T
都不是真的。