在 R 中使用 sqldf 连接数据
Joining data with sqldf in R
我有一段代码:
sql_iv <- "select year, month, day,
count(HR)
from y2
group by year, month, day
order by year, month, day"
y3=sqldf(sql_iv)
计算一天内进行了多少次测量(数量每天都在变化):
Year Month Day count(HR)
1 2018 4 7 88
2 2018 4 8 327
3 2018 4 9 318
4 2018 4 10 274
5 2018 4 11 345
6 2018 4 12 275
.
.
.
189 2018 10 12 167
现在我需要获取这些计算值并将它们与我的数据相结合,该数据的每个测量值都在不同的行中(即 4 月 4 日进行的所有测量值在最后一列中的值必须为 88)。谁能帮我解决这个问题?
前 10 个测量值的数据结构(共 48650 个):
structure(list(Date = structure(c(1523119800, 1523119920, 1523119980,
1523120280, 1523120340, 1523120400, 1523120460, 1523120520, 1523120580,
1523120640), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
HR = c("97.0", "98.0", "95.0", "93.0", "94.0", "94.0", "92.0",
"96.0", "89.0", "90.0"), Year = c(2018, 2018, 2018, 2018,
2018, 2018, 2018, 2018, 2018, 2018), Month = c(4, 4, 4, 4,
4, 4, 4, 4, 4, 4), Day = c(7, 7, 7, 7, 7, 7, 7, 7, 7, 7),
Hour = c(16, 16, 16, 16, 16, 17, 17, 17, 17, 17), Minute = c(50,
52, 53, 58, 59, 0, 1, 2, 3, 4)), row.names = c(NA, -10L), class = c("tbl_df",
"tbl", "data.frame"))
你在找这个吗?
library(dplyr)
mydata %>%
as_tibble() %>%
left_join(sqldf %>% as_tibble, by = c("Year", "Month", "Day"))
我有一段代码:
sql_iv <- "select year, month, day,
count(HR)
from y2
group by year, month, day
order by year, month, day"
y3=sqldf(sql_iv)
计算一天内进行了多少次测量(数量每天都在变化):
Year Month Day count(HR)
1 2018 4 7 88
2 2018 4 8 327
3 2018 4 9 318
4 2018 4 10 274
5 2018 4 11 345
6 2018 4 12 275
.
.
.
189 2018 10 12 167
现在我需要获取这些计算值并将它们与我的数据相结合,该数据的每个测量值都在不同的行中(即 4 月 4 日进行的所有测量值在最后一列中的值必须为 88)。谁能帮我解决这个问题?
前 10 个测量值的数据结构(共 48650 个):
structure(list(Date = structure(c(1523119800, 1523119920, 1523119980,
1523120280, 1523120340, 1523120400, 1523120460, 1523120520, 1523120580,
1523120640), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
HR = c("97.0", "98.0", "95.0", "93.0", "94.0", "94.0", "92.0",
"96.0", "89.0", "90.0"), Year = c(2018, 2018, 2018, 2018,
2018, 2018, 2018, 2018, 2018, 2018), Month = c(4, 4, 4, 4,
4, 4, 4, 4, 4, 4), Day = c(7, 7, 7, 7, 7, 7, 7, 7, 7, 7),
Hour = c(16, 16, 16, 16, 16, 17, 17, 17, 17, 17), Minute = c(50,
52, 53, 58, 59, 0, 1, 2, 3, 4)), row.names = c(NA, -10L), class = c("tbl_df",
"tbl", "data.frame"))
你在找这个吗?
library(dplyr)
mydata %>%
as_tibble() %>%
left_join(sqldf %>% as_tibble, by = c("Year", "Month", "Day"))