《R语言经典示例》学习笔记(二)

输入与输出

  • b<-data.frame();a<-edir(b)先创建一个空的数据框,在使用R内置的表格编辑器填充其中的内容。适合于少量数据。
  • print(pi,digits=4)输出pi的值,并保留4位小数(R一般以7位浮点数的形式作为输出格式)。
    print()函数中的digits参数能控制显示结果的位数。
  • options(digits=7)设定默认的位数,改变了R内置函数的输出格式。
  • list.files()显示当前工作目录中的文件。
  • read.table()函数读取一个包含表格数据的文本文件,结果会返回一个数据框。
  • read.csv()函数读取逗号分隔符文件中的数据。如果csv文件不包含标题栏:read.csv(“filename”,header=FALSE)。
  • write.csv(x,file=”filename”,row.name=FALSE)函数以csv文件的格式保存数据。

数据结构

向量

  • 同质;
  • 可按照位置进行索引;
  • 元素可以被命名:
v<-c(10,20,30)
names(v)<-c("Moe","Larry","Curly")
v
    Moe    Larry    Curly
    10         20         30
  • 向量元素被命名,可用名称选定它们:
v["Larry"]
Larry 
20

列表

  • 非同质;
  • 可按照位置进行索引;
  • 允许抽取子列表;
  • 列表元素可以有名称。

矩阵

有维数的向量

数组

创建维数为2*3*2的3维数组:

>d<-1:12
> d
 [1]  1  2  3  4  5  6  7  8  9 10 11 12
> dim(d)<-c(2,3,2)
> d
, , 1

     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

, , 2

     [,1] [,2] [,3]
[1,]    7    9   11
[2,]    8   10   12

因子

  • 分类变量
    一个因子可以代表一个分类变量。分类变量用于:列联表、线性回归、方差分析、逻辑回归以及许多其他领域。
  • 分组
    根据数据组别来分类或标记数据项。

数据框

  • 列表的元素是向量或者因子,构成数据框的列;
  • 向量或者因子必须有相同的长度;
  • 数据框的列必须命名;
  • 由于数据框既是列表又是矩阵,R软件提供两种不同模式来访问数据框的内容:
    列表操作:a[i]、a[[i]]或者a$name;
    矩阵操作:a[i,j]、a[,j]或者a[i,]。
对向量添加数据
  • v<-c(v,newItems)构造含有额外数据项的向量。
> v<-c(1,2,3)
> v
[1] 1 2 3
> v<-c(v,4,5,6)
> v
[1] 1 2 3 4 5 6
  • v[length(v)+1]<-newItem把添加的新数据项赋给下一个向量元素,R会自动扩充向量。
> v<-c(1,2,3)
> v
[1] 1 2 3
> v[c(4,5,6)]<-c(6,5,4)
> v
[1] 1 2 3 6 5 4
在向量中插入数据
  • append(vec,newvalues,after=n)使用after参数在一个向量中插入数据,该参数给出新数据项的插入点。n=0意味着在向量的最前面插入新数据项。
> append(1:10,24,after = 3)
 [1]  1  2  3 24  4  5  6  7  8  9 10
> append(1:10,24,after = 0)
 [1] 24  1  2  3  4  5  6  7  8  9 10
矩阵运算
  • t(A)求矩阵A的转置。
  • solve(A)求矩阵A的逆。
  • A%*%B两矩阵相乘。
  • diag(n)一个n阶对角(单位)矩阵。
将描述性名称赋给矩阵的行和列
  • rownames()<-c(“”,”“,”“)行
  • colnames()<-c(“”,”“,”“)列
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
With more than 200 practical recipes, this book helps you perform data analysis with R quickly and efficiently. The R language provides everything you need to do statistical work, but its structure can be difficult to master. This collection of concise, task-oriented recipes makes you productive with R immediately, with solutions ranging from basic tasks to input and output, general statistics, graphics, and linear regression. Each recipe addresses a specific problem, with a discussion that explains the solution and offers insight into how it works. If you're a beginner, R Cookbook will help get you started. If you're an experienced data programmer, it will jog your memory and expand your horizons. You'll get the job done faster and learn more about R in the process. * Create vectors, handle variables, and perform other basic functions * Input and output data * Tackle data structures such as matrices, lists, factors, and data frames * Work with probability, probability distributions, and random variables * Calculate statistics and confidence intervals, and perform statistical tests * Create a variety of graphic displays * Build statistical models with linear regressions and analysis of variance (ANOVA) * Explore advanced statistical techniques, such as finding clusters in your data "Wonderfully readable, R Cookbook serves not only as a solutions manual of sorts, but as a truly enjoyable way to explore the R language-one practical example at a time." -Jeffrey Ryan, software consultant and R package author
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值