read.table读取文本
csv文件需要sep函数取消分隔符,header取第一行为标签,skip跳过行,nrows读取到行为止,na.strings替换缺失值,stringsAsFactors字符串转化为因子
read.table("clipboard",header=T,sep=" ")读取剪切板内容
read.table(gzfile(""))读取解压缩gz
readLines(file,n=行)读取不规则文件
scan(file,what=指定类型list(X1=character(3),X2=numeric(0),X3=numeric(0)))
write(x)
write.table(x,file,sep,row.names=False,append=) 没有行序列
write.table(mtcars,gzfile(".txt.gz"))
XLConnect
ex <- loadWorkbook("data.xlsx")
readWorksheet(ex,1)
readWorksheetFromFile()
wb<- loadWorkbook("file.xlsx",create=T)
createSheet(wb,"Sheet 1")
writeWorksheet(wb,data=mtcars,sheet="Sheet 1")
saveWorkbook(wb)
writeWorksheetToFile(file,data,sheet)
read.xlsx("x.xlsx",1) write.xlsx()
saveRDS(iris,file='iris.RDS') readRDS
矩阵转化为数据框,矩阵要求类型统一
> is.data.frame(cars32)
[1] TRUE
> is.data.frame(state.x77)
[1] FALSE
> dstate.x77 <- as.data.frame(state.x77)
> is.data.frame(dstate.x77)
[1] TRUE
> dstate.x77
向量变为矩阵
> x <- state.abb
> dim(x) <- c(5,10)
向量变为因子
> x <- state.abb
> as.factor(x)
向量变为列表
> as.list(x)
变为数据框
> state <- data.frame(x,state.region,state.x77)
取列> state$Income
取行> state["Nevada",]
unname去除列名
unlist()转化为向量
who[which(who$Africa > 1500),7]
subset()
sample(x,n,replace)
sort()
cbind rbind要求一样的数据
data4[duplicate(data4)] ]unique()去重复值
t() 行列翻转 rev() women[rev(rownames(w)),]
transform()
order返回排序序列 rivers[order(rivers)] rivers[order(-rivers)]
apply(x,margin=1,fun=sum)数据框矩阵 lapply列表/sapply向量矩阵
scale()去中心化
merge(x,y,by=,incomparables=)
reshape2
melt()
> names(airquality) <- tolower(names(airquality))更改列名
> aql <- melt(airquality)清晰数据
> aql <- melt(airquality,id.vars=c("month","day"))选取观测值
aqw <- dcast(aql,month ~ variable,fun.aggregate = sum,na.rm=T)