How can I read a .dat file separating "::" in R

I have a text file delimited by "::".
When I read this file as below.

tmp <- fread("file.dat", sep="::")  
tmp <- read.table("file.dat", sep="::")

An error message appears 'sep' must be 'auto' or a single characteror invalid 'sep' value: must be one byte.

How can I read this file?

+4
source share
1 answer

You can try

fread("cat file.dat | tr -s :", sep = ":")

fread()allows you to receive a system call in your first argument. This one uses tr -s, which is the compress command, replacing repetitions :with individual occurrences of this character.

With this call, it fread()can even recognize the argument sepautomatically, eliminating the need for its name.

, ( "x.txt" ):

writeLines("a::b::c", "x.txt")
read.table(text = system("cat x.txt | tr -s :", intern = TRUE), sep = ":")
#   V1 V2 V3
# 1  a  b  c

, Windows.

+3

Source: https://habr.com/ru/post/1589335/


All Articles