随笔分类 -  R语言

数据操作之排序
摘要:R R order(X, na.last=TRUE, decreasing=FALSE) 返回值: X排好序的下标向量 na.last 控制空值NA排在最前还是最后,默认最后 desceasing 控制升序还是降序排列 例子: #vector X <- c(7,4,5,2,8,1,9,3) orde 阅读全文

posted @ 2016-03-23 15:01 MartinChau 阅读(214) 评论(0) 推荐(0)

常用的数据类型及处理
摘要:● 向量(X) ● 赋值 assign(向量名, X)。 向量名<- X X ->向量名 ● 运算 加(+),减(-),乘(*),除(/),整除(%/%),求余数(%%) exp(),log,cos,tan,sqrt… 最大值:max(X) 最小值:min(X) 范围:range(X) 求和:sum 阅读全文

posted @ 2016-03-23 14:59 MartinChau 阅读(392) 评论(0) 推荐(0)

分组求和
摘要:刚遇到一个问题,就是分组求和在R里怎么实现比较简便?对应SQL语句为:SELECT customer, sum(consumption) FROM consume group by customer 然后就是类似的,分组计数怎么做? 在搞复杂点。 阅读全文

posted @ 2016-03-23 14:56 MartinChau 阅读(311) 评论(0) 推荐(0)

批量读取本地文件
摘要:doc.names <- dir("path") doc.path <- sapply(doc.names,function(names) paste(path,names,sep='/')) doc <- sapply(doc.path, function(doc) readLines(doc)) doc.names <- dir("F:/5分钟报价1") #读取目录内... 阅读全文

posted @ 2016-03-23 14:54 MartinChau 阅读(206) 评论(0) 推荐(0)

数据文件读写
摘要:R语言数据储存与读取 1 首先用getwd() 获得当前目录,用setwd("C:/data")设定当前目录 2 数据保存 创建数据框d 2.1 保存为简单文本 2.2 保存为逗号分割文本 2.3 保存为R格式文件 2.4 保存工作空间镜像 3 数据读取 读取函数主要有:read.table( ), 阅读全文

posted @ 2016-03-23 14:51 MartinChau 阅读(218) 评论(0) 推荐(0)

数据类型转换
摘要:test for data type is.numeric() is.character() is.vector() is.matrix() is.data.frame() convert it as.numeric() as.character() as.vector() as.matrix() 阅读全文

posted @ 2016-03-23 14:49 MartinChau 阅读(130) 评论(0) 推荐(0)

常用基础知识
摘要:举例 Examples Loops The most commonly used loop structures in R are for, while and apply loops. Less common are repeat loops. The break function is used 阅读全文

posted @ 2016-03-23 14:47 MartinChau 阅读(139) 评论(0) 推荐(0)

算术运算和逻辑运算
摘要:Arithmetic Operators Operator Description + addition - subtraction * multiplication / division ^ or ** exponentiation x %% y modulus (x mod y) 5%%2 is 1 x %/% y integer division 5%/... 阅读全文

posted @ 2016-03-23 14:42 MartinChau 阅读(294) 评论(0) 推荐(0)

