R Language

向量定义:x1 = c(1,2,3); x2 = c(1:100)

类型显示:mode(x1)

向量长度:length(x2)

向量元素显示:x1[c(1,2,3)]

多维向量:multi-dimensional vector:rbind(x1,x2); cbind(x1,x2)

 1 > x = c(1,2,3,4,5,6)
 2 > y = c(6,5,4,3,2,1)
 3 > z = rbind(x,y)
 4 > z
 5   [,1] [,2] [,3] [,4] [,5] [,6]
 6 x    1    2    3    4    5    6
 7 y    6    5    4    3    2    1
 8 > z = cbind(x,y)
 9 > z
10      x y
11 [1,] 1 6
12 [2,] 2 5
13 [3,] 3 4
14 [4,] 4 3
15 [5,] 5 2
16 [6,] 6 1
17 > 

一维变二维向量:

> mtx=matrix(1:12, nrow=3, ncol=4)
> mtx
     [,1] [,2] [,3] [,4]
[1,]    1    4    7   10
[2,]    2    5    8   11
[3,]    3    6    9   12

> mtx=matrix(1:12, nrow=3, ncol=4, byrow=T)
> mtx
     [,1] [,2] [,3] [,4]
[1,]    1    2    3    4
[2,]    5    6    7    8
[3,]    9   10   11   12

矩阵转置:

> mtx
     [,1] [,2] [,3] [,4]
[1,]    1    2    3    4
[2,]    5    6    7    8
[3,]    9   10   11   12
> t(mtx)
     [,1] [,2] [,3]
[1,]    1    5    9
[2,]    2    6   10
[3,]    3    7   11
[4,]    4    8   12

矩阵相乘:

> a = mtx%*%t(mtx)> a
     [,1] [,2] [,3]
[1,]   30   70  110
[2,]   70  174  278
[3,]  110  278  446

对角线矩阵:

> diag(a)
[1]  30 174 446

> diag(diag(a))
     [,1] [,2] [,3]
[1,]   30    0    0
[2,]    0  174    0
[3,]    0    0  446

> diag(3)
     [,1] [,2] [,3]
[1,]    1    0    0
[2,]    0    1    0
[3,]    0    0    1

逆矩阵:

> a = matrix(rnorm(16),4,4)
> a
           [,1]       [,2]      [,3]       [,4]
[1,]  0.5116868 -0.5839355 0.9038526 -1.5063944
[2,] -1.0657446 -2.2067686 1.2187536  0.1999609
[3,]  0.4784326 -2.1762163 0.1937103  0.0255462
[4,] -2.5393649 -0.1884904 2.7594314 -0.6955184

> solve(a)
           [,1]      [,2]     [,3]      [,4]
[1,] -1.7427160 -5.204571 5.530340 2.4812951
[2,] -0.6103805 -1.743684 1.396904 0.8719984
[3,] -2.2412701 -6.107496 6.505573 3.3373229
[4,] -2.3639756 -4.756515 5.240446 2.5072471

> solve(a)%*%a
              [,1]          [,2]          [,3]          [,4]
[1,]  1.000000e+00  2.390341e-15 -1.167469e-15  1.311885e-16
[2,]  5.746272e-17  1.000000e+00 -2.270319e-16  1.114018e-16
[3,] -2.550044e-16 -1.047339e-16  1.000000e+00 -1.275022e-16
[4,]  5.872039e-16 -1.514197e-15 -7.502679e-17  1.000000e+00


Data Exploration

平均值:mean(x1)

求和:sum(x1)

最大值:max(x1)

最小值:min(x1)

方差:var(x1)

标准差:sd(x1)

累乘:prod(x1)



公差向量

> seq(5, 10, by=2)
[1] 5 7 9

> seq(5, 50, length=13)
 [1]  5.00  8.75 12.50 16.25 20.00 23.75 27.50 31.25 35.00 38.75 42.50 46.25
[13] 50.00

字母向量

> letters
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r"
[19] "s" "t" "u" "v" "w" "x" "y" "z"

> letters[1:4]
[1] "a" "b" "c" "d"

> letters[1:30]
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r"
[19] "s" "t" "u" "v" "w" "x" "y" "z" NA  NA  NA  NA

下标函数:which()

> a=c(2,3,4,5,6,7,2,3,4,8,9,5)

> which.max(a)
[1] 11

