R语言学习笔记:SQL操作

虽然R很强大,但如果对SQL非常熟悉,也不能浪费这项技能了,可以用上sqldf包,从example("sqldf")抄了几条用法放在这里,以后可能会用上。

library("tcltk")

a1r <- head(warpbreaks)

a1s <- sqldf("select * from warpbreaks limit 6")

a2r <- subset(CO2, grepl("^Qn", Plant))

a2s <- sqldf("select * from CO2 where Plant like ‘Qn%‘")

data(farms, package = "MASS")

a3r <- subset(farms, Manag %in% c("BF", "HF"))

row.names(a3r) <- NULL

a3s <- sqldf("select * from farms where Manag in (‘BF‘, ‘HF‘)")

a4r <- subset(warpbreaks, breaks >= 20 & breaks <= 30)

a4s <- sqldf("select * from warpbreaks where breaks between 20 and 30",  row.names = TRUE)

a5r <- subset(farms, Mois == ‘M1‘)

a5s <- sqldf("select * from farms where Mois = ‘M1‘", row.names = TRUE)

a6r <- subset(farms, Mois == ‘M2‘)

a6s <- sqldf("select * from farms where Mois = ‘M2‘", row.names = TRUE)

a7r <- rbind(a5r, a6r)

a7s <- sqldf("select * from a5s union all select * from a6s")

row.names(a7r) <- NULL

其它例子暂时用不到,就不试了,把example(sqldf)的输出记录在这里。

sqldf> # aggregate - avg conc and uptake by Plant and Type
sqldf> a8r <- aggregate(iris[1:2], iris[5], mean)

sqldf> a8s <- sqldf(‘select Species, avg("Sepal.Length") `Sepal.Length`,
sqldf+ avg("Sepal.Width") `Sepal.Width` from iris group by Species‘)

sqldf> all.equal(a8r, a8s)
[1] TRUE

sqldf> # by - avg conc and total uptake by Plant and Type
sqldf> a9r <- do.call(rbind, by(iris, iris[5], function(x) with(x,
sqldf+ data.frame(Species = Species[1],
sqldf+ mean.Sepal.Length = mean(Sepal.Length),
sqldf+ mean.Sepal.Width = mean(Sepal.Width),
sqldf+ mean.Sepal.ratio = mean(Sepal.Length/Sepal.Width)))))

sqldf> row.names(a9r) <- NULL

sqldf> a9s <- sqldf(‘select Species, avg("Sepal.Length") `mean.Sepal.Length`,
sqldf+ avg("Sepal.Width") `mean.Sepal.Width`,
sqldf+ avg("Sepal.Length"/"Sepal.Width") `mean.Sepal.ratio` from iris
sqldf+ group by Species‘)

sqldf> all.equal(a9r, a9s)
[1] TRUE

sqldf> # head - top 3 breaks
sqldf> a10r <- head(warpbreaks[order(warpbreaks$breaks, decreasing = TRUE), ], 3)

sqldf> a10s <- sqldf("select * from warpbreaks order by breaks desc limit 3")

sqldf> row.names(a10r) <- NULL

sqldf> identical(a10r, a10s)
[1] TRUE

sqldf> # head - bottom 3 breaks
sqldf> a11r <- head(warpbreaks[order(warpbreaks$breaks), ], 3)

sqldf> a11s <- sqldf("select * from warpbreaks order by breaks limit 3")

sqldf> # attributes(a11r) <- attributes(a11s) <- NULL
sqldf> row.names(a11r) <- NULL

sqldf> identical(a11r, a11s)
[1] TRUE

sqldf> # ave - rows for which v exceeds its group average where g is group
sqldf> DF <- data.frame(g = rep(1:2, each = 5), t = rep(1:5, 2), v = 1:10)

sqldf> a12r <- subset(DF, v > ave(v, g, FUN = mean))

sqldf> Gavg <- sqldf("select g, avg(v) as avg_v from DF group by g")

sqldf> a12s <- sqldf("select DF.g, t, v from DF, Gavg where DF.g = Gavg.g and v > avg_v")

sqldf> row.names(a12r) <- NULL

sqldf> identical(a12r, a12s)
[1] TRUE

