names() Function in R with Examples

The names() function in R is used to get or set the name of an object. The object can be a list, vector, matrix, and DataFrame. Use names(x) to get the name of the object and names(x) <- vector to assign names to the object. To remove the object name assign the value NULL.

When setting a name to the vector, list, or DataFrame make sure you have the character vector of the same length as x (x being the object you are setting name to).

1. Quick Examples of names() Function

Following are quick examples of names()


# Create a vector
vec <- c(10,30,50,70,90)

# Set name to Vector
names(vec) <- c('a','b','c','d','e')

# Get name of vector
names(vec)

# Remove vector name
names(vec) <- NULL

# Create list object
li <- list('Spark','R','Java','Python')

# Assign name to the list
names(li) <- c('AA','AB','AC','AD')

# Get name of the list object
names(li)

#Change names of the DataFrame
names(df) <- c('A','B','C','D','E')

2. Syntax of names()

Following is the syntax of the names() Function.


# Get name
names(x)

# Set name
names(x) <- value

Here,

  • x is the name of an object in R
  • value is a character vector of up to the same length as x, or NULL.

3. Set & Get name from the R Vector

To set the name of an object or get the name of an object or even to remove the name use the names() function in R. The object can be a list, vector, matrix, and DataFrame. Use names(x) to get the name of the object and names(x) <- vector to assign names to the object. To remove the object name assign the value NULL.

3.1 Set Name of Vector

Let’s create a vector using the combine function. In the below example, vector I created doesn’t have names for the elements and I will assign names to the vector. c() is a combined function that I am using to create a character vector.


# Create a vector
vec <- c(10,30,50,70,90)
print(vec)

# Output
# [1] 10 30 50 70 90

# Set name to Vector
names(vec) <- c('a','b','c','d','e')

3.2 Get Name of Vector

Use names(vec) to get the name of the vector in R.


# Get name of vector
names(vec)

# Output
# [1] "a" "b" "c" "d" "e"

# Print vector
print(vec)

# Output
# a  b  c  d  e 
#10 30 50 70 90 

3.3 Remove Vector Name

To remove the name of the vector or any object assign NULL to it.


# Remove vector name
names(vec) <- NULL
names(vec)
print(vec)

# Output
# > names(vec)
# NULL
# > print(vec)
# [1] 10 30 50 70 90

4. Set & Get name from the R List

You can also set name to the list object, get name of the list object and finally remove name from the list object. Let’s see an example.


# Create list object
li <- list('Spark','R','Java','Python')

# Assign name to the list
names(li) <- c('AA','AB','AC','AD')

# Get name of the list object
names(li)

# Print list to console
print(li)

I will leave this to you to run and explore the output.

5. Set & Get names from R DataFrame

You can also use names in R to change the column names of the DataFrame. If you already have a column name, you can use this to replace the column names of the DataFrame.


# Create dataframe
df=data.frame(id=c(11,22,33,44,55),
              pages=c(32,45,33,22,56),
              name=c("spark","python","R","java","jsp"),
              chapters=c(76,86,11,15,7),
              price=c(144,553,321,567,890))
df

#change names of the dataframe
names(df) <- c('A','B','C','D','E')
df

Yields below output.

names in r

Conclusion

In this article, you have learned how to get or set a name to the object by using R names() function. The object can be a list, vector, matrix, and DataFrame. Use names(x) to get the name of the object and names(x) <- vector to assign names to the object. To remove the object name assign the value NULL.

Related Articles

References

NNK

SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..

Leave a Reply