我需要回答
no fac date count
1 fac1 2015-01 3
2 fac1 2016-01 5
3 fac1 2017-01 7
4 fac2 2015-01 9
5 fac2 2017-01 11
6 fac3 2016-01 13
7 fac4 2015-01 15
8 fac5 2017-01 17
制作
date fac1 fac2 fac3 fac4 fac5
2015-01 3 9 0 15 0
2016-01 5 0 13 0 0
2017-01 7 11 0 0 17
像这样。
我尝试使用左连接但发生此错误。
# first Data
Data0 <-mid[1:31,]
# Separate Data by factor
for (i in 1:72) {
assign(paste0("Data", i), subset(mid, midnames %in% mid_names[i]))
}
# Make first dataset
df3 <- Data0
df3$date[is.na(df3$date)] <- Sys.time()
df3[is.na(df3)]<-0
#leftjoin Datas by factor
for (i in 1:72) {
df3 <- sqldf(paste("SELECT *
FROM df3
LEFT OUTER JOIN Data",i," USING(year,month,date)",sep=""))
df3[is.na(df3)]<-0
}
错误:无法将NA传递给dbQuoteIdentifier()
另外:警告信息: 在field_types []&lt; - field_types [names(data)]中: 要替换的项目数量不是替换长度的倍数
答案 0 :(得分:0)
您的查询已关闭;你需要一个标准的数据透视查询:
SELECT
date,
MAX(CASE WHEN fac = 'fac1' THEN count END) AS fac1,
MAX(CASE WHEN fac = 'fac2' THEN count END) AS fac2,
MAX(CASE WHEN fac = 'fac3' THEN count END) AS fac3,
MAX(CASE WHEN fac = 'fac4' THEN count END) AS fac4,
MAX(CASE WHEN fac = 'fac5' THEN count END) AS fac5
FROM yourTable
GROUP BY
date
我假设您有一个数据框,其中包含您的第一个样本信息中的数据。