What is the easiest way to assign names to a vector? Explain with an example

943    Asked by OkamotoBando in Data Science , Asked on Nov 3, 2019
Answered by Nitin Solanki

In R, names() function is used to assign names to each element in a vector. Let us assign a vector named temps which contains temperature of seven days.

temps <- c(72,71,68,73,69,75,71)

Now we do not know the temperature corresponds to which weekdays. For that, we can assign names() function to assign weekdays to the temperature

names(temps) <- c('Mon','Tue','Wed','Thu','Fri','Sat','Sun')

Now we can see the following output

temps

Mon 72

Tue 71

Wed 68

Thu 73

Fri 69

Sat 75

Sun 71



Your Answer

Interviews

Parent Categories