Come elencare i file per data in r (con esempio)


È possibile utilizzare la seguente sintassi di base per elencare i file nella directory di lavoro corrente in R per data:

 #extract all CSV files in working directory
file_info = file. info ( list.files (pattern=" *.csv "))

#sort files based on mtime (date and time modification)
file_info = file_info[ with (file_info, order (as. POSIXct (mtime))), ]

#view only file names with modification date and time
file_info[c(" mtime ")]

L’esempio seguente mostra come utilizzare questa sintassi nella pratica.

Esempio: come elencare i file per data in R

Supponiamo di voler elencare tutti i file CSV nella directory di lavoro corrente in R per data.

Posso utilizzare la seguente sintassi per estrarre prima tutti i file CSV (con le informazioni sui file) dalla directory di lavoro corrente:

 #extract all CSV files in working directory
file_info = file. info ( list.files (pattern=" *.csv "))

#view all CSV files
file_info

                        size isdir mode mtime ctime atime exe
basketball_data.csv 55 FALSE 666 2023-01-06 11:07:43 2022-07-12 09:07:26 2023-04-18 09:42:19 no
df1.csv 126 FALSE 666 2022-04-21 10:48:24 2022-04-21 10:48:24 2023-04-18 09:42:19 no
df2.csv 126 FALSE 666 2022-04-21 10:48:30 2022-04-21 10:48:29 2023-04-18 09:42:19 no
df3.csv 126 FALSE 666 2022-04-21 10:48:34 2022-04-21 10:48:34 2023-04-18 09:42:19 no
my_data.csv 53 FALSE 666 2022-09-09 09:02:21 2022-04-22 09:00:13 2023-04-18 09:42:19 no
my_list.csv 90 FALSE 666 2022-04-21 09:40:01 2022-04-21 09:39:59 2023-04-18 09:42:19 no
my_test.csv 146 FALSE 666 2022-04-21 09:42:25 2022-04-21 09:42:25 2023-04-18 09:42:19 no
player_stats.csv 137 FALSE 666 2023-04-11 09:07:20 2023-04-11 09:07:20 2023-04-18 09:42:19 no
players_data.csv 50 FALSE 666 2023-01-06 09:44:12 2023-01-06 09:44:12 2023-04-18 09:42:19 no
team_info.csv 131 FALSE 666 2023-04-11 09:07:21 2023-04-11 09:07:21 2023-04-18 09:42:19 no
test.csv 18059168 FALSE 666 2022-09-07 09:07:34 2020-02-01 13:44:03 2023-04-18 09:42:19 no
uneven_data.csv 43 FALSE 666 2023-01-06 14:02:17 2023-01-06 14:00:27 2023-04-18 09:42:19 no

Posso quindi utilizzare la funzione order() per ordinare i file in base a mtime , che rappresenta la data e l’ora dell’ultima modifica dei file:

 #sort files based on mtime (date and time modification)
file_info = file_info[ with (file_info, order (as. POSIXct (mtime))), ]

#view sorted files
file_info

                        size isdir mode mtime ctime atime exe
my_list.csv 90 FALSE 666 2022-04-21 09:40:01 2022-04-21 09:39:59 2023-04-18 09:42:19 no
my_test.csv 146 FALSE 666 2022-04-21 09:42:25 2022-04-21 09:42:25 2023-04-18 09:42:19 no
df1.csv 126 FALSE 666 2022-04-21 10:48:24 2022-04-21 10:48:24 2023-04-18 09:42:19 no
df2.csv 126 FALSE 666 2022-04-21 10:48:30 2022-04-21 10:48:29 2023-04-18 09:42:19 no
df3.csv 126 FALSE 666 2022-04-21 10:48:34 2022-04-21 10:48:34 2023-04-18 09:42:19 no
test.csv 18059168 FALSE 666 2022-09-07 09:07:34 2020-02-01 13:44:03 2023-04-18 09:42:19 no
my_data.csv 53 FALSE 666 2022-09-09 09:02:21 2022-04-22 09:00:13 2023-04-18 09:42:19 no
players_data.csv 50 FALSE 666 2023-01-06 09:44:12 2023-01-06 09:44:12 2023-04-18 09:42:19 no
basketball_data.csv 55 FALSE 666 2023-01-06 11:07:43 2022-07-12 09:07:26 2023-04-18 09:42:19 no
uneven_data.csv 43 FALSE 666 2023-01-06 14:02:17 2023-01-06 14:00:27 2023-04-18 09:42:19 no
player_stats.csv 137 FALSE 666 2023-04-11 09:07:20 2023-04-11 09:07:20 2023-04-18 09:42:19 no
team_info.csv 131 FALSE 666 2023-04-11 09:07:21 2023-04-11 09:07:21 2023-04-18 09:42:19 no

Tieni presente che puoi invece utilizzare ctime se desideri ordinare i file in base alla data di creazione o atime per ordinare i file in base alla data di accesso .

Infine, possiamo sottoimpostare il frame di dati per visualizzare solo i nomi dei file e la data e l’ora dell’ultima modifica:

 #view only file names with modification date and time
file_info[c(" mtime ")]

                                  mtime
my_list.csv 2022-04-21 09:40:01
my_test.csv 2022-04-21 09:42:25
df1.csv 2022-04-21 10:48:24
df2.csv 2022-04-21 10:48:30
df3.csv 2022-04-21 10:48:34
test.csv 2022-09-07 09:07:34
my_data.csv 2022-09-09 09:02:21
players_data.csv 2023-01-06 09:44:12
basketball_data.csv 2023-01-06 11:07:43
uneven_data.csv 2023-01-06 14:02:17
player_stats.csv 2023-04-11 09:07:20
team_info.csv 2023-04-11 09:07:21

Se lo desideri, puoi anche visualizzare solo i nomi dei file per data:

 #view only file names
rownames(file_info)

 [1] "my_list.csv" "my_test.csv" "df1.csv" "df2.csv" "df3.csv"            
 [6] "test.csv" "my_data.csv" "players_data.csv" "basketball_data.csv" "uneven_data.csv"    
[11] "player_stats.csv" "team_info.csv"  

I dodici nomi di file CSV sono ordinati per data.

Correlato: Come utilizzare la funzione list.files() in R (4 esempi)

Risorse addizionali

I seguenti tutorial spiegano come eseguire altre attività comuni in R:

Come leggere i file Zip in R
Come importare file CSV in R
Come importare file Excel in R

Aggiungi un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *