sqldf

Automatic transfer data from the sql to R

自作多情 提交于 2020-01-15 09:24:09
问题 I get the data from the sql server to perform regression analysis, and then the regression results i return back to another sql table. library("RODBC") library(sqldf) dbHandle <- odbcDriverConnect("driver={SQL Server};server=MYSERVER;database=MYBASE;trusted_connection=true") sql <- "select Dt ,CustomerName ,ItemRelation ,SaleCount ,DocumentNum ,DocumentYear ,IsPromo from dbo.mytable" df <- sqlQuery(dbHandle, sql) reg=lm(SaleCount~IsPromo,data=df) #save to sql table sqlSave(dbHandle, as.data

How to handle column names not supported by sqldf in R

喜你入骨 提交于 2020-01-15 04:57:11
问题 I've a data frame where some of the column names are of the format . format. For ex: Company.1 when i'm using that column in a sqldf function it throws an error data=sqldf(select Company.1 from test) Error in sqliteExecStatement(con, statement, bind.data) : RS-DBI driver: (error in statement: near ".1": syntax error) Any workaround so that i can use the column name as it is? 回答1: The dot has another meaning in SQL (e.g., separating table name from column name) and is replaced by an underscore

r read in multiple .dat-files

时间秒杀一切 提交于 2020-01-14 12:43:32
问题 Hi I am new here and a beginner in R, My problem: in the case i have more than one file (test1.dat, test2.dat,...) to work with in R i use this code to read them in filelist <- list.files(pattern = "*.dat") df_list <- lapply(filelist, function(x) read.table(x, header = FALSE, sep = "," ,colClasses = "factor", comment.char = "", col.names = "raw")) Now i have the problem that my data is big, i found a solution to speed things up using the sqldf-package : sql <- file("test2.dat") df <- sqldf(

r read in multiple .dat-files

白昼怎懂夜的黑 提交于 2020-01-14 12:43:29
问题 Hi I am new here and a beginner in R, My problem: in the case i have more than one file (test1.dat, test2.dat,...) to work with in R i use this code to read them in filelist <- list.files(pattern = "*.dat") df_list <- lapply(filelist, function(x) read.table(x, header = FALSE, sep = "," ,colClasses = "factor", comment.char = "", col.names = "raw")) Now i have the problem that my data is big, i found a solution to speed things up using the sqldf-package : sql <- file("test2.dat") df <- sqldf(

r read in multiple .dat-files

旧时模样 提交于 2020-01-14 12:43:05
问题 Hi I am new here and a beginner in R, My problem: in the case i have more than one file (test1.dat, test2.dat,...) to work with in R i use this code to read them in filelist <- list.files(pattern = "*.dat") df_list <- lapply(filelist, function(x) read.table(x, header = FALSE, sep = "," ,colClasses = "factor", comment.char = "", col.names = "raw")) Now i have the problem that my data is big, i found a solution to speed things up using the sqldf-package : sql <- file("test2.dat") df <- sqldf(

Inner join on LIKE sqldf

て烟熏妆下的殇ゞ 提交于 2020-01-14 12:25:26
问题 How can I use the LIKE clause with an inner join using sqldf in R? The code: Name <- c("Jack","Jill","Romeo") Name <- as.data.frame(Name) FullName <- c("School Jack H", "School Juliet G", "College Jill M", "College Romeo F") Marks <- c("100","82","54","0") FullBio <- cbind(FullName, Marks) FullBio <-as.data.frame(FullBio) And then when I run: sqldf("select a.*, b.* from Name a join FullBio b on a.Name like '%'+b.[FullName]+'%'") returns 0 rows. Why? What are my other alternatives please. I

Inner join on LIKE sqldf

橙三吉。 提交于 2020-01-14 12:25:26
问题 How can I use the LIKE clause with an inner join using sqldf in R? The code: Name <- c("Jack","Jill","Romeo") Name <- as.data.frame(Name) FullName <- c("School Jack H", "School Juliet G", "College Jill M", "College Romeo F") Marks <- c("100","82","54","0") FullBio <- cbind(FullName, Marks) FullBio <-as.data.frame(FullBio) And then when I run: sqldf("select a.*, b.* from Name a join FullBio b on a.Name like '%'+b.[FullName]+'%'") returns 0 rows. Why? What are my other alternatives please. I

SQL-like functionality in R

孤街浪徒 提交于 2020-01-13 07:48:11
问题 I am used to writing data manipulation logic in SQL and now that I am learning R I find myself sometimes just wanting to do something that would be simple in SQL but I have to learn a bunch of stuff with R to do the same manipulation on an R data frame. Is there a simple work around? 回答1: look at the package sqldf. http://code.google.com/p/sqldf/ It seems perfect for your needs. 回答2: I'm also more comfortable with SQL, but when working with large data sets in R, my favourite manipulation tool

Dplyr Filter Multiple Like Conditions

瘦欲@ 提交于 2020-01-11 14:13:09
问题 I am trying to do a filter in dplyr where a column is like certain observations. I can use sqldf as Test <- sqldf("select * from database Where SOURCE LIKE '%ALPHA%' OR SOURCE LIKE '%BETA%' OR SOURCE LIKE '%GAMMA%'") I tried to use the following which doesn't return any results: database %>% dplyr::filter(SOURCE %like% c('%ALPHA%', '%BETA%', '%GAMMA%')) Thanks 回答1: You can use grepl with ALPHA|BETA|GAMMA , which will match if any of the three patterns is contained in SOURCE column. database %

Updating data.table by inserting new rows that are different from old rows

喜欢而已 提交于 2019-12-31 03:19:07
问题 I have two data.table(dt1 & dt2). dt1 is past product data and dt2 is present product data. I want to create a third data.table that inserts new rows from dt2 into dt1 only when product characteristics(Level or Color) are different or Product itself is different. library(data.table) dt1 <- fread(' Product Level Color ReviewDate A 0 Blue 9/7/2016 B 1 Red 9/7/2016 C 1 Purple 9/7/2016 D 2 Blue 9/7/2016 E 1 Green 9/7/2016 F 4 Yellow 9/7/2016 ') dt2 <- fread(' Product Level Color ReviewDate A 1