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.frame
s 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.
To change the name of a column in a dataframe, just use a combination of the names() function, indexing, and reassignment.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With