首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从SQLite数据库中读取多个表的For循环

从SQLite数据库中读取多个表的For循环
EN

Stack Overflow用户
提问于 2021-04-10 10:03:52
回答 1查看 84关注 0票数 0

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

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

代码语言:javascript
运行
复制
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()
}

任何帮助都将不胜感激!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-10 10:20:42

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

代码语言:javascript
运行
复制
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)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67030172

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档