1. type.convert()函数主要用在read.table()函数中,返回向量和因子类型,当输入为double型时会丢失精度。
>type.convert(c('abc','bcd')) # 返回因子类型
[1] abc bcd
Levels: abc bcd
>type.convert(c(as.double(1.12121221111),'1.121')) # double型丢失精度
[1] 1.121212 1.121000
2. 如果一个文件包含有小数位的数据,通过read.table()函数读取时,会指定为numeric类型。
新建一个文件num.csv包括小数
1,2,1.11
2.1,3,4.5
用read.table读取文件,并查看列的类型。
>num<-read.table(file="num.csv",sep=",") # 读文件
>num
V1 V2 V3
1 1.0 2 1.11
2 2.1 3 4.50
>class(num)
[1] "data.frame"
>class(num$V1)# 查看列的类型为numeric
[1] "numeric"
3. tools包用Rdiff()函数的参数useDiff为FALSE时,与POSIX系统的diff -b命令类似。
新建文件num2.csv
3,2,1.11
2.1,3,4.5
用Rdiff()比较两个文件num.csv和num2.csv。
>Rdiff('num.csv','num2.csv',useDiff = FALSE)
1c1
<1,2,1.11
---
>3,2,1.11
[1] 1
4. 新函数anyNA(),结果与 any(is.na(.))一致,性能更好。
>is.na(c(1, NA))
[1] FALSE TRUE
>any(is.na(c(1, NA)))
[1] TRUE
>anyNA(c(1, NA))
[1] TRUE
5. arrayInd()和which()函数增加useNames参数,用于列名的匹配。我在测试过程,不太理解这个参数的意义。
>which
function (x, arr.ind = FALSE, useNames = TRUE)
6. is.unsorted()函数支持处理原始数据的向量。
>is.unsorted(1:10) # 排序的向量
[1] FALSE
>is.unsorted(sample(1:10)) # 无序的向量
[1] TRUE
7. 用于处理table的as.data.frame()函数和as.data.frame.table()函数,支持向provideDimnames(sep,base)函数传参数。我在测试过程中,也不理解具体是什么更新。
8. uniroot()函数增加新的可选参数extendInt,允许自动扩展取值范围,并增加返回对象参数init.it。
>f1 <- function(x) (121 - x^2)/(x^2+1) # 函数f1
>f2 <- function(x) exp(-x)*(x - 12) # 函数f2
>try(uniroot(f1, c(0,10))) # 在(0,10)的区间求f1函数的根
Error in uniroot(f1, c(0, 10)) :
f() values at end points not of opposite sign
>try(uniroot(f2, c(0, 2))) # 在(0,2)的区间求f2函数的根
Error in uniroot(f2, c(0, 2)) :
f() values at end points not of opposite sign
>str(uniroot(f1, c(0,10),extendInt="yes")) # 通过extendInt参数扩大取值搜索范围
List of 5
$ root : num 11
$ f.root: num -3.63e-06
$ iter : int 12
$ init.it : int 4
$ estim.prec: num 6.1e-05
>str(uniroot(f2, c(0,2), extendInt="yes")) # 通过extendInt参数扩大取值搜索范围
List of 5
$ root : num 12
$ f.root: num 4.18e-11
$ iter : int 23
$ init.it : int 9
$ estim.prec: num 6.1e-05
9. switch(f,)函数,当参数f是因子类型时,会出警告提示,需要转换字符串参数。
>switch(ff[1], A = "I am A", B="Bb..", C=" is C")# ->"A" # 警告提示
[1] "I am A"
Warning message:
In switch(ff[1], A = "I am A", B = "Bb..", C = " is C") :
EXPR is a "factor", treated as integer.
Consider using 'switch(as.character( * ), ...)' instead.
>switch(as.character(ff[1]), A = "I am A", B="Bb..", C=" is C") # 转型为字符串处理
[1] " is C"
10. 解析器已经更新,使用更少的内存。
下面这三部,可以逐一试一下。
1、Rstudio相关设置要改成UTF-8。位置在:1、Tools → Global Options → Default text encoding ;2、File → Save with encoding
2、数据文本要更改为UTF-8编码,工具notepad++,操作:Encoding → Convert to UTF-8,保存,参考
3、读取数据是,在read.csv或其他读取函数中,添加读取文件编码的参数 fileEncoding = "UTF8
reshape2包的进化版—tidyr包
tidyr包的作者是Hadley Wickham。这个包常跟dplyr结合使用。
本文将演示tidyr包中下述四个函数的用法:
gather—宽数据转为长数据。类似于reshape2包中的melt函数
spread—长数据转为宽数据。类似于reshape2包中的cast函数
unit—多列合并为一列
separate—将一列分离为多列
下面使用datasets包中的mtcars数据集做演示。
library(tidyr)
library(dplyr)
head(mtcars)
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
为方便处理,在数据集中增加一列car
mtcars$car <- rownames(mtcars)
mtcars <- mtcars[, c(12, 1:11)]
gather
gather的调用格式为:
gather(data, key, value, ..., na.rm = FALSE, convert = FALSE)
这里,...表示需要聚合的指定列。
与reshape2包中的melt函数一样,得到如下结果:
mtcarsNew <- mtcars %>% gather(attribute, value, -car)
head(mtcarsNew)
car attribute value
1 Mazda RX4 mpg 21.0
2 Mazda RX4 Wag mpg 21.0
3 Datsun 710 mpg 22.8
4 Hornet 4 Drive mpg 21.4
5 Hornet Sportabout mpg 18.7
6 Valiant mpg 18.1
tail(mtcarsNew)
car attribute value
347 Porsche 914-2 carb 2
348 Lotus Europa carb 2
349 Ford Pantera L carb 4
350 Ferrari Dino carb 6
351 Maserati Bora carb 8
352 Volvo 142E carb 2
如你所见,除了car列外,其余列聚合成两列,分别命名为attribute和value。
tidyr很好的一点是可以只gather若干列而其他列保持不变。如果你想gather在map和gear之间的所有列而保持carb和car列不变,可以像下面这样做:
mtcarsNew <- mtcars %>% gather(attribute, value, mpg:gear)
head(mtcarsNew)
car carb attribute value
1 Mazda RX4 4 mpg 21.0
2 Mazda RX4 Wag 4 mpg 21.0
3 Datsun 710 1 mpg 22.8
4 Hornet 4 Drive 1 mpg 21.4
5 Hornet Sportabout 2 mpg 18.7
6 Valiant 1 mpg 18.1
spread
spread的调用格式为:
spread(data, key, value, fill = NA, convert = FALSE, drop = TRUE)
与reshape2包中的cast函数一样,得到如下结果:
mtcarsSpread <- mtcarsNew %>% spread(attribute, value)
head(mtcarsSpread)
car carb mpg cyl disp hp drat wt qsec vs am gear
1 AMC Javelin 2 15.2 8 304 150 3.15 3.435 17.30 0 0 3
2 Cadillac Fleetwood 4 10.4 8 472 205 2.93 5.250 17.98 0 0 3
3 Camaro Z28 4 13.3 8 350 245 3.73 3.840 15.41 0 0 3
4 Chrysler Imperial 4 14.7 8 440 230 3.23 5.345 17.42 0 0 3
5 Datsun 710 1 22.8 4 108 93 3.85 2.320 18.61 1 1 4
6 Dodge Challenger 2 15.5 8 318 150 2.76 3.520 16.87 0 0 3
unite
unite的调用格式如下:
unite(data, col, ..., sep = "_", remove = TRUE)
where ... represents the columns to unite and col represents the c
这里,...表示需要合并的列,col表示合并后的列。
我们先虚构一些数据:
set.seed(1)
date <- as.Date('2016-01-01') + 0:14
hour <- sample(1:24, 15)
min <- sample(1:60, 15)
second <- sample(1:60, 15)
event <- sample(letters, 15)
data <- data.frame(date, hour, min, second, event)
data
date hour min second event
1 2016-01-01 7 30 29 u
2 2016-01-02 9 43 36 a
3 2016-01-03 13 58 60 l
4 2016-01-04 20 22 11 q
5 2016-01-05 5 44 47 p
6 2016-01-06 18 52 37 k
7 2016-01-07 19 12 43 r
8 2016-01-08 12 35 6 i
9 2016-01-09 11 7 38 e
10 2016-01-10 1 14 21 b
11 2016-01-11 3 20 42 w
12 2016-01-12 14 1 32 t
13 2016-01-13 23 19 52 h
14 2016-01-14 21 41 26 s
15 2016-01-15 8 16 25 o
现在,我们需要把date,hour,min和second列合并为新列datetime。通常,R中的日期时间格式为"Year-Month-Day-Hour:Min:Second"。
dataNew <- data %>%
unite(datehour, date, hour, sep = ' ') %>%
unite(datetime, datehour, min, second, sep = ':')
dataNew
datetime event
1 2016-01-01 7:30:29 u
2 2016-01-02 9:43:36 a
3 2016-01-03 13:58:60 l
4 2016-01-04 20:22:11 q
5 2016-01-05 5:44:47 p
6 2016-01-06 18:52:37 k
7 2016-01-07 19:12:43 r
8 2016-01-08 12:35:6 i
9 2016-01-09 11:7:38 e
10 2016-01-10 1:14:21 b
11 2016-01-11 3:20:42 w
12 2016-01-12 14:1:32 t
13 2016-01-13 23:19:52 h
14 2016-01-14 21:41:26 s
15 2016-01-15 8:16:25 o
separate
separate的调用格式为:
separate(data, col, into, sep = "[^[:alnum:]]+", remove = TRUE,
convert = FALSE, extra = "warn", fill = "warn", ...)
我们可以用separate函数将数据恢复到刚创建的时候,如下所示:
data1 <- dataNew %>%
separate(datetime, c('date', 'time'), sep = ' ') %>%
separate(time, c('hour', 'min', 'second'), sep = ':')
data1
date hour min second event
1 2016-01-01 07 30 29 u
2 2016-01-02 09 43 36 a
3 2016-01-03 13 59 00 l
4 2016-01-04 20 22 11 q
5 2016-01-05 05 44 47 p
6 2016-01-06 18 52 37 k
7 2016-01-07 19 12 43 r
8 2016-01-08 12 35 06 i
9 2016-01-09 11 07 38 e
10 2016-01-10 01 14 21 b
11 2016-01-11 03 20 42 w
12 2016-01-12 14 01 32 t
13 2016-01-13 23 19 52 h
14 2016-01-14 21 41 26 s
15 2016-01-15 08 16 25 o
首先,将datetime分为date列和time列。然后,将time列分为hour,min,second列。