> which(a==2)
[1] 1 7

> which(a>3)
[1]  3  4  5  6  9 10 11 12

> a[which(a>3)]
[1] 4 5 6 7 4 8 9 5

向量排序:rev(x), sort(x)



线性方程组:Linear Equations

时间: 2024-10-14 19:43:09

R Language的相关文章

String comparison is too slow in R language

## String comparison is too slow in R language ## it will take 3 minutes, it is too slow date() strArray1<-rep("1234567890",10000) strArray2<-rep("1234567890",10000) tt<-0 for(xx in 1:10000) { for(yy in 1:10000) { if(strArray1

R语言入门(二)基础语法

1.help可以提供帮助,如help(nchar), help("[["),或者用?nchar也能获取帮助.example(nchar)可以获取到某个主题的使用方法. 2.nchar获取字符串的长度,nchar(x)返回x中每个字符串的长度,length获取向量的长度,区别如下: > nchar("hello") [1] 5 > nchar(3) [1] 1 > nchar(3000) [1] 4 > help(nchar) > nch

《R in Nutshell》 读书笔记(连载)

R in Nutshell 前言 例子(nutshell包) 本书中的例子包括在nutshell的R包中,使用数据,需加载nutshell包 install.packages("nutshell") 第一部分:基础 第一章 批处理(Batch Mode) R provides a way to run a large set of commands in sequence and save the results to a file. 以batch mode运行R的一种方式是:使用系统

R 统计与作图---入门(一)数据类型

R语言,一种自由软件编程语言与操作环境,主要用于统计分析.绘图.数据挖掘.R本来是由来自新西兰奥克兰大学的Ross Ihaka和Robert Gentleman开发(也因此称为R),现在由“R开发核心团队”负责开发.R是基于S语言的一个GNU计划项目,所以也可以当作S语言的一种实现,通常用S语言编写的代码都可以不作修改的在R环境下运行.R的语法是来自Scheme. R的源代码可自由下载使用,亦有已编译的可执行文件版本可以下载,可在多种平台下运行,包括UNIX(也包括FreeBSD和Linux).

install r on ubuntu

install r-base on ubuntu: sudo apt-get install -y r-base download rstudio: https://download1.rstudio.org/rstudio-1.0.153-amd64.deb install gui tool(rstudio) for r language: sudo dpkg -i rstudio-***-amd64.deb some examples for r language: *** # librar

R语言的cpp扩展支持Rcpp模块介绍

概述 Rcpp包提供C++类方便C or C++代码与R软件包进行交互,使用R中提供的.Call() 调用界面. Rcpp提供R中的基础数据类型的C++类供访问.包作者可以保持R的数据结构而无需与C++进行不断的转换.同时,这些数据结构提供C++级别的存取.数据类型可以双向映射.可以从 R中把数据赋给C++, 返回数据从C++到R也完全一样.下面列出支持的数据类型. Transfer from R to C++, and from C++ to R R 数据类型 (SEXP) 与C++对象是一致

Create and format Word documents using R software and Reporters package

http://www.sthda.com/english/wiki/create-and-format-word-documents-using-r-software-and-reporters-package Install and load the ReporteRs R package Create a simple Word document Add texts : title and paragraphs of texts Format the text of a Word docum

关于机器学习和深度学习的资料

声明:转来的,原文出处:http://blog.csdn.net/achaoluo007/article/details/43564321 编者按:本文收集了百来篇关于机器学习和深度学习的资料,含各种文档,视频,源码等.而且原文也会不定期的更新,望看到文章的朋友能够学到更多. <Brief History of Machine Learning> 介绍:这是一篇介绍机器学习历史的文章,介绍很全面,从感知机.神经网络.决策树.SVM.Adaboost 到随机森林.Deep Learning. &

一起学ASP.NET Core 2.0学习笔记(二): ef core2.0 及mysql provider 、Fluent API相关配置及迁移

不得不说微软的技术迭代还是很快的,上了微软的船就得跟着她走下去,前文一起学ASP.NET Core 2.0学习笔记(一): CentOS下 .net core2 sdk nginx.supervisor.mysql环境搭建搭建好了.net core linux的相关环境,今天就来说说ef core相关的配置及迁移: 简介: Entity Framework(以下简称EF) 是微软以 ADO.NET 为基础所发展出来的对象关系对应 (O/R Mapping) 解决方案,EF Core是Entity