Basic Operations Guide in R

Understanding the basics of R, a programming language widely used in statistics and data analysis.

Arithmetic Operations

In R, arithmetic operations like addition, subtraction, multiplication, and division are performed using the symbols +, -, *, and / respectively. Other common operations include exponentiation with ^, modulus with %%, and integer division with %/%, allowing for complex mathematical calculations in a straightforward and efficient manner.

  • Addition
    Use `+` to add numbers.

    3 + 2

    5

  • Subtraction
    Use `-` to subtract numbers.

    5 - 2

    3

  • Multiplication
    Use `*` to multiply numbers.

    4 * 2

    8

  • Division
    Use `/` to divide numbers.

    10 / 2

    5

  • Exponentiation
    Use `^` to raise a number to a power.

    3^2

    9

  • Modulus
    Use `%%` to find the remainder of a division, denoted by a sequence of two "%" symbols.

    5 %% 2

    1

  • Integer Division
    Use `%/%` to find the quotient of a division, with the division symbol "/" between two "%" symbols.

    5 %/% 2

    2

Logical Operations

Logical operations in R are used for comparing values and producing Boolean results (TRUE or FALSE). They are crucial for controlling program flow, allowing code execution based on specific conditions and criteria.

  • Greater Than
    Use `>` to check if one number is greater than another.

    3 > 2

    TRUE

  • Less Than
    Use `<` to check if one number is less than another.

    3 < 2

    FALSE

  • Equal To
    Use `==` to verify equality.

    3 == 3

    TRUE

  • Not Equal To
    Use `!=` to check for inequality.

    3 != 2

    TRUE

  • Greater Than or Equal To
    Use `>=` to verify if a number is greater than or equal to another.

    3 >= 2

    TRUE

  • Less Than or Equal To
    Use `<=` to check if a number is less than or equal to another.

    3 <= 4

    TRUE

  • Logical AND
    Use `&` to check if both conditions are true.

    (3 > 2) & (2 > 1)

    TRUE

  • Logical OR
    Use `|` to check if at least one of the two conditions is true.

    (3 > 2) | (2 < 1)

    TRUE

  • Logical NOT
    Use `!` to invert the truth value.

    !(3 > 2)

    FALSE

Remember, in R it's important to understand the context in which these operations are performed, especially when working with vectors or matrices.

Each operation can behave differently depending on the data structure you are working with.




Report a mistake or post a question




FacebookTwitterLinkedinLinkedin