【R语言入门与数据分析-3】 数据转换

使用过程中发现自己的R语言基础太差了,回头补课。P34-P40
所学视频
https://www.bilibili.com/video/BV19x411X7C6?p=31&spm_id_from=pageDriver
面向纯小白的R语言入门,我看过的最棒教材。授课老师平平无奇的东北口音、该死的幽默,扶我起来,我还可以再肝!

  • 数据转换,重中之重。
library(openxlsx)
cars32 <- read.xlsx("RData/mtcars.xlsx")
is.data.frame(cars32)
#数据框 → 矩阵 ,转换为字符型
methods(is)#查看全部判断内容
methods(as)
#基础的
x <-state.abb
dim(x) <- c(5,10)

who <- read.csv('RData/WHO.csv',header = T)
whol <- who[c(1:10),]
View(whol)
who$Country
who3<- who[which(who$Continent==7),]
who4 <- who[which(who$CountryID >50 &who$CountryID <100),]
who5 <- subset(who,who$CountryID >50 &who$CountryID <100)
sample() #replace =T 有刚回抽样,默认为F
x <- 1:100
sample(x,60,replace = T)
sample(who$CountryID,30,replace=F)
who[sample(who$CountryID,30,replace=F),]
#删除固定行
#负索引
#NULL
#行取出来还是行

#数据框的合并添加等
state.division
data.frame(USArrests,state.division)
cbind(USArrests,state.division)
#合并行,需要前后的列名一致
rbind()#cbind()也是要求前后行列数一致

!duplicated()#取出非重复的部分
unique()

t() 
rev() #应用于向量 ,翻转
women
women[rev(rownames(women)),]#翻转行
transform()#修改数据框中 列的数值
transform(women, height = height *2.54)

#排序函数,三个
sort #向量,返回排序后结果,不可用于数据框,可以排完序索引
order #向量,返回位置/索引,
mtcars[order(-mtcars$mpg),]#取order相反的结果,前面加- 
mtcars[rev(order(mtcars$mpg)),]#与上面一样
rank #求秩
mtcars[order(mtcars$mpg,mtcars$disp),]#多个条件筛选

WorldPhones
worldphones <- as.data.frame(WorldPhones)
rs <- rowSums(worldphones)
cm <- colMeans(worldphones)
total <- cbind(worldphones,total = rs)
total

#apply 系列函数
apply(WorldPhones,1,FUN = sum)#数据框,矩阵,数组
lapply(state.center, FUN = length)#列表,返回列表
sapply(state.center, FUN = length)##列表,返回向量
tapply(state.name,state.division,FUN = length) #处理因子

##数据的中心化与标准化
#中心化:各项数据减去数据集的均值
#标准化: 中心化之后再除以数据集的标准差,即数据集中的各项数据减去数据集的均值再除以数据集的标准差。
#以列为单位缩小
x <- c(1,2,3,6,3)
x- mean(x)
(x- mean(x))/sd(x)
scale(x, center = T, scale = T)
x <- scale(state.x77,center = T, scale = T)
heatmap(x)
apply系列函数

reshape2

x <- data.frame(k1= c(NA,NA,3,4,5),k2= c(1,NA,NA,4,5),data= 1:5)
y <- data.frame(k1= c(NA,2,NA,4,5),k2= c(NA,NA,3,4,5),data= 1:5)
merge(x,y,by = 'k1')
merge(x,y,by = 'k2',incomparables = NA)#丢掉NA
merge(x,y, by = c("k1","k2"))
library(reshape2)
airquality
names(airquality) <- tolower(names(airquality) )
aql <- melt(airquality,id.vars = c('month','day'))
aqw <- dcast(aql, month+day~variable)
aqw <- dcast(aql, month~variable,fun.aggregate = sum, na.rm = TRUE)
aqw <- dcast(aql, month ~ variable)

tidyr /hadley wickham

#四个重要函数
gather
spread 
unite
separate

tdata <- mtcars[1:10,1:3]
tdata <- data.frame(names=rownames(tdata),tdata) #行名加入数据框中
gather(tdata,key = 'Key',value = 'Value',cyl,disp,mpg)
gather(tdata,key = 'Key',value = 'Value',cyl,disp,-mpg)#删掉mpg
gdata <- gather(tdata,key = 'Key',value = 'Value',cyl:disp )

spread(gdata, key = 'Key', value = 'Value')

df <- data.frame(x= c(NA,'a.b','a.d','b.c'))
separate(df,col= x,into = c('A','B')) #默认识别分隔符
df <- data.frame(x= c(NA,'a.b-c','a-d','b-c'))
separate(df,col= x,into = c('A','B'),sep = '-')

x <- separate(df,col= x,into = c('A','B'),sep = '-')
unite(x,col='AB',A,B,sep = '-')

dplyr

#单个、多个数据集进行操作
ls('package:dplyr')#查看包里所有函数
help(package = 'foreign') #查看帮助
example('select')#查看select函数的所有例子
dplyr::filter(iris,Sepal.Length >7)
dplyr::distinct()
dplyr::slice()
dplyr::sample_n(iris,10)
dplyr::sample_frac(iris,0.1)#0.1百分比
dplyr::arrange(iris,desc(Petal.Width))
select()

#统计函数
summarise

# 链式操作符#%>% ,ctrl+shift+M
head(mtcars,20) %>% tail(10)
dplyr::group_by(iris,Species)
iris %>% dplyr::group_by(Species) %>% summarise(avg = mean(Sepal.Width)) %>% arrange(avg)

dplyr::mutate(iris,new=Sepal.Length+Petal.Length)

##双表格操作
a <- data.frame(x1 = c('A','B','C'),x2=c(1,2,3))
b <- data.frame(x1 = c('A','B','D'),x3 = c(F,F,T))
dplyr::left_join(a,b,by='x1')
dplyr::right_join(a,b,by='x1')
inner_join,semi_join,anti_join,full_join,union

你可能感兴趣的:(【R语言入门与数据分析-3】 数据转换)