Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between names and colnames

Tags:

dataframe

r

names

I just want to understand if there is a difference between names and colnames when working with data.frame. Both seems to behave the same way. Can I subsitute one by the other?

like image 425
Manu H Avatar asked Jul 17 '14 09:07

Manu H


People also ask

What is Colnames?

colnames() function in R Language is used to set the names to columns of a matrix. Syntax: colnames(x) <- value.

What does Colnames mean in R?

The rownames() and colnames() functions in R are used to obtain or set the names of the row and column of a matrix-like object, respectively.

How do I use Colnames?

Method 1: using colnames() method colnames() method in R is used to rename and replace the column names of the data frame in R. The columns of the data frame can be renamed by specifying the new column names as a vector. The new name replaces the corresponding old name of the column in the data frame.

How do I get Colnames in R?

To access a specific column in a dataframe by name, you use the $ operator in the form df$name where df is the name of the dataframe, and name is the name of the column you are interested in. This operation will then return the column you want as a vector.


1 Answers

Are they the same for data.frames? YES

Are they the same in general? Not quite--the big difference is that colnames also works for matrices, whereas names does not (just dataframes).

In addition, you can use names to set/get the names of vectors (and, for obvious reasons, you can't do this with colnames--the result is NULL for getting and an error for setting).

like image 148
Steve S Avatar answered Oct 09 '22 13:10

Steve S