【问题标题】:For loop to read in multiple tables from SQLite databaseFor循环从SQLite数据库中读取多个表
【发布时间】:2023-03-26 08:15:01
【问题描述】:

我想创建一个从 SQLite 数据库读取多个表的 for 循环。我希望它读取前 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()
}

任何帮助将不胜感激!

【问题讨论】:

    标签: sql r sqlite for-loop rsqlite


    【解决方案1】:

    如果有 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)
    

    【讨论】:

      猜你喜欢
      • 2021-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-28
      • 2015-10-18
      • 1970-01-01
      相关资源
      最近更新 更多