数据预处理
1. 读入数据
读取 xlsx
1 2 library (openxlsx) d6.3 = read.xlsx('../Res/mvexer5.xlsx' , "E6.3" )
1 d10.1 = read.xlsx('mvstat5.xlsx' , 'd10.1' , rowNames=T )
2. 查看数据
2.1 数据维度 dim()
[1] 16 6
16行6列
2.2 查看变量数据类型 class()
1 class(data.frame(c(1 ,2 ,3 ),c(4 ,5 ,6 )))
2.3 转换数据类型
转换为 data.frame
1 2 3 # class(Giris): "matrix" "array" Giris <- as.data.frame(Giris) # "data.frame"
转换为 数值型,注意:这里 apply()
第二个参数是 2
,即按列
TODO: 不知道为什么,不能按行
1 diris <- apply(diris, 2, function(x)as.numeric(as.character(x))) # 转换为 数值型
转换为数值型后,前方索引为 [1,]
,而不是 1
转换前:
转换后:
其它常用
3. 数据处理
3.1 去重 unique()
1 2 3 duplicated(d6.4) d4_uni <- unique(d4)
3.2 添加列
读取数据选择 列
,利用 cols=c()
data.frame
添加列
1 2 3 d1 = read.xlsx("../Res/mvexer5.xlsx" , 'E6.5' , cols=c(2 ,3 ,4 ,5 )) d1$label = 1 ;d1
matrix
添加列 利用 transform()
1 2 3 4 5 6 7 8 m1 <- matrix(c(3 , 7 , 2 , 4 , 4 , 7 ), nrow = 3 , ncol = 2 , byrow = TRUE ) m1 = transform(m1, y = 1 ) m2 <- matrix(c(6 , 9 , 5 , 7 , 4 , 8 ), nrow = 3 , ncol = 2 , byrow = TRUE ) m2 = transform(m2, y = 2 ) data1 = rbind(m1,m2);data1
3.3 重定义列 colnames()
1 2 3 4 5 6 colnames(G1) <- c("x1" , "x2" , "x3" , "x4" ) colnames(G2) <- c("x1" , "x2" , "x3" , "x4" ) colnames(G3) <- c("x1" , "x2" , "x3" , "x4" ) diris = rbind(G1, G2, G3)
3.4 转换数据类型
1 d3 <- apply(d3, 2, function(x)as.numeric(as.character(x))) # 转换为 数值型
3.5 移除 NA 行
1 2 3 4 5 6 7 8 9 10 11 df = df[!is.na(df_$total_price),] x <- x[!is.na(x)] df_data = df_data[!is.na(df_data[, c("price" )]),]
is.na()
当值为 NA 或 NaN 时,返回 True
is.nan
当值为 NaN 时,返回 True
3.6 移除指定列名的列
1 2 df_temp = df_uq[, -which(names(df_uq)%in %c("product_color" , "price" ))]
3.7 将 na值 转为 0
1 2 df_data[is.na(df_data)] <- 0
3.8 将 0值 转为 NA
3.9 取出字符串两边空格
1 2 3 4 install.packages("raster" )library ("raster" ) df_data<-trim(df_data)
3.10 检查是否 有限值
1 sapply(df_tmp,is.finite)
3.11 数据分组以及分组汇总
aggregate函数——分组汇总
1 2 3 4 result1<-aggregate(df_data$product_color, by=df_data[,c("units_sold" )], sum) result2<-aggregate(df_data$product_color, by=df_data[,c("units_sold" )], max) result<-cbind(result1,result2)
3.12 按字符切割字符串
1 2 unlist(strsplit("Summer,Fashion,womenunderwearsuit,printedpajamasset,womencasualshort,Women's Fashion" , split = "," ))
3.13 修改行名、列名
1 names(df_data) <- c("wei" ,"hei" ,"gen" )
1 row.names(df_data)<-c("Mary" ,"Alice" ,"Bob" ,"Judy" )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 read.table() read.csv() read.csv2() read.delim() read.delim2() Data=read.csv("test.csv" ,row.names=1 ) Data=read.csv("test.csv" ,header=F )
1 2 rownames(df)<-df[,1 ] df<-df[,-1 ]
1 2 rownames(x) <- colnames(x) <-
3.14 根据多列、单列数据 新增列
根据多列 新增标签列
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 myFunc.get_diff_price_label<- function (x){ if (x[1 ]==x[2 ]) { return (1 ) } else if (x[1 ]<x[2 ]) { return (2 ) } else { return (3 ) } } df_data$diff_price_label = apply(df_data[, c("price" , "retail_price" )], 1 , myFunc.get_diff_price_label) myFunc.get_behavior_label<- function (x){ if (x[1 ]==1 && x[2 ]==0 ) { return (1 ) } else if (x[1 ]==0 && x[2 ]==1 ) { return (2 ) } else { return (3 ) } } df_data$behavior_label = apply(df_data[, c("uses_ad_boosts" , "has_urgency_banner" )], 1 , myFunc.get_behavior_label) df_data
根据单列 新增标签列
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 mean_units_sold = mean(df_data[, c("units_sold" )]) myFunc.get_sold_label<- function (x){ if (x[1 ]>mean_units_sold) { return (1 ) } else { return (0 ) } } df_data$sold_label = sapply(df_data[, c("units_sold" )], myFunc.get_sold_label) df_data
PS: 在R语言中,不同于其它语言,索引从 1 开始,维度也从 1 开始,所以 第1维度是行
而在Python中,索引从 0 开始,维度也从 0 开始,所以 第0维度 是 行
绘图
散点图
1 2 3 4 5 6 x1 = c(3 ,2 ,4 ,6 ,5 ,4 ) x2 = c(7 ,4 ,7 ,9 ,7 ,8 ) y = c(1 ,1 ,1 ,2 ,2 ,2 ) (data1 = data.frame(x1,x2,y)) plot(data1[,-3 ]) text(x1,x2,y,adj=-0.5 )
tidyverse
tidyverse
是一组处理与可视化R包的集合,其中ggplot2
与dplyr
最广为人知。
核心包有以下一些:
ggplot2 - 可视化数据
dplyr - 数据操作语法,可以用它解决大部分数据处理问题
tidyr - 清理数据
readr - 读入表格数据
purrr - 提供一个完整一致的工具集增强R的函数编程
tibble - 新一代数据框
stringr - 提供函数集用来处理字符数据
forcats - 提供有用工具用来处理因子问题
1 2 install.packages("tidyverse" )library (tidyverse)
分组汇总
属于 dplyr 包,已被包含于 tidyverse
1 2 3 4 5 6 df_product_color <- df_data %>% group_by(product_color) %>% summarise(sum_units_sold = sum(units_sold), items_count = n())
n() :无需参数返回当前分组的大小
PS: 出现报错,只是警告,无需在意
summarise()
ungrouping output (override with .groups
argument)
参考:分组汇总时提示:summarise() ungrouping output....解决方法
排序
1 2 3 4 5 6 7 8 9 10 11 df_product_color = arrange(df_product_color, desc(df_product_color$sum_units_sold)) df_product_color = as.data.frame(df_product_color) df_product_color_new = df_product_color[df_product_color["product_color" ]!='' ,] df_product_color_new
Q&A
补充
R语言中的 "因子" 变量类型 factor()
1 2 3 4 5 6 7 8 9 10 11 12 x <- c("10" , "20" , "30" , "50" ) as.numeric(x) y <- as.factor(x) y as.numeric(y)
因子(factor)转换成数值型(numeric)规则:
一共有n个数,那么转换后的数字就会在 1-n 中取值,数字最小的取1,次小的取2,以此类推
Q:那么如果让因子(factor)类型里的数值转换对应的数值型?
A:
as.numeric(as.character(factorName))
as.numeric(levels(factorName)[factorName])
设置国内镜像源
1 2 3 options(repos=structure(c(CRAN="http://mirrors.aliyun.com/CRAN/" ))) options(repos=structure(c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/" )))
参考
感谢帮助!