Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange behaviour for data.frames without column names

Tags:

dataframe

r

There is an unexpected behaviour for data.frames without column names. The following works as expected:

df <- data.frame(a = 1:5, b = 5:9)
df + 1
##   a  b
## 1 2  6
## 2 3  7
## 3 4  8

but if we remove the column names then the behaviour is strange:

names(df) <- NULL
df + 1
## data frame with 0 columns and 0 rows

The same is happening if the names are removed with unname, setNames. Any ideas of why this happens and is it (for some reason) expected behaviour?

Edit: So it is documented that nameless data.frames have unsupported results (thanks @neilfws, @Suren) but I am also interested to the why this happens. I try to find the actual c (?) code that makes this simple example to brake.

like image 282
alko989 Avatar asked Apr 17 '18 09:04

alko989


People also ask

What is the function to set column names for a data frame?

To change the name of a column in a dataframe, just use a combination of the names() function, indexing, and reassignment.

What is data frame manipulation?

Data Frame is a two-dimensional structured entity consisting of rows and columns. It consists equal length vectors as rows. The data is stored in cells which are accessed by specifying the corresponding [row, col] set of values of the data frame.

What are data frames in R and list down its characteristics?

Data Frames in R Language are generic data objects of R which are used to store the tabular data. Data frames can also be interpreted as matrices where each column of a matrix can be of the different data types. DataFrame is made up of three principal components, the data, rows, and columns.


1 Answers

In the documentation for data.frame, it says:

The column names should be non-empty, and attempts to use empty names will have unsupported results.

So, it is expected that outcome may not be the desired ones if the column names are empty.

like image 59
Suren Avatar answered Nov 15 '22 14:11

Suren