Course notes: Introduction to R
  • AI Chat
  • Code
  • Report
  • Beta
    Spinner

    Take Notes

    Add notes here about the concepts you've learned and code cells with code you want to keep.

    작동 방식

    오른쪽 편집기에는 연습 문제를 해결하기 위해 R 코드를 입력해야 합니다. '답변 제출' 버튼을 누르면, 각 코드 줄이 R에 의해 해석되고 실행되며, 코드가 올바른지 여부에 대한 메시지를 받게 됩니다. R 코드의 출력은 오른쪽 하단 콘솔에 표시됩니다.

    R은 주석을 추가하기 위해 # 기호를 사용합니다. 이렇게 하면 여러분과 다른 사람들이 R 코드의 내용을 이해할 수 있습니다. 마치 트위터처럼 말이죠! 주석은 R 코드로 실행되지 않으므로 결과에 영향을 주지 않습니다. 예를 들어, 오른쪽 편집기에서 3 + 4 계산은 주석입니다.

    또한 콘솔에서 직접 R 명령을 실행할 수도 있습니다. 이는 R 코드를 실험하는 좋은 방법입니다. 왜냐하면 제출한 내용이 올바른지 확인되지 않기 때문이죠.

    Add your notes here

    # Add your code snippets here
    # Calculate 3 + 4
    3 + 4
    
    # Calculate 6 + 12
    6 + 12
    

    Arithmetic with R

    In its most basic form, R can be used as a simple calculator. Consider the following arithmetic operators:

    • Addition: +
    • Subtraction: -
    • Multiplication: *
    • Division: /
    • Exponentiation: ^
    • Modulo: %%

    The last two might need some explaining:

    • The ^ operator raises the number to its left to the power of the number to its right: for example 3^2 is 9.
    • The modulo returns the remainder of the division of the number to the left by the number on its right, for example 5 modulo 3 or 5 %% 3 is 2.

    With this knowledge, follow the instructions to complete the exercise.

    7%%4
    # An addition
    5 + 5 
    
    # A subtraction
    5 - 5 
    
    # A multiplication
    3 * 5
    
     # A division
    (5 + 5) / 2 
    
    # Exponentiation
    3^3
    
    # Modulo
    7%%4

    Variable assignment

    A basic concept in (statistical) programming is called a variable.

    A variable allows you to store a value (e.g. 4) or an object (e.g. a function description) in R. You can then later use this variable's name to easily access the value or the object that is stored within this variable.

    You can assign a value 4 to a variable my_var with the command

    my_var <- 4
    # Assign the value 42 to x
    x <- 56
    
    # Print out the value of the variable x
    x

    Variable assignment (2)

    Suppose you have a fruit basket with five apples. As a data analyst in training, you want to store the number of apples in a variable with the name my_apples.

    # Assign the value 5 to the variable my_apples
    my_apples <- 5
    
    # Print out the value of the variable my_apples
    my_apples

    Variable assignment (3)

    Every tasty fruit basket needs oranges, so you decide to add six oranges. As a data analyst, your reflex is to immediately create the variable my_oranges and assign the value 6 to it. Next, you want to calculate how many pieces of fruit you have in total. Since you have given meaningful names to these values, you can now code this in a clear way:

    my_apples + my_oranges
    # Assign a value to the variables my_apples and my_oranges
    my_apples <- 5
    
    
    # Add these two variables together
    my_oranges <- 3
    
    # Create the variable my_fruit
    my_fruit <- my_apples + my_oranges
    minus_furit <- my_apples - my_oranges
    mul_fruit <- my_apples * my_oranges
    div_fruit <- my_apples / my_oranges
    
    my_fruit
    minus_furit
    mul_fruit
    div_fruit

    Apples and oranges

    Common knowledge tells you not to add apples and oranges. But hey, that is what you just did, no :-)? The my_apples and my_oranges variables both contained a number in the previous exercise. The + operator works with numeric variables in R. If you really tried to add "apples" and "oranges", and assigned a text value to the variable my_oranges (see the editor), you would be trying to assign the addition of a numeric and a character variable to the variable my_fruit. This is not possible.

    룰렛의 승리/패배를 변수 roulette_vector에 할당하세요. $24를 잃은 후에 $50을 잃었으며, $100을 얻었고, $350을 잃었으며, $10을 얻었습니다.