R Operators


ผู้ประกอบการ

ตัวดำเนินการใช้เพื่อดำเนินการกับตัวแปรและค่าต่างๆ

ในตัวอย่างด้านล่าง เราใช้ตัว+ดำเนินการเพื่อเพิ่มค่าสองค่าเข้าด้วยกัน:

ตัวอย่าง

10 + 5

R แบ่งตัวดำเนินการในกลุ่มต่อไปนี้:

  • ตัวดำเนินการเลขคณิต
  • ผู้ประกอบการที่ได้รับมอบหมาย
  • ตัวดำเนินการเปรียบเทียบ
  • ตัวดำเนินการลอจิก
  • ตัวดำเนินการเบ็ดเตล็ด

R ตัวดำเนินการเลขคณิต

ตัวดำเนินการเลขคณิตใช้กับค่าตัวเลขเพื่อดำเนินการทางคณิตศาสตร์ทั่วไป:

Operator Name Example Try it
+ Addition x + y
- Subtraction x - y
* Multiplication x * y
/ Division x / y
^ Exponent x ^ y
%% Modulus (Remainder from division) x %% y
%/% Integer Division x%/%y

R ผู้ดำเนินการมอบหมาย

ตัวดำเนินการมอบหมายใช้เพื่อกำหนดค่าให้กับตัวแปร:

ตัวอย่าง

my_var <- 3

my_var <<- 3

3 -> my_var

3 ->> my_var

my_var # print my_var

หมายเหตุ: <<-เป็นผู้มอบหมายส่วนกลาง คุณจะได้เรียนรู้เพิ่มเติมเกี่ยวกับสิ่งนี้ในบทGlobal Variable

นอกจากนี้ยังสามารถหมุนทิศทางของผู้ดำเนินการมอบหมายได้

x <- 3 เท่ากับ 3 -> x



R ตัวดำเนินการเปรียบเทียบ

ตัวดำเนินการเปรียบเทียบใช้เพื่อเปรียบเทียบสองค่า:

Operator Name Example Try it
== Equal x == y
!= Not equal x != y
> Greater than x > y
< Less than x < y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y

R ตัวดำเนินการลอจิก

ตัวดำเนินการเชิงตรรกะใช้เพื่อรวมคำสั่งเงื่อนไข:

Operator Description
& Element-wise Logical AND operator. It returns TRUE if both elements are TRUE
&& Logical AND operator - Returns TRUE if both statements are TRUE
| Elementwise- Logical OR operator. It returns TRUE if one of the statement is TRUE
|| Logical OR operator. It returns TRUE if one of the statement is TRUE.
! Logical NOT - returns FALSE if statement is TRUE

R ตัวดำเนินการเบ็ดเตล็ด

ตัวดำเนินการเบ็ดเตล็ดใช้เพื่อจัดการข้อมูล:

Operator Description Example
: Creates a series of numbers in a sequence x <- 1:10
%in% Find out if an element belongs to a vector x %in% y
%*% Matrix Multiplication x <- Matrix1 %*% Matrix2

หมายเหตุ:คุณจะได้เรียนรู้เพิ่มเติมเกี่ยวกับการคูณเมทริกซ์และเมทริกซ์ในบทต่อไป