I have a file like the following, when i try to read with read.delim
dataset4 <- read.delim("./Documents/time_series/cbe.dat",sep = "/t", stringsAsFactors = FALSE)
it would not work , it gives me the following error:
> invalid value for 'sep': must be a byte
but the dataset is clearly separated with /t, what i am doing wrong here?
I have also tried with read.table. It reads the dataset well but the values are read as characters not as numeric .
The dataset looks like the following :
"choc"/t"beer"/t"elec"
"1451"/t"96.3"/t"1497"
"2037"/t"84.4"/t"1463"
"2477"/t"91.2"/t"1648"
"2785"/t"81.9"/t"1595"
"2994"/t"80.5"/t"1777"
"2681"/t"70.4"/t"1824"
"3098"/t"74.8"/t"1994"
"2708"/t"75.9"/t"1835"
"2517"/t"86.3"/t"1787"
"2445"/t"98.7"/t"1699"
"2087"/t"100.9"/t"1633"
"1801"/t"113.8"/t"1645"
"1216"/t"89.8"/t"1597"
"2173"/t"84.4"/t"1577"
"2286"/t"87.2"/t"1709"
"3121"/t"85.6"/t"1756"
"3458"/t"72"/t"1936"
"3511"/t"69.2"/t"2052"
"3524"/t"77.5"/t"2105"
"2767"/t"78.1"/t"2016"
1 answer
As many answers suggested, your file is actually not a tab separated file but a "/t" separated file. So you can either read in the file as is with some trickery (see above), or with your own function, but the best would be to replace the "/t" characters, depending on your platform. When editing the text of the file you have to replace the "/t" characters with tabs, so not with literal "\t" but with something that interprets "\t" as tabs (for example the find and replace function in gedit).
With sed you have to be a bit careful:
https://unix.stackexchange.com/questions/145299/simple-sed-replacement-of-tabs-mysteriously-failing
This would work:
sed -i "s/\/t/$(printf '\t')/g" ~/test.txt
Initially I tried to run this as a systemcall in R, but I gave up pretty quickly...
Log in to answer this question.
NO, Does not work either, the dataset looks like the following now with "\t" :
/tis not a valid character. How did this even happen? Did someone hand-craft this mangled dataset?My guess is that someone mis-typed
/tinstead of\t. Meant to use a tab, ended up using "/t"Maybe you should try
\/tLooks like someone intended to write a tab-separated file with \t and actually wrote a "custom" file format with /t. I would correct the file rather than trying to read it in as is.
I changed the backslash with a forwardslash. Now after executing the read.delim command as asked in the question , the head of the dataframe looks like this:
With
read.delimyou do not need to specify the separator as tabulator is used by default. Try this:OP's delimiter is not a tab. It's two characters - forward-slash followed by t. Across all platforms, that is two distinct characters, not an escape sequence.
Should you file be separeted by
\\t?You could try changing the separator field to "\t". Try
sed -i 's/\\t/\t/g' your_file. Do this in a copy of your file to test it!can you try this hack with OP text?
output would be some thing like this:
or sep on slash :(
Try following. It woorked.
Created on 2020-04-29 by the reprex package (v0.3.0)