记录程序运行的时间
摘要:f <- function(start_time) { start_time <- as.POSIXct(start_time) dt <- difftime(Sys.time(), start_time, units="secs") # Since you only want the H:M:S, we can ignore the date... # but you have... 阅读全文

posted @ 2016-03-23 14:40 MartinChau 阅读(151) 评论(0) 推荐(0)

数据框排序 data.frame order
摘要:# sorting examples using the mtcars dataset attach(mtcars) # sort by mpg newdata <- mtcars[order(mpg),] # sort by mpg and cyl newdata <- mtcars[order(mpg, cyl),] #sort by mpg (ascending) and cyl (... 阅读全文

posted @ 2016-03-23 14:38 MartinChau 阅读(213) 评论(0) 推荐(0)

合并data.frame (merge)
摘要:Merging Data Adding Columns To merge two data frames (datasets) horizontally, use the merge function. In most cases, you join two data frames by one o 阅读全文

posted @ 2016-03-23 14:37 MartinChau 阅读(568) 评论(0) 推荐(0)

命令行运行R语言脚本(代码)
摘要:1 Windows: 键入 cd C:\Program Files\R\R-3.2.0\bin 工作目录切换到R的核心程序目录键入 R BATCH F:\Test.R 或 Rscript F:\Test.R 运行脚本前者R控制台内容记录到Test.Rout文件中,后者则将数据输出到windows控制 阅读全文

posted @ 2016-03-23 14:32 MartinChau 阅读(728) 评论(0) 推荐(0)

R语言画图基础参数设置
摘要:Graphical Parameters You can customize many features of your graphs (fonts, colors, axes, titles) through graphic options. One way is to specify these 阅读全文

posted @ 2016-03-23 14:30 MartinChau 阅读(3804) 评论(0) 推荐(0)

日期时间函数
摘要:Sys.Date( ) #returns today's date. date() #returns the current date and time. # print today's date today <-Sys.Date() format(today, format="%B %d %Y") "June 20 2007" # convert date info in format '... 阅读全文

posted @ 2016-03-23 14:25 MartinChau 阅读(290) 评论(0) 推荐(0)

绘制地图
摘要:在R中画地图先从简单的maps包开始。 library("maps") 在这个maps包中有一些数据集,用命令data(package=”maps”),可以看到如下数据: canada.cities Database of Canadian cities county.fips FIPS count 阅读全文

posted @ 2016-03-23 14:23 MartinChau 阅读(299) 评论(0) 推荐(0)

向量
摘要:向量是R语言最基本的数据类型。 单个数值(标量)其实没有单独的数据类型,它只不过是只有一个元素的向量。 R不需要提前申明变量,大小写敏感,数组是从1开始。 冒号运算符 循环补齐recycle 这种特性在其它语言中以前还没有看见过!超强的向量运算功能! 取模运算%% 向量索引 普通的语言中索引只能是正 阅读全文

posted @ 2016-03-23 14:18 MartinChau 阅读(292) 评论(0) 推荐(0)

日期处理
摘要:1、取出当前日期 2、在R中日期实际是double类型,是从1970年1月1日以来的天数 3、转换为日期 用as.Date()可以将一个字符串转换为日期值,默认格式是yyyy-mm-dd。 格式 意义 %Y 年份,以四位数字表示,2007 %m 月份,以数字形式表示,从01到12 %d 月份中当的天 阅读全文

posted @ 2016-03-23 14:16 MartinChau 阅读(156) 评论(0) 推荐(0)

简单的回归分析
摘要:查看step与dist的相关度,可以看出值几乎等于1,说明步数与距离是强相关。 进行简单的线性回归: 显示dist.step的值可以看到: 说明拟合的公式为:dist = -0.0059966 + 0.0007171 * step 可以用abline函数画出拟合的直线: 还有一个更牛的car包,可以 阅读全文

posted @ 2016-03-23 14:15 MartinChau 阅读(429) 评论(0) 推荐(0)

字符串处理
摘要:想在R语言中生成一个图形文件的文件名,前缀是fitbit,后面跟上月份,再加上".jpg",先不百度,试了试其它语言的类似语法,没一个可行的: C#中:"fitbit" + month + ".jpg" VB:"fitbit" & month & ".jpg" Haskell:"fitbit" ++ 阅读全文

posted @ 2016-03-23 14:11 MartinChau 阅读(717) 评论(0) 推荐(0)

SQL操作
摘要:虽然R很强大,但如果对SQL非常熟悉,也不能浪费这项技能了,可以用上sqldf包,从example("sqldf")抄了几条用法放在这里,以后可能会用上。 library("tcltk") a1r <- head(warpbreaks) a1s <- sqldf("select * from war 阅读全文

posted @ 2016-03-23 14:05 MartinChau 阅读(195) 评论(0) 推荐(0)

导航