이번엔 각 벡터에 이름을 만들어보자.
이 강좌에서는 벡터에 이름을 주는 방법을 2가지 정도 가르쳐주고 있다.
처음엔 약간 헷갈렸는데, 역시 연습을 몇번해보니 쉽다.
Naming a vector (1)
Go ahead and assign the days of the week as names to poker_vector and roulette_vector. In case you are not sure, the days of the week are: Monday, Tuesday, Wednesday, Thursday and Friday.
# Poker winnings from Monday to Friday
poker_vector <- c(140, -50, 20, -120, 240)
# Roulette winnings from Monday to Friday
roulette_vector <- c(-24, -50, 100, -350, 10)
# Add names to both poker_vector and roulette_vector
names(poker_vector) <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
names(roulette_vector) <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
Naming a vector (2)
Create a variable days_vector that contains the days of the week, from Monday to Friday. Use that variable days_vector to set the names of poker_vector and roulette_vector.
# Poker winnings from Monday to Friday
poker_vector <- c(140, -50, 20, -120, 240)
# Roulette winnings from Monday to Friday
roulette_vector <- c(-24, -50, 100, -350, 10)
# Create the variable days_vector
days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
# Assign the names of the day to roulette_vector and poker_vector
names(poker_vector) <- days_vector
names(roulette_vector) <- days_vector
Different ways to create and name vectors
The previous exercises outlined different ways of creating and naming vectors. Have a look at this chunk of code:
poker_vector1 <- c(140, -50, 20, -120, 240)
names(poker_vector1) <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
poker_vector2 <- c(Monday = 140, -50, 20, -120, 240)
roulette_vector1 <- c(-24, -50, 100, -350, 10)
days_vector <- names(poker_vector1)
names(roulette_vector1) <- days_vector
roulette_vector2 <- c(-24, -50, 100, -350, 10)
names(roulette_vector2) <- "Monday"
Which of the following statements is true?
poker_vector1 and roulette_vector1 have the same names, while poker_vector2 and roulette_vector2 show a names mismatch.
'etc.' 카테고리의 다른 글
사람들은 왜 '럼블(Rumble)'로 이동하는가 (0) | 2020.12.11 |
---|---|
R 데이터 초보 / 벡터의 연산(덧셈, 뺄셈) _ Introduction to R by EDX (0) | 2019.10.07 |
R 데이터 초보 / 벡터만들기 _ Introduction to R by EDX (0) | 2019.10.02 |
모든 것을 바꾸는 한 가지, 신뢰 - 줄거리 (0) | 2019.09.24 |
삼분위 수 Tertiles (0) | 2019.09.19 |
댓글