Home > Blockchain >  Join separator in R fread shell command
Join separator in R fread shell command

Time:05-28

I have two .txt files that I want to join using the R data.table fread function's shell command feature as a pre-processing step before importing into R. The two files are:

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

I tried this code

library(data.table)
foods <- fread(cmd = paste("join -t '|' food_types.txt foods.txt"))

But it returns an error saying

''' is not recognized as an internal or external command,
operable program or batch file.

However, the join works properly if I change the delimiter to a comma and use

foods <- fread(cmd = paste("join -t ',' food_types.txt foods.txt"))

How can I get the join to work with the pipe delimiter? I am using R version 4.1.3 on Windows 10 Pro.

CodePudding user response:

Can you switch the double and single quotes?

fread(cmd = paste('join -t "|" food_types.txt foods.txt'))

Output:

             ID         TYPE\r   FOOD
1: 100000853384          Fat\r Cheese
2: 100003735682 Carbohydrate\r Potato
3: 100006367485        Fruit\r  Apple
4: 100007267644        Protein   Beef
  • Related