R语言如何按行读取无序的TXT

Python021

R语言如何按行读取无序的TXT,第1张

r语言读取.txt文件中的内容,采用函数read.table(“xxx.txt”,head=TRUE|FALSE)其中head=TRUE表示含有属性的标题,head=FALSE表示不含属性的标题。下面以读取exam0203.txt中的文件为例。

//exam0203.txt

Name Sex Age Height Weight

Alice F 13 56.5 84.0

Becka F 13 65.3 98.0

Gail F 14 64.3 90.0

Karen F 12 56.3 77.0

Kathey F 12 59.8 84.5

Mary F 15 66.5 112.0

Sandy F 11 51.3 50.5

Sharon F 15 62.5 112.5

Tammy F 14 62.8 102.5

Alfred M 14 69.0 112.5

Duke M 14 63.5 84.0

Guido M 15 67.0 133.0

James M 12 57.3 83.0

Jeffrey M 13 62.5 84.0

John M 12 59.0 99.5

Philip M 16 72.0 150.0

Robert M 12 64.8 128.0

Thomas M 11 57.5 85.0

William M 15 66.5 112.0

由于该文件第一行是属性名称,即是含标题的.txt,所以head=TRUE。然后,编写.R脚本文件,如下:

//exam0203.R

setwd("G:\\myProject\\RDoc\\Unit1")

rt <- read.table("exam0203.txt",head=TRUE)rt

lm.sol <- lm(Weight~Height,data=rt)

summary(lm.sol)

sort()是对向量进行从小到大的排序

rank()返回的是对向量中每个数值对应的秩

order()返回的值表示位置,依次对应的是向量的最小值、次小值、第三小值......最大值

rank() sort() order() 和 reorder()

data<=c(2,3,6,1)

reorder()函数在ggplot2中见过:

x = reorder(Var1, -Freq) Var1是分类变量,Freq是数值型变量

help(recorder) 的结果:

Reorder Levels of a Factor

Description

reorder is a generic function. The "default" method treats its first argument as a categorical variable, and reorders its levels based on the values of a second variable, usually numeric.

Usage

reorder(x, ...)

Default S3 method:

reorder(x, X, FUN = mean, ...,

order = is.ordered(x))