sqldf

selecting every Nth column in using SQLDF or read.csv.sql

守給你的承諾、 提交于 2019-12-08 03:30:16
问题 I am rather new to using SQL statements, and am having a little trouble using them to select the desired columns from a large table and pulling them into R. I want to take a csv file and read selected columns into r, in particular, every 9th and 10th column. In R, something like: read.csv.sql("myfile.csv", sql(select * from file [EVERY 9th and 10th COLUMN]) My trawl of the internet suggests that selecting every nth row could be done with an SQL statement using MOD something like this (please

generate sequence (and starting over in case of a recurrence) and add new column with highest number per sequence, within group, in R

强颜欢笑 提交于 2019-12-08 00:52:00
问题 I am looking for a way to generate a sequence for a column with names of cities grouped by an ID. What is crucial is that when a name of a city is repeated (within the group) a new sequence has to start. A new sequence should also start in case of a new ID. EDIT: The question how to create the above mentioned sequence has been solved. To help select the row with the highest sequence number later on, I am looking for a way to add a new column to the data frame that shows for each record, per

how to use right/left to split a variable in sqldf, as in left(x,n)

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-07 21:22:05
问题 Is there a different function in R to use to get the same result as in SQL for left or right function? For instance, the following query in SQL would give the first 6 characters of a column: select left(x, 6) from table However, when I try this in sqldf like this: sqldf("select left(x,6) from table") I get the following error: Error in sqliteSendQuery(con, statement, bind.data) : error in statement: near "(": syntax error It gives me the x variable but not left(x,6). To clarify, the length of

sqldf can't find the data with error “no such table”

妖精的绣舞 提交于 2019-12-06 16:55:42
I've been using sqldf in my R-scripts until now when I got the following error: library(sqldf) data(mtcars) out <- sqldf("SELECT * FROM mtcars") > Error in rsqlite_send_query(conn@ptr, statement) : no such table: mtcars This hasn't been a problem before now, anyone know what's the issue? I had this problem with 0.4-10 from CRAN (Windows 10). > out <- sqldf("SELECT * FROM mtcars") Loading required package: tcltk Error in rsqlite_send_query(conn@ptr, statement) : no such table: mtcars Then I upgraded from GitHub, version 0.4-11 > devtools::install_github("ggrothendieck/sqldf") Downloading GitHub

how to use right/left to split a variable in sqldf, as in left(x,n)

℡╲_俬逩灬. 提交于 2019-12-06 12:04:49
Is there a different function in R to use to get the same result as in SQL for left or right function? For instance, the following query in SQL would give the first 6 characters of a column: select left(x, 6) from table However, when I try this in sqldf like this: sqldf("select left(x,6) from table") I get the following error: Error in sqliteSendQuery(con, statement, bind.data) : error in statement: near "(": syntax error It gives me the x variable but not left(x,6). To clarify, the length of x is more than 6. ah25 In sqldf , the function is named leftstr , not left . (answer by G.Grothendieck

generate sequence (and starting over in case of a recurrence) and add new column with highest number per sequence, within group, in R

家住魔仙堡 提交于 2019-12-06 06:11:59
I am looking for a way to generate a sequence for a column with names of cities grouped by an ID. What is crucial is that when a name of a city is repeated (within the group) a new sequence has to start. A new sequence should also start in case of a new ID. EDIT: The question how to create the above mentioned sequence has been solved. To help select the row with the highest sequence number later on, I am looking for a way to add a new column to the data frame that shows for each record, per sequence, per ID the highest number of each sequence. Here is an example of what I want to achieve,

call SQL function within R function

南笙酒味 提交于 2019-12-06 05:54:05
问题 I am wondering if it is possible to call an SQL function within an R function? Say for example that I have this dummy data and SQL function written in Postgres 9.3 CREATE TABLE tbl ( id VARCHAR(2) PRIMARY KEY ,name TEXT ,year_born NUMERIC ,nationality TEXT ); INSERT INTO tbl(id, name, year_born, nationality) VALUES ('A1','Bill',2001,'American') ,('B1','Anna',1997,'Swedish') ,('A2','Bill',1991,'American') ,('B2','Anna',2004,'Swedish') ,('B3','Anna',1989,'Swedish') ,('A3','Bill',1995,'American'

Make new feature using 2 tables

北城余情 提交于 2019-12-06 05:27:19
table1 <- data.frame(user_id=c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2), product_id = c(14, 24, 38, 40, 66, 2, 19, 30, 71, 98, 7, 16), first_order = c(1, 2, 1, 4, 5, 3, 2, 4, 2, 4, 2, 3), last_order = c(4, 7, 5, 8, 8, 3, 4, 7, 5, 9, 4, 5)) table2 <- data.frame(user_id=c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2), order_number=c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6), days_cumsum = c(0, 7, 15, 26, 34, 43, 53, 59, 66, 74, 82, 91, 5, 11, 17, 24, 29, 35)) I want to add new feature to table1 using table2. New feature is order interval of each product by each user. For

Convert an integer value to datetime in sqldf

烈酒焚心 提交于 2019-12-06 03:24:45
I am using sqldf library to return a data frame with distinct values and also only the max of the date column. The data frame looks like this +------+----------+--------+-----------------+ | NAME | val1 | val2 | DATE | +------+----------+--------+-----------------+ | A | 23.7228 | 0.5829 | 11/19/2014 8:17 | | A | 23.7228 | 0.5829 | 11/12/2014 8:16 | +------+----------+--------+-----------------+ When I try to run the below code to get the distinct values with max date df <- sqldf("SELECT DISTINCT NAME, val1, val2, MAX(DATE) FROM Table") I get this as the output. +------+----------+--------+---

sqldf : create table from data frame error: “no such table”. and two tables created instead of one

一曲冷凌霜 提交于 2019-12-06 02:28:09
问题 I've recently upgraded R, RSQLite, and sqldf (versions below). Normally: sqldf('create table foo as select * from bar', db = 'test.db') should create a table called 'foo' in an attached sqlite database, using data frame 'bar' if it exists to load the new table. Instead, I'm getting a 'no such table' error, and also when I look at the database there are both 'foo' and 'bar' tables created. Reproducible example: library(RSQLite) library(sqldf) mydb = 'test.db' ## remove file if it exists system