官术网_书友最值得收藏!

Working with objects

You can create an object using the <- operator:

n<-10
n
## [1] 10

In the preceding code, an object called n is created. A value of 10 has been assigned to this object. The assignment can also be made using the assign() function, although this isn't very common.

Once the object has been created, it is possible to perform operations on it, like in any other programming language:

n+5
## [1] 15

These are some examples of basic operations.

Let's create our variables:

x<-4
y<-3

Now, we can carry out some basic operations:

  • Sum of variables:
x + y
## [1] 7
  • Subtraction of variables:
x - y
## [1] 1
  • Multiplication of variables:
x * y
## [1] 12
  • Division of variables:
x / y
## [1] 1.333333
  • Power of variables:
x ** y
## [1] 64

Likewise in R, there are defined constants that are widely used, such as the following ones:

  • The pi () number :
x * pi
## [1] 12.56637
  • Exponential function:
exp(y)
## [1] 20.08554

There are also functions for working with numbers, such as the following:

  • Sign (positive or negative of a number):
sign(y)
## [1] 1
  • Finding the maximum value:
max(x,y)
## [1] 4
  • Finding the minimum value:
min(x,y)
## [1] 3
  • Factorial of a number:
factorial(y)
## [1] 6
  • Square root function:
sqrt(y)
## [1] 1.732051

It is also possible to assign the result of previous operations to another object. For example, the sum of variables x and y is assigned to an object named z:

z <- x + y
z
## [1] 7

As shown previously, these functions apply if the variables are numbers, but there are also other operators to work with strings:

x > y
## [1] TRUE
x + y != 8
## [1] TRUE

The main logical operators are summarized in the following table:

主站蜘蛛池模板: 揭东县| 宣威市| 兰考县| 怀宁县| 乾安县| 清新县| 宁安市| 江油市| 锦州市| 惠来县| 罗田县| 西宁市| 德江县| 蚌埠市| 辉县市| 曲松县| 铁力市| 方正县| 梁平县| 尉犁县| 博兴县| 万盛区| 化隆| 鸡东县| 邓州市| 克拉玛依市| 遂川县| 富顺县| 渝北区| 南城县| 天镇县| 南汇区| 五原县| 太原市| 福海县| 银川市| 阿合奇县| 南木林县| 紫阳县| 若尔盖县| 闵行区|