Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
For循环从SQLite数据库读入多个表_Sql_R_Sqlite_For Loop_Rsqlite - Fatal编程技术网

For循环从SQLite数据库读入多个表

For循环从SQLite数据库读入多个表,sql,r,sqlite,for-loop,rsqlite,Sql,R,Sqlite,For Loop,Rsqlite,我想创建一个for循环,从SQLite数据库中读取多个表。我希望它读取前300个表,但理想情况下,我希望它将300个随机表从我的数据库读入R 对于读入的每个表,我希望它仔细阅读编写的代码,在最后保存图形,然后用新表重新开始。如果可能的话,我希望所有的表都在同一个图上。我已经为一个表编写了代码,但我不确定如何从这里开始 for (i in 1:300){ # Reads the selected table in database ind1 <- dbReadTable(mydb, i)

我想创建一个for循环,从SQLite数据库中读取多个表。我希望它读取前300个表,但理想情况下,我希望它将300个随机表从我的数据库读入R

对于读入的每个表,我希望它仔细阅读编写的代码,在最后保存图形,然后用新表重新开始。如果可能的话,我希望所有的表都在同一个图上。我已经为一个表编写了代码,但我不确定如何从这里开始

for (i in 1:300){
# Reads the selected table in database
ind1 <- dbReadTable(mydb, i)

# Formats the SQL data to appropriate R data structure
cols <- c("Mortality", "AnimalID", "Species", "Sex", "CurrentCohort", 
          "BirthYear", "CaptureUnit","CaptureSubunit",
          "CaptureArea", "ProjectName")
ind[cols] <- lapply(ind[cols], factor)  ## as.factor() could also be used
ind$DateAndTime <- as.POSIXct(ind$DateAndTime, tz = "UTC",
                               origin = '1970-01-01')

# Converts the Longitude and Latitude to UTMs
ind <- convert_utm(ind1)

ind_steps <- ind %>% 
  # It's always a good idea to *double check* that your data are sorted
  # properly before using lag() or lead() to get the previous/next value.
  arrange(AnimalID, DateAndTime) %>% 
  # If we group_by() AnimalID, lead() will insert NAs in the proper
  # places when we get to the end of one individual's data and the beginning
  # of the next
  group_by(AnimalID) %>% 
  # Now rename our base columns to reflect that they are the step's start point
  rename(x1 = utm_x, 
         y1 = utm_y, 
         t1 = DateAndTime) %>% 
  # Attach the step's end point
  mutate(x2 = lead(x1),
         y2 = lead(y1),
         t2 = lead(t1)) %>% 
  # Calculate differences in space and time
  mutate(dx = x2 - x1,
         dy = y2 - y1,
         DateAndTime = as.numeric(difftime(t2, t1, units = "hours"))) %>% 
  # Calculate step length
  mutate(sl = sqrt(dx^2 + dy^2)) %>% 
  # Calculate absolute angle
  mutate(abs_angle = (pi/2 - atan2(dy, dx)) %% (2*pi)) %>% 
  # Calculate relative angle
  mutate(rel_diff = (abs_angle - lag(abs_angle)) %% (2*pi),
         rel_angle = ifelse(rel_diff > pi, rel_diff - 2*pi, rel_diff)) %>% 
  # Drop this uneccesary column
  select(-rel_diff) %>% 
  # Drop incomplete final step
  filter(!is.na(x2))

ind_steps <- ind_steps %>% 
  mutate(NSD = (x2 - x1[1])^2 + (y2 - y1[1])^2)

# Plot NSD
ind_steps %>% 
  ggplot(aes(x = t2, y = NSD)) +
  geom_line() +
  theme_bw()
}
for(1:300中的i){
#读取数据库中的选定表
ind1%
#计算步长
突变(sl=sqrt(dx^2+dy^2))%>%
#计算绝对角度
突变(abs_角=(pi/2-atan2(dy,dx))%%(2*pi))%%>%
#计算相对角
变异(rel_diff=(abs_角度-滞后(abs_角度))%%(2*pi),
rel_angle=ifelse(rel_diff>pi,rel_diff-2*pi,rel_diff))%>%
#删除此不成功的列
选择(-rel_diff)%>%
#放弃不完整的最后一步
过滤器(!is.na(x2))
ind_步数%
突变(NSD=(x2-x1[1])^2+(y2-y1[1])^2)
#绘图NSD
ind_步数%>%
ggplot(aes(x=t2,y=NSD))+
geom_线()+
主题_bw()
}

任何帮助都将不胜感激

如果有1000个表可以使用
sample
从中随机获得300个,请创建一个长度为300的列表来存储这些图,如果要将它们一起绘制,可以使用
cowplot::plot\u grid

random_tables <- sample(1000, 300, replace = TRUE)
plot_list <- vector('list', 300)

for (i in seq_along(random_tables)){
  # Reads the selected table in database
  ind1 <- dbReadTable(mydb, random_tables[i])
  
  #...Rest of the code
  #....
  #....
  # Plot NSD
  plot_list[[i]] <- ggplot(ind_steps, aes(x = t2, y = NSD)) + 
                    geom_line() + theme_bw()
}

cowplot::plot_grid(plotlist = plot_list, nrow = 30, ncol = 10)

random_tables如果有1000个表可以使用
sample
从中获取随机300,请创建一个长度为300的列表来存储绘图,如果要将它们一起绘图,可以使用
cowplot::plot_grid

random_tables <- sample(1000, 300, replace = TRUE)
plot_list <- vector('list', 300)

for (i in seq_along(random_tables)){
  # Reads the selected table in database
  ind1 <- dbReadTable(mydb, random_tables[i])
  
  #...Rest of the code
  #....
  #....
  # Plot NSD
  plot_list[[i]] <- ggplot(ind_steps, aes(x = t2, y = NSD)) + 
                    geom_line() + theme_bw()
}

cowplot::plot_grid(plotlist = plot_list, nrow = 30, ncol = 10)
random\u表