I came across a solution for renaming the column names of a data.frame:
names(data) <- c("new_name", "another_new_name")
Here is an example:
empl <- c("Mike", "Steven") wa <- c(25000, 30000) data <- data.frame(empl, wa) data # now rename the columns of the dataframe names(data) <- c("employee", "wage") data Now I am wondering how it is possible to assign a vector to a function-call. The result of names(data) is a vector with chars. And it seems that this vector is not linked in any way to the data.frame.
Can anyone enlighten me what the mechanisms are?
Trying to explain to myself
names(data) <- c("employee", "wage")
Looking at the assignment above:
- the left hand side
names(data)returns a vector with the old column names. - does this assignment not assign to a vector? Instead of to a dataframe‘s attribute?
f1 <- function(data, nm1) setNames(data, nm1); f1(data, c("employee", "wage"))