2.5.2 数据的读取

文本文件数据的读取

read.table()

scan()

read.fwf()

《1》使用函数read.table()用来创建一个数据框,因此对象是数据表。

> Price <- c(52.00, 54.75, 57.50, 57.5, 59.75)

> Floor <- c(111.0, 128.0, 101.0, 131.0, 93.0)
> Area <- c(830, 710, 1000, 690, 900)
> Rooms <- c(5,5,5,6,5)
> Age <- c(6.2, 7.5, 4.2, 8.8, 1.9)
> Gent.heat <- c("no", "no", "no", "no", "yes") #建立一个数据框

> d <- data.frame(Price, Floor, Area, Rooms, Age, Gent.heat)
> setwd("C:\\Users\\半个馒头\\Downloads\\统计分析\\R_study\\R_and_statistics_tangyincai\\data")  #转移到工作目录
> getwd()
[1] "C:/Users/半个馒头/Downloads/统计分析/R_study/R_and_statistics_tangyincai/data"
> write.table(d, file="houses.dat")
> HousePrice = read.table("houses.dat", header=TRUE)
> HousePrice
  Price Floor Area Rooms Age Gent.heat
1 52.00   111  830     5 6.2        no
2 54.75   128  710     5 7.5        no
3 57.50   101 1000     5 4.2        no
4 57.50   131  690     6 8.8        no
5 59.75    93  900     5 1.9       yes
> HousePrice = read.table("houses.dat")
> HousePrice
  Price Floor Area Rooms Age Gent.heat
1 52.00   111  830     5 6.2        no
2 54.75   128  710     5 7.5        no
3 57.50   101 1000     5 4.2        no
4 57.50   131  690     6 8.8        no
5 59.75    93  900     5 1.9       yes
> write.table(d, file="houses.csv")
> HousePrice = read.table("houses.csv")
> HousePrice
  Price Floor Area Rooms Age Gent.heat
1 52.00   111  830     5 6.2        no
2 54.75   128  710     5 7.5        no
3 57.50   101 1000     5 4.2        no
4 57.50   131  690     6 8.8        no
5 59.75    93  900     5 1.9       yes
> HousePrice = read.csv("houses.csv")
> HousePrice
  Price.Floor.Area.Rooms.Age.Gent.heat
1                1 52 111 830 5 6.2 no
2             2 54.75 128 710 5 7.5 no
3             3 57.5 101 1000 5 4.2 no
4              4 57.5 131 690 6 8.8 no
5             5 59.75 93 900 5 1.9 yes

 《2》scan()

和read.table()相比更加灵活,他们之间的区别是scan可以用what指定变量的类型,但是现在还没有看出他的作用,因为指定之前还是要知道变量的类型,否则报错,这与能不能指定关系不大。

> mydat <- scan("data.dat", what = list("", 0, 0))  #读文件
Read 4 records
> mydat
[[1]]
[1] "M" "F" "F" "M"

[[2]]
[1] 65 70 54 58

[[3]]
[1] 123 342 128 334

> mydat <- scan("data.dat", what = list(Sex="", Weight=0, Height=0)) #指定名称
Read 4 records
> mydat
$Sex
[1] "M" "F" "F" "M"

$Weight
[1] 65 70 54 58

$Height
[1] 123 342 128 334

> mydat <- scan("data.dat")    #缺省what,类型不对报错。
错误于scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  :
  scan()需要'a real', 而不是'M'

《3》read.fwf()

特异性在于其可以读取固定宽度的数据,其他的和read.table()无异。

> my <- read.fwf("data.txt", width=c(1,4,3), col.names=c("x", "y", "z"))
> my
  x    y   z
1 A 1.50 1.2
2 A 1.55 1.3
3 B 1.60 1.4
4 B 1.65 1.5
5 C 1.70 1.6
6 C 1.75 1.7


Excel数据的读取

有两种方式可以读取Excel数据。1)利用剪贴板,先复制,后在R中使用read.delim("clipboard")。2)使用程序包RODBC。但是需要安装,比较麻烦相对来说,但是命令还是附上。

Example:

> aa <- read.delim("clipboard")
> aa
                                         aa
1                          > library(RODBC)
2 > z <- odbcConnectExcel(c:/data/body.xls)
3              > foo <- sqlFetch(z, Sheet1)
4                                > close(z)

R中数据集的读取

1)R的标准数据datasets

这一段比较烦躁,没有搞清楚,需要需要的时候亲自实践,先放这儿,记着。

 

posted on 2012-11-28 21:17  半个馒头  阅读(1534)  评论(0编辑  收藏  举报

导航