数据读取

2018-08-31

2018-08-31
数据结构-获取数据

1 导入Excel数据


1.导入xlsx格式数据

说明:若要读取Excel数据,需借助readxl包导入数据,导入Excel数据到数据框中的函数是read_excel,基本书写格式为:

read_ excel(file=”文件名”)

例如,将Excel格式的cardata数据导入到数据框中:

library(readxl)                   ##加载readxl包
## Warning: package 'readxl' was built under R version 3.4.4
data=read_excel("F:/cardata.xlsx")     ##导入Excel数据
str(data)                        ##浏览对象data的存储类型和结构信息
## Classes 'tbl_df', 'tbl' and 'data.frame':    799 obs. of  7 variables:
##  $ V1: chr  "大众-朗逸 2013款 改款 1.4TSI DSG舒适版" "奥迪-A4L 2013款 35 TFSI 自动技术型" "纳智捷-优6 SUV 2016款 1.8T 风尚超值型" "奔驰-GLA 2016款 GLA 200 时尚型" ...
##  $ V2: num  8.5 23.8 9.5 26.5 5.1 7.8 7.3 17.1 3.95 5.2 ...
##  $ V3: POSIXct, format: "2013-10-01" "2014-04-01" ...
##  $ V4: num  8.13 3.56 1.61 3.85 9.2 6.59 4.3 9.3 6.2 0.8 ...
##  $ V5: chr  "国四" "国四" "国四" "国五" ...
##  $ V6: chr  "1.4" "2" "1.8" "1.6" ...
##  $ V7: num  14.8 37 11 29 14.9 ...

2.导入csv格式数据

说明:若要读取csv数据时,导入csv数据框中的函数是read.csv,基本书写格式为:

read.csv(file=”文件名.csv”)

为何对于csv标注了两种方法呢,详见如下:

(1)导入数据变量为字符型

data=read.csv("F:/cardata.csv")     ##导入csv数据
str(data)                        ##浏览对象data的存储类型和结构信息
## 'data.frame':    799 obs. of  7 variables:
##  $ V1: Factor w/ 680 levels "DS-5 2014款 1.6T 豪华版THP160",..: 247 36 474 94 601 549 587 44 300 408 ...
##  $ V2: Factor w/ 409 levels "卖家报价:¥0.65万",..: 382 188 401 201 294 369 354 115 236 299 ...
##  $ V3: Factor w/ 106 levels "2007年8月","2008年10月",..: 53 71 98 94 27 71 28 32 83 90 ...
##  $ V4: Factor w/ 470 levels "0.01万公里","0.02万公里",..: 421 212 63 226 451 357 247 455 335 29 ...
##  $ V5: Factor w/ 3 levels "国三","国四",..: 2 2 2 3 2 2 2 2 2 3 ...
##  $ V6: Factor w/ 20 levels "  0.9L 排量  ",..: 6 11 9 8 9 8 11 11 7 7 ...
##  $ V7: Factor w/ 429 levels "厂商新车指导价:10.08万",..: 88 269 21 215 90 64 112 281 331 358 ...

(2)变量改为因子型

data=read.csv("F:/cardata.csv", stringsAsFactors=F)      ##导入csv数据
str(data)                              ##浏览对象data的存储类型和结构信息
## 'data.frame':    799 obs. of  7 variables:
##  $ V1: chr  "大众-朗逸 2013款 改款 1.4TSI DSG舒适版" "奥迪-A4L 2013款 35 TFSI 自动技术型" "纳智捷-优6 SUV 2016款 1.8T 风尚超值型" "奔驰-GLA 2016款 GLA 200 时尚型" ...
##  $ V2: chr  "卖家报价:¥8.50万" "卖家报价:¥23.80万" "卖家报价:¥9.50万" "卖家报价:¥26.50万" ...
##  $ V3: chr  "2013年10月" "2014年4月" "2016年8月" "2016年4月" ...
##  $ V4: chr  "8.13万公里" "3.56万公里" "1.61万公里" "3.85万公里" ...
##  $ V5: chr  "国四" "国四" "国四" "国五" ...
##  $ V6: chr  "  1.4L 排量  " "  2.0L 排量  " "  1.8L 排量  " "  1.6L 排量  " ...
##  $ V7: chr  "厂商新车指导价:14.79万" "厂商新车指导价:36.98万" "厂商新车指导价:10.98万" "厂商新车指导价:28.98万" ...

  可以看到如果不加stringsAsFactors=F,则全部变量为因子型,加了stringsAsFactors=F则全部变量为字符串,由于cardata数据大多为数值型数据,因此加了stringsAsFactors=F把字符串转为数值型是比较合适的,因此建议大家如果遇到了数据变量大多为数值型数据可以直接加入stringsAsFactors=F将字符串变为数值型。

2 导入文本文件数据


说明:若要读取文本文件数据时,导入数据框中的函数是read.table,基本书写格式为:

read.table(”文件名.txt”,sep=””) 解释:sep=“” 表示以tab(制表符)为分隔符

data=read.table("F:/cardata.txt",sep="\t")
str(data)
## 'data.frame':    800 obs. of  7 variables:
##  $ V1: Factor w/ 681 levels "DS-5 2014款 1.6T 豪华版THP160",..: 28 248 37 475 95 602 550 588 45 301 ...
##  $ V2: Factor w/ 410 levels "V2","卖家报价:¥0.65万",..: 1 383 189 402 202 295 370 355 116 237 ...
##  $ V3: Factor w/ 107 levels "2007年8月","2008年10月",..: 107 53 71 98 94 27 71 28 32 83 ...
##  $ V4: Factor w/ 471 levels "0.01万公里","0.02万公里",..: 471 421 212 63 226 451 357 247 455 335 ...
##  $ V5: Factor w/ 4 levels "V5","国三","国四",..: 1 3 3 3 4 3 3 3 3 3 ...
##  $ V6: Factor w/ 21 levels "  0.9L 排量  ",..: 21 6 11 9 8 9 8 11 11 7 ...
##  $ V7: Factor w/ 430 levels "V7","厂商新车指导价:10.08万",..: 1 89 270 22 216 91 65 113 282 332 ...

3 导入剪贴板数据


说明:若要读取剪贴板数据时,首先需要对数据进行剪贴或复制再用下面的函数进行读取。导入数据框中的函数是read.table,基本书写格式为:

read.table(“clipboard”,header=T,sep=“”)

将剪贴板数据导入R如下:

data=read.table("clipboard",header=T,sep="\t")
str(data)
## 'data.frame':    799 obs. of  7 variables:
##  $ V1: Factor w/ 680 levels "DS-5 2014款 1.6T 豪华版THP160",..: 247 36 474 94 601 549 587 44 300 408 ...
##  $ V2: num  8.5 23.8 9.5 26.5 5.1 7.8 7.3 17.1 3.95 5.2 ...
##  $ V3: Factor w/ 106 levels "2007年8月","2008年10月",..: 53 71 98 94 27 71 28 32 83 90 ...
##  $ V4: num  8.13 3.56 1.61 3.85 9.2 6.59 4.3 9.3 6.2 0.8 ...
##  $ V5: Factor w/ 3 levels "国三","国四",..: 2 2 2 3 2 2 2 2 2 3 ...
##  $ V6: num  1.4 2 1.8 1.6 1.8 1.6 2 2 1.5 1.5 ...
##  $ V7: num  14.8 37 11 29 14.9 ...