R语言实用函数整理
2017-03-20 22:12
676 查看
初始化
options(stringsAsFactors=F,scipen=99) rm(list=ls());gc() getwd() 获得工作路径信息 setwd() 设置工作路径
清空控制台
快捷键control+L
获取目录下所有文件名
filenames=dir("/Users/yuyin/Downloads/数据/Excel数据") ##or推荐第二种 setwd("/Users/yuyin/Downloads/数据/Excel数据") filenames=dir()
读取文件输出文件
require(data.table) da<- fread("/Users/yuyin/Downloads/train_all_weekday.csv",header = FALSE) #读取gbk编码文件 u<- read.csv("JData_User.csv",fileEncoding='gbk',header = TRUE) write.table (out, file ="/Users/yuyin/Downloads/2.csv",sep =",",row.names = F,col.names=F,quote =F)
读写xlsx文件
library("xlsx") t=read.xlsx('吉林2014.xlsx',sheetIndex=1) write.xlsx(t, file="./s.xlsx")
SQL查询
library(sqldf) re=sqldf("select V1,V2,V6 from da where V2>=20161004 and V2<=20161017 order by V1,V2")
绘图
library(recharts) echartr(tmp,as.character(tmp$V2),V6,type = 'line')
分位数
#四个分位数 quantile(ck) #自定义分位数 quantile(ck, probs = c(0.85,0.95)) median中位数 mean均值
查看行数
nrow(data.frame)
字符串操作
拼接字符串
##方法一 paste(Y,'/',m,'/',d,sep='') ##方法二 library(stringr) pout=str_c(path,name,collapse='')
替换字符串
name=str_replace_all(name,"/","_")
DF去重
tt=unique(tt)
合并数据框
合并行 rbind(t1,t2) 合并列 cbind(t1,t2)
DF排序
x=x[order(x$bad_comment_rate,decreasing=F),]
生成随机数
runif(n, min=0, max=1) 均匀分布 rnorm(n, mean=0, sd=1) 正态分布 sample(seq(0,100,by=1),1,replace=TRUE) 抽样生成随机数
最大最小归一化
b1=(data[,1]-min(data[,1]))/(max(data[,1])-min(data[,1])) b1=(d-min(d))/(max(d)-min(d))
日期转换
dateChar<-("2014-04-06") dtV<-as.POSIXct(dateChar,format="%Y-%m-%d") ##或者dtV<-as.Date(dateChar,format="%Y-%m-%d") format(dtV,"%Y/%m/%d %H:%M:%S") #转换为2014/4/6 Y=format(dtV,"%Y") m=as.character(as.numeric(format(dtV,"%m"))) d=as.character(as.numeric(format(dtV,"%d"))) dt<-paste(Y,'/',m,'/',d,sep='')
计算时间差
d <- c('2013-12-05 18:43:00','2013-08-23 22:29:00') difftime(d[2],d[1]) difftime(strptime(d, "%Y-%m-%d %H:%M:%S")[2],strptime(d, "%Y-%m-%d %H:%M:%S")[1],units='secs')
高效数据清洗包dplyr代替sqldf
速度比sqldf快很多 适合数据量大处理library(dplyr) #将数据整理成的tbl_df数据(处理速度快) iris <- tbl_df(iris) ##变量筛选select 对应select 删除- select(iris,Sepal.Length,Sepal.Width) select(iris,-Species) ##对数据运算并添加为新列mutate() 对应 count(a) as t1 mutate(iris,t1=Sepal.Length*2) ##计算 n(): 计算个数 n_distinct() #: 计算 x 中唯一值的个数 first(x), last(x) 和 nth(x, n)#: 返回对应秩的值, 类似于自带函数 x[1], x[length(x)], 和 x ##过滤filter 对应 where filter(iris,Sepal.Length>5,Sepal.Width<4) filter(iris,Sepal.Length>5 & Sepal.Width<4 & (Species == "setosa" | Species == "versicolor")) ##数据排序arrange 对应 order by arrange(iris,Sepal.Length) arrange(iris,desc(Sepal.Length)) ##汇总group_by() 分组-汇总 group_by(iris, Species) group_by(iris,Species,Petal.Width) %>% summarise(c1=n(),c2=n_distinct(Species)) ##计算summarise() summarise(iris,c1=n(),c2=mean(Sepal.Length)) ##多步操作连接符%>% filter(iris,Sepal.Length>5,Sepal.Width<4) %>% summarise(c1=n(),c2=mean(Sepal.Length)) ##抽样sample_n sample_frac sample_n(iris,20) ##左连接 ab交集 差集 left_join(a, b, by="x1") right_join(a, b, by="x1") inner_join(a, b, by="x1")##保留匹配的数据 outer_join(a, b, by="x1")##保留所有数据 semi_join(a, b, by="x1") # 数据集a中能与数据集b匹配的记录 anti_join(a, b, by="x1") # 数据集a中雨数据集b不匹配的记录 intersect(x, y): x 和 y 的交集(按行) union(x, y): x 和 y 的并集(按行) setdiff(x, y): x 和 y 的补集 (在x中不在y中) ##列合并 bind_cols(y, z) ##行合并 bind_rows(y, z)
参考(特别是文章后面的翻译图片)
查询相关R包
library(sos) findFn('onehot') ##具体看sos的help
相关文章推荐
- MATLAB实用函数及技巧整理
- R语言时间序列函数整理[不断更新]
- 【python】整理一些实用的函数
- 常见实用的SQL语句、存储过程、游标、函数、视图(不断整理中)
- 【python】整理一些实用的函数
- 【python】整理一些实用的函数
- R语言时间序列函数整理
- 整理一些实用的函数
- R语言学习-对象处理实用函数
- 【python】整理一些实用的函数
- 【python】整理一些实用的函数
- php实用函数整理
- R语言学习-对象处理实用函数
- 最实用的JS数组函数整理
- 【python】整理一些实用的函数
- 【python】整理一些实用的函数
- 【python】整理一些实用的函数
- MATLAB 图像处理一些实用函数整理
- 【python】整理一些实用的函数
- MFC编程实用函数整理