我得到这个错误,而我必须将行转置到列
library(sqldf)
sqldf("SELECT id,name,ROW_NUMBER() over (PARTITION BY id order by id) as row_no FROM dd")
rsqlite_send_query中的错误(conn @ptr,statement):near“(”:syntax 错误_____
我应该怎么做或者请给我一个替代方案,因为我想在shiny
应用中使用它
答案 0 :(得分:1)
假设我们有这个:
library(sqldf)
dd <- data.frame(id = c(1, 2, 3), name = c("A", "B", "C"), X = 1:9)
然后这三个都给出了相同的结果:
# 1. PostgreSQL - supports over/partition
library(RPostgreSQL)
sqldf("SELECT id, name, \"X\", ROW_NUMBER() over (PARTITION BY id order by id) as row_no
FROM dd order by id")
# 2. SQLite - does not support over/partition, use join instead
# Ensure RPostgreSQL is NOT loaded to force use of SQLite.
sqldf("select a.*, count(*) row_no
from dd a join dd b on a.id = b.id and b.rowid <= a.rowid
group by a.rowid
order by a.rowid")
# 3. Base R
transform(dd, row_no = ave(1:nrow(dd), id, FUN = seq_along))
答案 1 :(得分:0)
sqlite
不支持分区。
您可以查看this link