Global Trend Radar
Web: www.datamentor.io US web_search 2026-05-04 04:16

R演算子(例付き)

原題: R Operators (With Examples) - Datamentor

元記事を開く →

分析結果

カテゴリ
AI
重要度
54
トレンドスコア
18
要約
この記事では、Rプログラミング言語における演算子について説明しています。演算子は、データの操作や計算を行うための重要な要素であり、算術演算子、比較演算子、論理演算子などが含まれます。具体的な例を通じて、各演算子の使い方やその効果を学ぶことができます。Rの基本的な構文やデータフレーム、リスト、ヒストグラムの作成方法についても触れています。
キーワード
R Operators (With Examples) Course Index Explore Programiz Python R Popular Tutorials R Operators R if...else Statement R Data Frame R Lists R Histogram Start Learning R Popular Examples R Hello World Program Find Factorial of a Number Add Two Vectors Sample From a Population Sort a Vector Explore R Examples Learning Paths Challenges Learn Python Interactively Try Programiz PRO Courses Learn SQL Course Python Interview Questions Python Recursion Course View all Courses Python R Popular Tutorials R Operators R if...else Statement R Data Frame R Lists R Histogram Start Learning R All R Tutorials R Popular Examples R Hello World Program Find Factorial of a Number Add Two Vectors Sample From a Population Sort a Vector All R Examples R Introduction R Reserved Words R Variables and Constants R Operators R Operator Precedence and Associativitys R Flow Control R if…else Statement R ifelse() Function R for Loop R while Loop R break and next Statement R repeat loop R Functions R Functions R Return Value from Function R Environment and Scope R Recursive Function R Infix Operator R switch() Function R Data Structures R Vector R Matrix R Lists R Data Frame R Factor R Object & Class R Classes and Objects R S3 Class R S4 Class R Reference Class R Graphs & Charts R Bar Plot R Histograms R Pie Chart R Box Plot R Strip Chart R Advanced Topics R Plot Function R Multiple Plots Saving a Plot in R R Plot Color R 3D Plot Related Topics R Operator Precedence and Associativity R Infix Operator R Program to Add Two Vectors R ifelse() Function R Operators In this article, you will learn about different R operators with the help of examples. R has many operators to carry out different mathematical and logical operations. Operators perform tasks including arithmetic, logical and bitwise operations. Type of operators in R Operators in R can mainly be classified into the following categories: Arithmetic Operators Relational Operators Logical Operators Assignment Operators R Arithmetic Operators These operators are used to carry out mathematical operations like addition and multiplication. Here is a list of arithmetic operators available in R. Operator Description + Addition - Subtraction * Multiplication / Division ^ Exponent %% Modulus(Remainder from division) %/% Integer Division Let's look at an example illustrating the use of the above operators: x <- 5 y <- 16 x + y x - y x * y y / x y %/% x y %% x y ^ x Output [1] 21 [1] -11 [1] 80 [1] 3.2 [1] 3 [1] 1 [1] 1048576 R Relational Operators Relational operators are used to compare between values. Here is a list of relational operators available in R. Operator Description Less than > Greater than Less than or equal to >= Greater than or equal to == Equal to != Not equal to Let's see an example for this: x <- 5 y <- 16 x < y x > y x <= 5 y >= 20 y == 16 x != 5 Output [1] TRUE [1] FALSE [1] TRUE [1] FALSE [1] TRUE [1] FALSE Operation on Vectors The above mentioned operators work on vectors . The variables used above were in fact single element vectors. We can use the function c() (as in concatenate) to make vectors in R. All operations are carried out in element-wise fashion. Here is an example. x <- c(2, 8, 3) y <- c(6, 4, 1) x + y x > y Output [1] 8 12 4 [1] FALSE TRUE TRUE When there is a mismatch in length (number of elements) of operand vectors, the elements in the shorter one are recycled in a cyclic manner to match the length of the longer one. R will issue a warning if the length of the longer vector is not an integral multiple of the shorter vector. x <- c(2, 1, 8, 3) y <- c(9, 4) x + y x - 1 x + c(1, 2, 3) Output [1] 11 5 17 7 [1]1 0 7 2 [1] 3 3 11 4 Warning message: In x + c(1, 2, 3) : longer object length is not a multiple of shorter object length R Logical Operators Logical operators are used to carry out Boolean operations like AND , OR etc. Operator Description ! Logical NOT & Element-wise logical AND && Logical AND | Element-wise logical OR || Logical OR Operators & and | perform element-wise operation producing result having length of the longer operand. But && and || examines only the first element of the operands resulting in a single length logical vector. Zero is considered FALSE and non-zero numbers are taken as TRUE . Let's see an example for this: x <- c(TRUE, FALSE, 0, 6) y <- c(FALSE, TRUE, FALSE, TRUE) !x x & y x && y x | y x || y Output [1] FALSE TRUE TRUE FALSE [1] FALSE FALSE FALSE TRUE [1] FALSE [1] TRUE TRUE FALSE TRUE [1] TRUE R Assignment Operators These operators are used to assign values to variables. Operator Description Leftwards assignment ->, ->> Rightwards assignment The operators <- and = can be used, almost interchangeably, to assign to variables in the same environment. The <<- operator is used for assigning to variables in the parent environments (more like global assignments). The rightward assignments, although available, are rarely used. x <- 5 x x <- 9 x 10 -> x x Output [1] 5 [1] 9 [1] 10 Check out these examples to learn more: Add Two Vectors Take Input From User R Multiplication Table Table of Contents Introduction Type of operators in R R Arithmetic Operators R Relational Operators Operation on Vectors R Logical Operators R Assignment Operators Previous Tutorial: R Variables and Constants Next Tutorial: R Operator Precedence and Associativitys Share on: Did you find this article helpful? Sorry about that. How can we improve it? Feedback R Tutorials Programming R Operator Precedence and Associativity Programming R Infix Operator Programming R Program to Add Two Vectors Programming R ifelse() Function

類似記事(ベクトル近傍)