Table of Contents
Function names in R are used to call specific actions or operations within the R programming language. These names are typically used in conjunction with parentheses and arguments to specify the desired outcome. Some common examples of using function names in R include creating plots with the “plot()” function, performing mathematical calculations with the “sum()” or “mean()” functions, and sorting data with the “sort()” function. Other examples include manipulating strings with the “paste()” function, generating random numbers with the “runif()” function, and loading external packages with the “library()” function. These function names provide a standardized and efficient way to execute tasks in R, making it a versatile and powerful tool for data analysis and statistical computing.
Use the names Function in R (3 Examples)
You can use the names() function to set the names of an object or get the names of an object in R.
This function uses the following syntax:
#get names of object
names(x)
#set names of object
names(x) <- c('value1', 'value2', 'value3', ...)
The following examples show how to use the names() function with different objects.
Example 1: Use names() Function with Vector
We can use the names() function to set the names for a vector:
#create vector
my_vector <- c(5, 10, 15, 20, 25)
#view vector
my_vector
[1] 5 10 15 20 25
#set names for vector
names(my_vector) <- c('A', 'B', 'C', 'D', 'E')
#view updated vector
my_vector
A B C D E
5 10 15 20 25 We can then use brackets to access the values in a vector based off the name:
#access value in vector that corresponds to 'B' name
my_vector['B']
B
10
Example 2: Use names() Function with List
We can use the names() function to set the names for a list:
#create list
my_list <- list(c(1, 2, 3), 'hello', 10)
#view list
my_list
[[1]]
[1] 1 2 3
[[2]]
[1] "hello"
[[3]]
[1] 10
#set names for list
names(my_list) <- c('A', 'B', 'C')
#view updated list
my_list
$A
[1] 1 2 3
$B
[1] "hello"
$C
[1] 10We can then use brackets to access the values in a list based off the name:
#access value in list that corresponds to 'C' name
my_list['C']
$C
[1] 10Example 3: Use names() Function with Data Frame
We can use the names() function to set the names for the columns of a data frame:
#create data frame
df <- data.frame(A=c('A', 'B', 'C', 'D', 'E'),
B=c(99, 90, 86, 88, 95),
C=c(33, 28, 31, 39, 34),
D=c(30, 28, 24, 24, 28))
#get names of data frame
names(df)
[1] "A" "B" "C" "D"
#set names of data frame
names(df) <- c('team', 'points', 'assists', 'rebounds')
#view updated names of data frame
names(df)
[1] "team" "points" "assists" "rebounds"
Additional Resources
The following tutorials explain how to perform other common tasks in R:
Cite this article
stats writer (2024). What are some examples of using the function names in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/what-are-some-examples-of-using-the-function-names-in-r/
stats writer. "What are some examples of using the function names in R?." PSYCHOLOGICAL SCALES, 29 Jun. 2024, https://scales.arabpsychology.com/stats/what-are-some-examples-of-using-the-function-names-in-r/.
stats writer. "What are some examples of using the function names in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/what-are-some-examples-of-using-the-function-names-in-r/.
stats writer (2024) 'What are some examples of using the function names in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/what-are-some-examples-of-using-the-function-names-in-r/.
[1] stats writer, "What are some examples of using the function names in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. What are some examples of using the function names in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
