r语言P=p%*%p是什么意思

Python017

r语言P=p%*%p是什么意思,第1张

%*%表示通常意义下的两个矩阵的乘积。而矩阵乘积只有在第一个矩阵的列数(column)和第二个矩阵的行数(row)相同时才有意义。

x+y加法

x-y减法

x*y乘法

x/y除法

x^y乘幂

x%%y模运算

x%/%y整数除法

x==y判断是否相等

x <= y判断是否小于等于

x >= y 判断是否大于等于

x &&y 标量的逻辑“与”运算

x||y标量的逻辑“或”运算

x &y 向量的逻辑“与”运算(x、y以及运算结果都是向量)                                 

X|y的逻辑“或”运算(x、y以及运算结果都是向量)

!x逻辑非

扩展资料

R语言表面上没有标量的类型,因为标量可以看作是含有一个元素的向量,但我们看到表中,逻辑运算符对标量和向量有着不同的形式,这虽然看起来很奇怪,但这种区别很有必要。

>x <- c( TRUE, FALSE, TRUE )

>y <- c( TRUE, TRUE, FALSE )

>x &y

[1]TRUEFALSEFALSE

>x[1] &&y[1]

[1]TRUE

>x &&y

[1]TRUE

>if( x[1] &&y[1] )   print( "both TRUE" )

[1]"bothTRUE"

>if( x &y )   print( "both TRUE" )

[1]"bothTRUE"

Sys.Date( ) returns today's date.

date() returns the current date and time.

# print today's date

today <-Sys.Date()

format(today, format="%B %d %Y")

"June 20 2007"

# convert date info in format 'mm/dd/yyyy'

strDates <- c("01/05/1965", "08/16/1975")

dates <- as.Date(strDates, "%m/%d/%Y")

# convert dates to character data

strDates <- as.character(dates)

--------------------------------------

>as.Date('1915-6-16')

[1] "1915-06-16"

>as.Date('1990/02/17')

[1] "1990-02-17"

>as.Date('1/15/2001',format='%m/%d/%Y')

[1] "2001-01-15"

>as.Date('April 26, 2001',format='%B %d, %Y')

[1] "2001-04-26"

>as.Date('22JUN01',format='%d%b%y') # %y is system-specificuse with caution

[1] "2001-06-22"

>bdays = c(tukey=as.Date('1915-06-16'),fisher=as.Date('1890-02-17'),

+ cramer=as.Date('1893-09-25'), kendall=as.Date('1907-09-06'))

>weekdays(bdays)

tukey fisher cramer kendall

"Wednesday""Monday""Monday""Friday"

>dtimes = c("2002-06-09 12:45:40","2003-01-29 09:30:40",

+"2002-09-04 16:45:40","2002-11-13 20:00:40",

+"2002-07-07 17:30:40")

>dtparts = t(as.data.frame(strsplit(dtimes,' ')))

>row.names(dtparts) = NULL

>thetimes = chron(dates=dtparts[,1],times=dtparts[,2],

+ format=c('y-m-d','h:m:s'))

>thetimes

[1] (02-06-09 12:45:40) (03-01-29 09:30:40) (02-09-04 16:45:40)

[4] (02-11-13 20:00:40) (02-07-07 17:30:40)

>dts = c("2005-10-21 18:47:22","2005-12-24 16:39:58",

+ "2005-10-28 07:30:05 PDT")

>as.POSIXlt(dts)

[1] "2005-10-21 18:47:22" "2005-12-24 16:39:58"

[3] "2005-10-28 07:30:05"

>dts = c(1127056501,1104295502,1129233601,1113547501,

+ 1119826801,1132519502,1125298801,1113289201)

>mydates = dts

>class(mydates) = c('POSIXt','POSIXct')

>mydates

[1] "2005-09-18 08:15:01 PDT" "2004-12-28 20:45:02 PST"

[3] "2005-10-13 13:00:01 PDT" "2005-04-14 23:45:01 PDT"

[5] "2005-06-26 16:00:01 PDT" "2005-11-20 12:45:02 PST"

[7] "2005-08-29 00:00:01 PDT" "2005-04-12 00:00:01 PDT"

>mydate = strptime('16/Oct/2005:07:51:00',format='%d/%b/%Y:%H:%M:%S')

[1] "2005-10-16 07:51:00"

>ISOdate(2005,10,21,18,47,22,tz="PDT")

[1] "2005-10-21 18:47:22 PDT"

>thedate = ISOdate(2005,10,21,18,47,22,tz="PDT")

>format(thedate,'%A, %B %d, %Y %H:%M:%S')

[1] "Friday, October 21, 2005 18:47:22"

>mydate = as.POSIXlt('2005-4-19 7:01:00')

>names(mydate)

[1] "sec" "min" "hour" "mday" "mon" "year"

[7] "wday" "yday" "isdst"

>mydate$mday

[1] 19

1、先在R中创建简单的矩阵,取名为my_matrix

2、现在对每一行进行求和,要使用到apply函数。apply族函数有很多,在平常的使用中,用到最多的就是tapply函数,第一个参数x是数据;第二个参数index是索引,就比如是分组的标准,第三个参数fun也就是要用到的函数。

3、对于tapply函数调用R本身自带的数据airquality,现在计算按照Month来分组,每一个Month中Solar.R的均值

4、上图中Month等于5和8时,Solar.R的均值为NA,这说明在原数据中存在NA的情况。而且在指定数据的时候比较麻烦,可以使用with函数来简化代码量

5、矩阵与矩阵之间的运算,比如加法,减法

6、矩阵相乘需要使用%*%符号

R是用于统计分析、绘图的语言和操作环境。R是属于GNU系统的一个自由、免费、源代码开放的软件,它是一个用于统计计算和统计制图的优秀工具。