sql - 循环遍历不规则的数字列表以将行附加到汇总表
问题描述
我正在尝试编写将循环遍历与多个传感器相关的整数列表的代码,以提供汇总统计信息(在此阶段只是 cor())。
# GOOD TO HERE
corr_table <-data.frame(ID = integer()
, HxT = double())
for(j in gt_thrsh_key){ #this is currently set to 2:5 for testing - its a list of sensors I want to summarise
# extract humidity and time vectors
x <- sqldf(sprintf("SELECT humidity FROM data_agg_2 WHERE ID = %s",j))
y <- sqldf(sprintf("SELECT time_elapsed FROM data_agg_2 WHERE ID = %s",j))
# format into row
new_row <- data.frame(ID = c(j), HxT = c(cor(x,y))) #insert new variables into row
# append to dataframe
corr_table <- rbind(corr_table, new_row)
print(sprintf("Sensor %s has been summarised.",j)) # check 1
print(cor(x,y)) # check 2
}
print(corr_table)
assign("data_agg_2", data_agg_2, envir = .GlobalEnv)
我得到输出:
[1] "Sensor 2 has been summarised." "Sensor 3 has been summarised." "Sensor 4 has been summarised." "Sensor 5 has been summarised."
humidity -0.08950285
ID HxT
1 2 -0.08950285 #INCORRECT
2 3 -0.08950285 #INCORRECT
3 4 -0.08950285 #INCORRECT
4 5 -0.08950285 #correct
这只是循环 (id = 5) 最终迭代的正确测量,所以我必须以某种方式覆盖以前的条目。有谁知道为什么会这样?或者你能推荐一种更好的方法来执行这个循环吗?
谢谢!!
编辑:检查 2 通过循环打印 x 和 y 的 cor() 确认只有循环的最终运行正在计算一个值。有没有人见过这个?
解决方案
这是一个基本 R 解决方案,用于lapply()
生成相关性并将它们写入list()
. 该列表将转换为带有 的数据框do.call(rbind,...)
。
# simulate some data
set.seed(19041798) # ensure consistency across multiple runs
ID <- rep(1:10,20)
humidity <- rnorm(200,mean = 30,sd = 15)
elapsed_time <- rpois(200,2.5)
data <- data.frame(ID,humidity, elapsed_time)
uniqueIDs <- unique(data$ID)
correlationList <- lapply(uniqueIDs,function(x){
y <- subset(data,ID == x)
HxT <- cor(y$humidity,y$elapsed_time)
# return as data frame
data.frame(ID = x,HxT = HxT)
})
correlations <- do.call(rbind,correlationList)
...和输出:
> correlations
ID HxT
1 1 -0.1805885
2 2 -0.3166290
3 3 0.1749233
4 4 -0.2517737
5 5 0.1428092
6 6 0.3112812
7 7 -0.3180825
8 8 0.3774637
9 9 -0.3790178
10 10 -0.3070866
>
sqldf() 版本
我们可以从原始帖子中重构代码,以便通过单个 SQL 查询提取所需的所有数据,并在 R 中执行所有后续处理。
首先,我们模拟 60,000 行数据。
set.seed(19041798) # ensure consistency across multiple runs
ID <- rep(1:30,2000)
humidity <- rnorm(60000,mean = 30,sd = 15)
elapsed_time <- rpois(60000,2.5)
data <- data.frame(ID,humidity, elapsed_time)
接下来,我们从具有 的数据中提取前 5 个传感器的数据sqldf()
,以及 uniqueID 的向量。
library(sqldf)
# select ID <= 5
sqlStmt <- "select ID, humidity,elapsed_time from data where ID <= 5"
dataSubset <- sqldf(sqlStmt)
sqlStmt <- "select distinct ID from data where ID <= 5"
uniqueIDs <- sqldf(sqlStmt)[[1]]
此时,dataSubset
数据框有 10,000 个观测值。我们使用lapply()
uniqueIDs 的向量来生成相关性ID
,计算complete.cases()
每个相关性中包含的相关性,并将结果写入数据帧列表。
correlationList <- lapply(uniqueIDs,function(x){
y <- subset(dataSubset,ID == x)
count <- sum(complete.cases(y)) # number of obs included in cor()
HxT <- cor(y$humidity,y$elapsed_time)
# return as data frame
data.frame(ID = x,count = count,HxT = HxT)
})
最后,一个do.call(rbind,...)
和一个打印,我们有我们的相关性列表,包括用于计算相关性的行数。
correlations <- do.call(rbind,correlationList)
correlations
...和输出:
> correlations
ID count HxT
1 1 2000 0.015640244
2 2 2000 0.017143573
3 3 2000 -0.011283180
4 4 2000 0.052482666
5 5 2000 0.002083603
>
推荐阅读
- c - 类型特定的功能
- flutter - 如何在 CircleAvatar 周围放置图标
- python - 在多索引数据框中定位特定数据
- react-native - 任务:app:bundleReleaseJsAndAssets 在捆绑包中失败
- python - 可以创建自定义语言环境,例如 fr_EN?
- python-3.x - 缺少属性时具有类属性的 BeautifulSoup find_all
- r - 防止来自 RStudio 的持久数据库连接
- node.js - 在节点js中使用sharp进行图像压缩
- html - Bootstrap 3导航栏不折叠
- python - 重新索引熊猫数据框的新日期范围