Hello everyone,
I have a question about the default order of observations in a data.frame in R. Of course functions such as sort and arrange allow to retrieve observations based on the desired criteria. Yet, I would like to undersntand how the internal structures of dataframes in R handle by default the order of observations once the dataframe is being queried to read data. To clarify what I mean exactly, I prefer to compare dataframes in R to ordinary tables in classic relational databases such as Oracle, PostgreSQL, etc. to show better what I intend to understand. Here is what Oracle online documentation says about the default order of rows (= observations in R) once they have been inserted into a table ( = for me that is data.frame in R):
By default, a table is organized as a heap, which means that the database places rows where they fit best rather than in a user-specified order. Thus, a heap-organized table is an unordered collection of rows.
As users add rows, the database places the rows in the first available free space in the data segment. Rows are not guaranteed to be retrieved in the order in which they were inserted.
Therefore, this means for example if you insert the following CSV file into a table :
Data.txt
line-01
line-02
line-03
line-04
line-05
Then there is no guarantee that
select * from mytable
returns the rows exactly based on the order as they appear in the data file Data.txt unless an appropriate ORDER BY clause has been specified. So my question is that whether the same concept exists for dataframes in R. If you import data from a CSV file into a dataframe and you interrogate the data without using any kind of sort, arrange, etc. will the data be returned exactly based on the very same order as they were in the CSV data file and does a dataframe maintain this order unless the user specifies differently via sort, arrange. etc. ?
I checked the manual of data.frame but I didn't find anything in this regard. Therefore, I would appreciate if you could kindly make some clarification.
Thanks in advance