R에서 날짜별로 파일을 나열하는 방법(예제 포함)


다음 기본 구문을 사용하여 R의 현재 작업 디렉터리에 있는 파일을 날짜별로 나열할 수 있습니다.

 #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 ")]

다음 예에서는 실제로 이 구문을 사용하는 방법을 보여줍니다.

예: R에서 날짜별로 파일을 나열하는 방법

R의 현재 작업 디렉토리 에 있는 모든 CSV 파일을 날짜별로 나열하고 싶다고 가정해 보겠습니다.

다음 구문을 사용하여 먼저 현재 작업 디렉터리에서 모든 CSV 파일(파일 정보 포함)을 추출할 수 있습니다.

 #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

그런 다음 order() 함수를 사용하여 파일이 마지막으로 수정된 날짜와 시간을 나타내는 mtime 별로 파일을 정렬할 수 있습니다.

 #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

대신 생성 날짜 별로 파일을 정렬하려면 ctime을 사용하고, 액세스 날짜 별로 파일을 정렬하려면 atime을 사용할 수 있습니다.

마지막으로 데이터 프레임을 부분 집합하여 파일 이름과 마지막으로 수정된 날짜 및 시간만 표시할 수 있습니다.

 #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

원하는 경우 날짜별로 파일 이름만 표시할 수도 있습니다.

 #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"  

12개의 CSV 파일 이름은 날짜별로 정렬됩니다.

관련 항목: R에서 list.files() 함수를 사용하는 방법(예제 4개)

추가 리소스

다음 튜토리얼에서는 R에서 다른 일반적인 작업을 수행하는 방법을 설명합니다.

R에서 Zip 파일을 읽는 방법
CSV 파일을 R로 가져오는 방법
Excel 파일을 R로 가져오는 방법

의견을 추가하다

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다