我有兩個 .txt 檔案,我想使用 R data.table fread函式的 shell 命令功能作為匯入 R 之前的預處理步驟來加入它們。這兩個檔案是:
foods.txt:
ID|FOOD
100000853384|Cheese
100003735682|Potato
100006367485|Apple
100007267644|Beef
food_types.txt:
ID|TYPE
100000853384|Fat
100003735682|Carbohydrate
100006367485|Fruit
100007267644|Protein
我試過這段代碼
library(data.table)
foods <- fread(cmd = paste("join -t '|' food_types.txt foods.txt"))
但它回傳一個錯誤說
''' is not recognized as an internal or external command,
operable program or batch file.
但是,如果我將分隔符更改為逗號并使用,則連接可以正常作業
foods <- fread(cmd = paste("join -t ',' food_types.txt foods.txt"))
如何讓連接與管道分隔符一起使用?我在 Windows 10 Pro 上使用 R 版本 4.1.3。
uj5u.com熱心網友回復:
可以切換雙引號和單引號嗎?
fread(cmd = paste('join -t "|" food_types.txt foods.txt'))
輸出:
ID TYPE\r FOOD
1: 100000853384 Fat\r Cheese
2: 100003735682 Carbohydrate\r Potato
3: 100006367485 Fruit\r Apple
4: 100007267644 Protein Beef
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/482063.html
