在RGui中读取.txt文件中的内容,采用函数read.table(“xxx.txt”,head=TRUE|FALSE); 其中head=TRUE表示含有属性的标题,head=FALSE表示不含属性的标题。下面以读取exam0203.txt中的文件为例。
//exam0203.txtName Sex Age Height WeightAlice F 13 56.5 84.0Becka F 13 65.3 98.0Gail F 14 64.3 90.0Karen F 12 56.3 77.0Kathey F 12 59.8 84.5Mary F 15 66.5 112.0Sandy F 11 51.3 50.5Sharon F 15 62.5 112.5Tammy F 14 62.8 102.5Alfred M 14 69.0 112.5Duke M 14 63.5 84.0Guido M 15 67.0 133.0James M 12 57.3 83.0Jeffrey M 13 62.5 84.0John M 12 59.0 99.5Philip M 16 72.0 150.0Robert M 12 64.8 128.0Thomas M 11 57.5 85.0William M 15 66.5 112.01
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
由于该文件第一行是属性名称,即是含标题的.txt,所以head=TRUE。然后,编写.R脚本文件,如下:
//exam0203.Rsetwd("G:\\myProject\\RDoc\\Unit1")rt
2
3
4
将.txt和.R文件放在同一个目录下,比如:G:\myProject\RDoc\Unit1 ,如果放在其他目录,则.R中的setwd(“X:\\”)要进行相应的修改,如图(1)所示 : 图(1)将.txt和.R文件放在同一个目录下
效果如下: