2016年3月23日

数据的输入

摘要: 键盘输入 调用edit函数,比如我们要让用户输入一个长度为5的向量并赋值给变量a,那么可以: a<-vector("integer",5) a<-edit(a) 另外也可以用函数fix来直接编辑变量,而不需要再赋值变量。所以上面编辑a变量的命令可以改为: a<-vector("integer",5) 阅读全文

posted @ 2016-03-23 15:06 MartinChau 阅读(158) 评论(0) 推荐(0) 编辑

循环函数(Grouping Function)

摘要: R语言中有几个常用的函数,可以按组对数据进行处理,apply, lapply, sapply, tapply, mapply,等。这几个函数功能有些类似,下面介绍下这几个函数的用法。 Apply 这是对一个Matrix或者Array进行某个维度的运算。其格式是: Apply(数据,维度Index,运 阅读全文

posted @ 2016-03-23 15:05 MartinChau 阅读(776) 评论(0) 推荐(0) 编辑

Data Frame数据框常用操作

摘要: Data Frame一般被翻译为数据框,感觉就像是R中的表,由行和列组成,与Matrix不同的是,每个列可以是不同的数据类型,而Matrix是必须相同的。 Data Frame每一列有列名,每一行也可以指定行名。如果不指定行名,那么就是从1开始自增的Sequence来标识每一行。 初始化 使用dat 阅读全文

posted @ 2016-03-23 15:04 MartinChau 阅读(583) 评论(0) 推荐(0) 编辑

数据操作之排序

摘要: 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 阅读(194) 评论(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 阅读(375) 评论(0) 推荐(0) 编辑

分组求和

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

posted @ 2016-03-23 14:56 MartinChau 阅读(304) 评论(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 阅读(201) 评论(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 阅读(200) 评论(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 阅读(127) 评论(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 阅读(133) 评论(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 阅读(289) 评论(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 阅读(147) 评论(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 阅读(211) 评论(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 阅读(538) 评论(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 阅读(683) 评论(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 阅读(3756) 评论(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 阅读(280) 评论(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 阅读(276) 评论(0) 推荐(0) 编辑

向量

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

posted @ 2016-03-23 14:18 MartinChau 阅读(280) 评论(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 阅读(154) 评论(0) 推荐(0) 编辑

导航