r语言2次方怎么表示

Python060

r语言2次方怎么表示,第1张

pow(r,2.0)即可。

r的2次方写成pow(r,2.0)即可,如果r不是double型,写成pow((double)r,2.0)即可。

R语言二进制文件

二进制文件是一个文件,其中包含仅以位和字节形式存储的信息(0和1)。它们不可读,因为其中的字节转换为包含许多其他不可打印字符的字符和符号。尝试使用任何文本编辑器读取二进制文件将显示为类似?和?这样的字符。

二进制文件必须由特定程序读取才能使用。例如,Microsoft Word程序的二进制文件只能通过Word程序读取到人类可读的形式。这表明,除了人类可读的文本之外,还有更多的信息,如格式化的字符和页码等,它们也与字母数字字符一起存储。最后二进制文件是一个连续的字节序列。 我们在文本文件中看到的换行符是将第一行连接到下一个的字符。

有时,由其他程序生成的数据需要由R作为二进制文件处理。 另外R需要创建可以与其他程序共享的二进制文件。

R有两个函数用来创建和读取二进制文件,它们分别是:WriteBin()和readBin()函数。

语法

writeBin(object,con)readBin(con,what,n)

R

以下是使用的参数的描述 -

con- 是要读取或写入二进制文件的连接对象。

object- 是要写入的二进制文件。

what- 是像字符,整数等的模式,代表要读取的字节。

n- 是从二进制文件读取的字节数。

实例

这里考虑使用R内置数据“mtcars”。 首先,我们从它创建一个csv文件并将其转换为二进制文件并将其存储为操作系统文件。接下来将这个二进制文件读入R中。

1. 写入二进制文件

我们将数据帧“mtcars”读为csv文件,然后将其作为二进制文件写入操作系统。参考以下代码实现 -

# Read the "mtcars" data frame as a csv file and store only the columns"cyl","am"and"gear".write.table(mtcars,file="mtcars.csv",row.names=FALSE,na="",col.names=TRUE,sep=",")# Store 5 records from the csv file as a new data frame.new.mtcars<-read.table("mtcars.csv",sep=",",header=TRUE,nrows=5)# Create a connection object to write the binary file using mode "wb".write.filename=file("/web/com/binmtcars.dat","wb")# Write the column names of the data frame to the connection object.writeBin(colnames(new.mtcars),write.filename)# Write the records in each of the column to the file.writeBin(c(new.mtcars$cyl,new.mtcars$am,new.mtcars$gear),write.filename)# Close the file for writing so that it can be read by other program.close(write.filename)

R2. 读取二进制文件

上面创建的二进制文件将所有数据作为连续字节存储。 因此,我们将通过选择列名称和列值的适当值来读取它。

# Create a connection object to read the file in binary mode using "rb".read.filename<-file("/web/com/binmtcars.dat","rb")# First read the column names. n = 3 as we have 3 columns.column.names<-readBin(read.filename,character(),n=3)# Next read the column values. n = 18 as we have 3 column names and 15 values.read.filename<-file("/web/com/binmtcars.dat","rb")bindata<-readBin(read.filename,integer(),n=18)# Print the data.print(bindata)# Read the values from 4th byte to 8th byte which represents "cyl".cyldata=bindata[4:8]print(cyldata)# Read the values form 9th byte to 13th byte which represents "am".amdata=bindata[9:13]print(amdata)# Read the values form 9th byte to 13th byte which represents "gear".geardata=bindata[14:18]print(geardata)# Combine all the read values to a dat frame.finaldata=cbind(cyldata,amdata,geardata)colnames(finaldata)=column.namesprint(finaldata)

R

当我们执行上面的代码,它产生以下结果和图表 -

[1]7108963 17280812497496037 6 6 4 [7] 6 8 1 1 1 0[13] 0 4 4 4 3 3[1] 6 6 4 6 8[1] 1 1 1 0 0[1] 4 4 4 3 3 cyl am gear[1,] 6 14[2,] 6 14[3,] 4 14[4,] 6 03[5,] 8 03

Shell

我们可以看到,通过读取R中的二进制文件,得到了原始数据。

