本文へスキップ
からだにいいもの

Rのトピックスを中心に『まだ、まだ、知らない、役に立つ情報?』を発信します。

Rで解析:文字列やフォルダの操作に便利「filesstrings」パッケージ

文字列やフォルダの操作に便利なパッケージの紹介です。

パッケージバージョンは3.2.3。実行コマンドはwindows 11のR version 4.2.1で確認しています。

パッケージのインストール

下記コマンドを実行してください。

#パッケージのインストール
install.packages("filesstrings")
スポンサーリンク

実行コマンドの紹介

詳細はコマンド、各パッケージのヘルプを確認してください。

#パッケージの読み込み
library("filesstrings")

###フォルダとファイルを作成#####
library("tcltk")
#ダイアログでフォルダを新規作成し保管場所を指定する
setwd(paste(as.character(tkchooseDirectory(title = "フォルダを選択"), sep = "", collapse ="")))
#保存場所にフォルダを作成
CreateDir <- paste0(getwd(), "/", c("Test_00", "Test 1", "Test_02"))
for(i in seq(CreateDir)){ dir.create(CreateDir[i]) }
#ファイルを作成
file.create(c("Test 000.txt", "Test 1.txt", "Test 02.txt"))
########

#ファイルをフォルダに移動:PutFilesInDirコマンド
#移動するファイルを指定:filesオプション
#移動先のフォルダを指定:destinationsオプション;既存でなければフォルダを新規作成
move_files(files = "Test 000.txt", destinations = "Test_00",
           overwrite = FALSE)
1 file moved. 0 failed.

#フォルダを削除:remove_dirコマンド
remove_dir(dirs = c("Test_1", "Test_02"))
1 directory deleted. 1 failed to delete.

#ファイル名やフォルダ名の半角スペースを
#指定内容で置換:RemoveFileNameSpacesコマンド
#指定内容:replacementオプション
#半角スペース以外のパターンを正規表現で指定:patternオプション
remove_filename_spaces(replacement = "_")
3 files required renaming and this was done successfully.

#ファイル名を整える:nice_file_numsコマンド
#ファイルの拡張子を指定:patternオプション
#例では"Test 000.txt", "Test 1.txt", "Test 02.txt"を
#"Test_000.txt", "Test_001.txt", "Test_002.txt"にする
nice_file_nums(pattern = "\\.txt$")
2 files renamed into the desired format. 0 failed.

#文字列に含まれるn番目の文字を取得:str_elemコマンド
str_elem(paste0(LETTERS[1:24], collapse = ""), 12)
[1] "L"

#英数文字列を一文字ずつに分割;str_to_vecコマンド
str_to_vec("abc1def")
[1] "a" "b" "c" "1" "d" "e" "f"

#英数文字列に含まれる数字で分割:str_split_by_numsコマンド
str_split_by_nums("Karada1234Good.net") [[1]]
[1] "Karada" "1234" "Good.net"

少しでも、あなたの解析が楽になりますように!!

このコードの続きをAIに聞く

スポンサーリンク
Prices and shipping availability may change. Please refer to the product page at time of purchase.
Content displayed on this site is provided by Amazon and may be updated or removed.
Amazon Associate, karada-good earns income through qualifying sales.