摘要: 数据科学实战, 抓住一本好的学习资料, 然后静下心来研究, 实战, 比到处东跑西看要好的多.
处理流程:
1) 将当前路径设置为文件vehicles.csv所在的路径:
# setwd()可以设置R软件的当前工作目录,getwd()可以获取当前工作目录
setwd("your file path")
2) 载入数据, 可以直接从zip文件中载入数据
vehicles <- read.csv(unz("vehicles.csv.zip", "vehicles.csv"), stringsAsFactors = F)
函数用法:
# 输入参数: description:文件名称, filename:zip文件中的文件名,相似的函数有: file,gzfile,bzfile等
unz(description, filename, open="",encoding=getOption("encoding"))
# 读取csv文件, 是read.table()的衍生品, R会自动将字符串的列辨认成factor,比如有一列是名字,如果不告诉R的话
# R会将这一列认成因子模式factor(character->factor). stringsAsFactors = FALSE
read.csv()
3) 查看数据是否已经载入,可以展示数据的前几行:
# Return the first or last part of an Object (data.frame,matrix,table,function)
> head(vehicles, n=5) # 打印前5行数据
> tail(vehicles, n=5) # 打印末尾5行数据
> tail(vehicles, n=2)
barrels08 barrelsA08 charge120 charge240 city08 city08U cityA08 cityA08U
37899 15.69571 0 0 0 18 0 0 0
37900 18.31167 0 0 0 16 0 0 0
cityCD cityE cityUF co2 co2A co2TailpipeAGpm co2TailpipeGpm comb08
37899 0 0 0 -1 -1 0 423.1905 21
37900 0 0 0 -1 -1 0 493.7222 18
comb08U combA08 combA08U combE combinedCD combinedUF cylinders displ
37899 0 0 0 0 0 0 4 2.2
37900 0 0 0 0 0 0 4 2.2
drive engId eng_dscr feScore fuelCost08
37899 4-Wheel or All-Wheel Drive