본문 바로가기
etc.

R 데이타 초보 / 벡터 이름 만들기 _ Introduction to R by EDX

by ifnotnow82 2019. 10. 5.

이번엔 각 벡터에 이름을 만들어보자. 

이 강좌에서는 벡터에 이름을 주는 방법을 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.

댓글