sqldf> # same but reduce the two select statements to one using a subquery
sqldf> a13s <- sqldf("select g, t, v
sqldf+ from DF d1, (select g as g2, avg(v) as avg_v from DF group by g)
sqldf+ where d1.g = g2 and v > avg_v")

sqldf> identical(a12r, a13s)
[1] TRUE

sqldf> # same but shorten using natural join
sqldf> a14s <- sqldf("select g, t, v
sqldf+ from DF
sqldf+ natural join (select g, avg(v) as avg_v from DF group by g)
sqldf+ where v > avg_v")

sqldf> identical(a12r, a14s)
[1] TRUE

sqldf> # table
sqldf> a15r <- table(warpbreaks$tension, warpbreaks$wool)

sqldf> a15s <- sqldf("select sum(wool = ‘A‘), sum(wool = ‘B‘)
sqldf+ from warpbreaks group by tension")

sqldf> all.equal(as.data.frame.matrix(a15r), a15s, check.attributes = FALSE)
[1] TRUE

sqldf> # reshape
sqldf> t.names <- paste("t", unique(as.character(DF$t)), sep = "_")

sqldf> a16r <- reshape(DF, direction = "wide", timevar = "t", idvar = "g", varying = list(t.names))

sqldf> a16s <- sqldf("select
sqldf+ g,
sqldf+ sum((t == 1) * v) t_1,
sqldf+ sum((t == 2) * v) t_2,
sqldf+ sum((t == 3) * v) t_3,
sqldf+ sum((t == 4) * v) t_4,
sqldf+ sum((t == 5) * v) t_5
sqldf+ from DF group by g")

sqldf> all.equal(a16r, a16s, check.attributes = FALSE)
[1] TRUE

sqldf> # order
sqldf> a17r <- Formaldehyde[order(Formaldehyde$optden, decreasing = TRUE), ]

sqldf> a17s <- sqldf("select * from Formaldehyde order by optden desc")

sqldf> row.names(a17r) <- NULL

sqldf> identical(a17r, a17s)
[1] TRUE

sqldf> # centered moving average of length 7
sqldf> set.seed(1)

sqldf> DF <- data.frame(x = rnorm(15, 1:15))

sqldf> s18 <- sqldf("select a.x x, avg(b.x) movavgx from DF a, DF b
sqldf+ where a.row_names - b.row_names between -3 and 3
sqldf+ group by a.row_names having count(*) = 7
sqldf+ order by a.row_names+0",
sqldf+ row.names = TRUE)

sqldf> r18 <- data.frame(x = DF[4:12,], movavgx = rowMeans(embed(DF$x, 7)))

sqldf> row.names(r18) <- NULL

sqldf> all.equal(r18, s18)
[1] TRUE

sqldf> # merge. a19r and a19s are same except row order and row names
sqldf> A <- data.frame(a1 = c(1, 2, 1), a2 = c(2, 3, 3), a3 = c(3, 1, 2))

sqldf> B <- data.frame(b1 = 1:2, b2 = 2:1)

sqldf> a19s <- sqldf("select * from A, B")

sqldf> a19r <- merge(A, B)

sqldf> Sort <- function(DF) DF[do.call(order, DF),]

sqldf> all.equal(Sort(a19s), Sort(a19r), check.attributes = FALSE)
[1] TRUE

sqldf> # within Date, of the highest quality records list the one closest
sqldf> # to noon. Note use of two sql statements in one call to sqldf.
sqldf>
sqldf> Lines <- "DeployID Date.Time LocationQuality Latitude Longitude
sqldf+ STM05-1 2005/02/28 17:35 Good -35.562 177.158
sqldf+ STM05-1 2005/02/28 19:44 Good -35.487 177.129
sqldf+ STM05-1 2005/02/28 23:01 Unknown -35.399 177.064
sqldf+ STM05-1 2005/03/01 07:28 Unknown -34.978 177.268
sqldf+ STM05-1 2005/03/01 18:06 Poor -34.799 177.027
sqldf+ STM05-1 2005/03/01 18:47 Poor -34.85 177.059
sqldf+ STM05-2 2005/02/28 12:49 Good -35.928 177.328
sqldf+ STM05-2 2005/02/28 21:23 Poor -35.926 177.314
sqldf+ "

sqldf> DF <- read.table(textConnection(Lines), skip = 1, as.is = TRUE,
sqldf+ col.names = c("Id", "Date", "Time", "Quality", "Lat", "Long"))

sqldf> sqldf(c("create temp table DFo as select * from DF order by
sqldf+ Date DESC, Quality DESC,
sqldf+ abs(substr(Time, 1, 2) + substr(Time, 4, 2) /60 - 12) DESC",
sqldf+ "select * from DFo group by Date"))
Id Date Time Quality Lat Long
1 STM05-2 2005/02/28 12:49 Good -35.928 177.328
2 STM05-1 2005/03/01 18:47 Poor -34.850 177.059

sqldf> ## Not run:
sqldf> ##D
sqldf> ##D # test of file connections with sqldf
sqldf> ##D
sqldf> ##D # create test .csv file of just 3 records
sqldf> ##D write.table(head(iris, 3), "iris3.dat", sep = ",", quote = FALSE)
sqldf> ##D
sqldf> ##D # look at contents of iris3.dat
sqldf> ##D readLines("iris3.dat")
sqldf> ##D
sqldf> ##D # set up file connection
sqldf> ##D iris3 <- file("iris3.dat")
sqldf> ##D sqldf(‘select * from iris3 where "Sepal.Width" > 3‘)
sqldf> ##D
sqldf> ##D # using a non-default separator
sqldf> ##D # file.format can be an attribute of file object or an arg passed to sqldf
sqldf> ##D write.table(head(iris, 3), "iris3.dat", sep = ";", quote = FALSE)
sqldf> ##D iris3 <- file("iris3.dat")
sqldf> ##D sqldf(‘select * from iris3 where "Sepal.Width" > 3‘, file.format = list(sep = ";"))
sqldf> ##D
sqldf> ##D # same but pass file.format through attribute of file object
sqldf> ##D attr(iris3, "file.format") <- list(sep = ";")
sqldf> ##D sqldf(‘select * from iris3 where "Sepal.Width" > 3‘)
sqldf> ##D
sqldf> ##D # copy file straight to disk without going through R
sqldf> ##D # and then retrieve portion into R
sqldf> ##D sqldf(‘select * from iris3 where "Sepal.Width" > 3‘, dbname = tempfile())
sqldf> ##D
sqldf> ##D ### same as previous example except it allows multiple queries against
sqldf> ##D ### the database. We use iris3 from before. This time we use an
sqldf> ##D ### in memory SQLite database.
sqldf> ##D
sqldf> ##D sqldf() # open a connection
sqldf> ##D sqldf(‘select * from iris3 where "Sepal.Width" > 3‘)
sqldf> ##D
sqldf> ##D # At this point we have an iris3 variable in both
sqldf> ##D # the R workspace and in the SQLite database so we need to
sqldf> ##D # explicitly let it know we want the version in the database.
sqldf> ##D # If we were not to do that it would try to use the R version
sqldf> ##D # by default and fail since sqldf would prevent it from
sqldf> ##D # overwriting the version already in the database to protect
sqldf> ##D # the user from inadvertent errors.
sqldf> ##D sqldf(‘select * from main.iris3 where "Sepal.Width" > 4‘)
sqldf> ##D sqldf(‘select * from main.iris3 where "Sepal_Width" < 4‘)
sqldf> ##D sqldf() # close connection
sqldf> ##D
sqldf> ##D ### another way to do this is a mix of sqldf and RSQLite statements
sqldf> ##D ### In that case we need to fetch the connection for use with RSQLite
sqldf> ##D ### and do not have to specifically refer to main since RSQLite can
sqldf> ##D ### only access the database.
sqldf> ##D
sqldf> ##D con <- sqldf()
sqldf> ##D # this iris3 refers to the R variable and file
sqldf> ##D sqldf(‘select * from iris3 where "Sepal.Width" > 3‘)
sqldf> ##D sqldf("select count(*) from iris3")
sqldf> ##D # these iris3 refer to the database table
sqldf> ##D dbGetQuery(con, ‘select * from iris3 where "Sepal.Width" > 4‘)
sqldf> ##D dbGetQuery(con, ‘select * from iris3 where "Sepal.Width" < 4‘)
sqldf> ##D sqldf()
sqldf> ##D
sqldf> ## End(Not run)

时间: 2024-10-06 06:03:17

R语言学习笔记:SQL操作的相关文章

R语言学习笔记

參考:W.N. Venables, D.M. Smith and the R DCT: Introduction to R -- Notes on R: A Programming Environment for Data Analysis and Graphics,2003. http://bayes.math.montana.edu/Rweb/Rnotes/R.html 前言:关于R 在R的官方教程里是这么给R下注解的:一个数据分析和图形显示的程序设计环境(A system for data

R语言学习笔记2——绘图

R语言提供了非常强大的图形绘制功能.下面来看一个例子: > dose <- c(20, 30, 40, 45, 60)> drugA <- c(16, 20, 27, 40, 60)> drugB <- c(15, 18, 25, 31, 40) > plot(dose, drugA, type="b") > plot(dose, drugB, type="b") 该例中,我们引入了R语言中第一个绘图函数plot.pl

R语言学习笔记 之 可视化地研究参议员相似性

基于相似性聚类 很多时候,我们想了解一群人中的一个成员与其他成员之间有多么相似.例如,假设我们是一家品牌营销公司,刚刚完成了一份挂怒有潜力新品牌的研究调查问卷.在这份调查问卷中,我们向一群人展示了新品牌的几个特征,并且要求他们对这个新品牌的每个特征按五分制打分.同时也收集了目标人群的社会经济特征,例如:年龄.性别.种族.住址的邮编以及大概的年收入. 通过这份调查问卷,我们想搞清楚品牌如何吸引不同社会经济特征的人群.最重要的是,我们想要知道这个品牌是否有很大的吸引力.换个角度想这个问题,我们想看看

R语言学习笔记之: 论如何正确把EXCEL文件喂给R处理

前言: 应用背景兼吐槽 继续延续之前每个月至少一次更新博客,归纳总结学习心得好习惯. 这次的主题是论R与excel的结合,又称 论如何正确把EXCEL文件喂给R处理 分为: 1. xlsx包安装及注意事项 2.用vba实现xlsx批量转化csv 以及,这个的对象,针对跟我一样那些从R开始接触编程的,一直以来都是用excel做数据分析的人……编程大牛请轻拍 之所以要研究这个,是因为最近工作上接了个活,要把原来在excel端的报表迁移到R端,自动输出可视化图形,并制作PDF或PPT. 这个活可以分为

R语言学习笔记:基础知识

1.数据分析金字塔 2.[文件]-[改变工作目录] 3.[程序包]-[设定CRAN镜像] [程序包]-[安装程序包] 4.向量 c() 例:x=c(2,5,8,3,5,9) 例:x=c(1:100) 表示把1 - 100的所有数字都给x这个变量 5.查看x的类型:>mode(x) 6.查看x的长度:>length(x) 7.将两个向量组成一个矩阵: >rbind(x1, x2)  注:r是row的意思,即行,按行组成矩阵. >cbind(x1, x2)  注c是column的意思,

R语言学习笔记(二)

今天主要学习了两个统计学的基本概念:峰度和偏度,并且用R语言来描述. > vars<-c("mpg","hp","wt") > head(mtcars[vars]) mpg hp wt Mazda RX4 21.0 110 2.620 Mazda RX4 Wag 21.0 110 2.875 Datsun 710 22.8 93 2.320 Hornet 4 Drive 21.4 110 3.215 Hornet Sportab

R语言学习笔记:字符串处理

想在R语言中生成一个图形文件的文件名,前缀是fitbit,后面跟上月份,再加上".jpg",先不百度,试了试其它语言的类似语法,没一个可行的: C#中:"fitbit" + month + ".jpg" VB:"fitbit" & month & ".jpg" Haskell:"fitbit" ++ month ++ ".jpg" 还想到concat之

R语言学习笔记——日期时间处理

一.在利用R语言实际工作中,我们经常需要将字符串转换成时间,或者将时间转化成字符串,R语言和其他语言一样,你要告诉它如何转化?也就是告诉它format,它就可以正常的转化,但是在实际中,我碰到了一下几个很难注意的问题,先总结如下: 计算机如何理解日期:日期格式(也就是Date)表示为自1970年1月1日相对的数量,较1970-01-01更早的日期表示负值.(大部分语言都是这么处理的) 大部分语言有默认的日期格式,只要按照这个日期格式去转换字符串,计算机就能正确识别.如下: <span style

R语言学习笔记-概率函数

在R语言中,可以生成不同的分布,用于实验和学习. 在R中,概率函数形如①: 其中第一个字母表示其所指分布的某一方面: d = 密度函数(density) p = 分布函数(distribution function) q = 分位数函数(quantile function) r = 生成随机数(随机偏差) 常用的概率函数见下表: 表1 概率分布 分布名称 缩写 分布名称 缩写 Beta分布 beta Logistic分布 logis 二项分布 binom 多项分布 multinom 柯西分布 c

R语言学习笔记:日期处理

1.取出当前日期 Sys.Date() [1] "2014-10-29" date()  #注意:这种方法返回的是字符串类型 [1] "Wed Oct 29 20:36:07 2014" 2.在R中日期实际是double类型,是从1970年1月1日以来的天数 typeof(Sys.Date()) [1] "double" 3.转换为日期 用as.Date()可以将一个字符串转换为日期值,默认格式是yyyy-mm-dd. as.Date("