运算符是一个符号,通知编译器执行特定的数学或逻辑操作。 R语言具有丰富的内置运算符,并提供以下类型的运算符。

运算符的类型

R语言中拥有如下几种运算符类型:

算术运算符

关系运算符

逻辑运算符

赋值运算符

其他运算符

算术运算符

下表显示了R语言支持的算术运算符。 操作符对向量的每个元素起作用。

运算符 描述 例

关系运算符

下表显示了R语言支持的关系运算符。 将第一向量的每个元素与第二向量的相应元素进行比较。 比较的结果是布尔值。

运算符 描述 例

逻辑运算符

下表显示了R语言支持的逻辑运算符。 它只适用于逻辑,数字或复杂类型的向量。 所有大于1的数字被认为是逻辑值TRUE。

将第一向量的每个元素与第二向量的相应元素进行比较。 比较的结果是布尔值。

运算符 描述 例

& 它被称为元素逻辑AND运算符。 它将第一向量的每个元素与第二向量的相应元素组合,并且如果两个元素都为TRUE,则给出输出TRUE。

v <- c(3,1,TRUE,2+3i)

t <- c(4,1,FALSE,2+3i)

print(v&t)

它产生以下结果 -

TRUE TRUE FALSE TRUE

| 它被称为元素逻辑或运算符。 它将第一向量的每个元素与第二向量的相应元素组合,并且如果元素为真,则给出输出TRUE。

v <- c(3,0,TRUE,2+2i)

t <- c(4,0,FALSE,2+3i)

print(v|t)

它产生以下结果 -

TRUE FALSE TRUE TRUE

! 它被称为逻辑非运算符。 取得向量的每个元素,并给出相反的逻辑值。

v <- c(3,0,TRUE,2+2i)

print(!v)

它产生以下结果 -

FALSE TRUE FALSE FALSE

逻辑运算符&&和|| 只考虑向量的第一个元素,给出单个元素的向量作为输出。

运算符 描述 例

&& 称为逻辑AND运算符。 取两个向量的第一个元素,并且只有两个都为TRUE时才给出TRUE。

v <- c(3,0,TRUE,2+2i)

t <- c(1,3,TRUE,2+3i)

print(v&&t)

它产生以下结果 -

TRUE

|| 称为逻辑OR运算符。 取两个向量的第一个元素,如果其中一个为TRUE,则给出TRUE。

v <- c(0,0,TRUE,2+2i)

t <- c(0,3,TRUE,2+3i)

print(v||t)

它产生以下结果 -

FALSE

赋值运算符

这些运算符用于向向量赋值。

运算符 描述 例

<−

or

=

or

<<−

称为左分配

v1 <- c(3,1,TRUE,2+3i)

v2 <<- c(3,1,TRUE,2+3i)

v3 = c(3,1,TRUE,2+3i)

print(v1)

print(v2)

print(v3)

它产生以下结果 -

3+0i 1+0i 1+0i 2+3i

3+0i 1+0i 1+0i 2+3i

3+0i 1+0i 1+0i 2+3i

->

or

->>

称为右分配

c(3,1,TRUE,2+3i) ->v1

c(3,1,TRUE,2+3i) ->>v2

print(v1)

print(v2)

它产生以下结果 -

3+0i 1+0i 1+0i 2+3i

3+0i 1+0i 1+0i 2+3i

其他运算符

这些运算符用于特定目的,而不是一般的数学或逻辑计算。

运算符 描述 例

: 冒号运算符。 它为向量按顺序创建一系列数字。

v <- 2:8

print(v)

它产生以下结果 -

2 3 4 5 6 7 8

%in%此运算符用于标识元素是否属于向量。

v1 <- 8

v2 <- 12

t <- 1:10

print(v1 %in% t)

print(v2 %in% t)

它产生以下结果 -

TRUE

FALSE

% % 此运算符用于将矩阵与其转置相乘。

M = matrix( c(2,6,5,1,10,4), nrow = 2,ncol = 3,byrow = TRUE)

t = M % % t(M)

print(t)

它产生以下结果 -

[,1] [,2]

[1,] 65 82

[2,] 82 117