Reading a tab separated file in R - r

I'm trying to read a file in R but the fourth record appears as a new line (see attached). After the third line, there's no tab, just two spaces. I'm using this code:
df = read.delim("text.txt", header = FALSE, stringsAsFactors = FALSE, quote = "")
UPDATE: the third line has "¬" at the end.

use the sep argument of read.delim to specify the separator. in this case you would need
df = read.delim("text.txt", header = FALSE, stringsAsFactors = FALSE, quote = "", sep = "\t")

Related

How can I write a txt file with multiple delimiters [R]?

I'm trying to write a txt file that has multiple delimiters:
text|||value_1|value_2||||
Currently, I've tried:
write.table(variable, "file.txt", sep = "|", quote = FALSE, row.names = FALSE)
But I'm not sure how to use multiple delimiters given that sep only takes in 1 argument. Will appreciate any help! Thank you!
Maybe you can write using one delimiter, read it back, and replace the one delimiter with whatever you want before writing again
write.table(mtcars, "tmp.txt", sep = "|", quote = FALSE)
d = readLines("tmp.txt")
d = gsub("|", "|||", d, fixed = TRUE)
writeLines(d, "tmp2.txt")

R write.table function inserts unwanted empty line at the bottom of my csv

I have this code:
write.table(df, file = f, append = F, quote = TRUE, sep = ";",
eol = "\n", na = "", dec = ".", row.names = FALSE,
col.names = TRUE, qmethod = c("escape", "double"))
where df is my data frame and f is a .csv file name.
The problem is that the resulting csv file has an empty line at the end.
When I try to read the file:
dd<-read.table(f,fileEncoding = "UTF-8",sep = ";",header = T,quote = "\"")
I get the following error:
incomplete final line found by readTableHeader
Is there something I am missing?
Thank you in advance.
UPDATE: I solved the problem deleting the UTF-8 file encoding into the read.table:
dd<-read.table(f,sep = ";",header = T,quote = "\"")
but I can't explain the reason of this, since the default for write.table seems to be UTF-8 anyway (I checked this using an advanced text tool).
Any idea of why this is happening?
Thank you,

Tab - delimited .csv file into R

I have a .csv file tab delimited. While running the code
data <- read.table("xxx.csv",sep = "\t", dec=".", header = TRUE,
encoding="UTF-8", stringsAsFactors = FALSE)
R reads it as a single column without dividing (should make 42 columns). Any ideas? Link to file.
The problem arises because each line is between quotation marks (the whole line).
There are two possible ways to read the file.
Keep all quotation marks.
Use the parameter quote = "" to disable quoting.
read.table("xxx.csv", sep = "\t", dec = ".", header = TRUE,
encoding = "UTF-8", stringsAsFactors = FALSE, quote = "")
Remove the quotation marks before reading the file.
tmp <- gsub('^\"|\"$', '', readLines("xxx.csv"))
read.table(text = tmp, sep = "\t", dec = ".", header = TRUE,
encoding = "UTF-8", stringsAsFactors = FALSE)

How to Create Table from Irregular Length Element in R

I'm new with R and seek some digestible guidance. I wish to create data.frame so I can create column and establish variables in my data. I start with exporting url into R and save into Excel;
data <- read.delim("http://weather.uwyo.edu/cgi-bin/wyowx.fcgi?
TYPE=sflist&DATE=20170303&HOUR=current&UNITS=M&STATION=wmkd",
fill = TRUE, header = TRUE,sep = "\t" stringsAsFactors = TRUE,
na.strings = " ", strip.white = TRUE, nrows = 27, skip = 9)
write.xlsx(data, "E:/Self Tutorial/R/data.xls")
This data got missing value somewhere in the middle of element thus make the length irregular. Due to irregular length I use write.table instead of data.frame.
As 1st attempt, in global environment, data exist in R value(NULL) not in R data;
dat.table = write.table(data)
str(dat.table) # just checking #result NULL?
try again
dat.table = write.table(data,"E:/Self Tutorial/R/data.xls", sep = "\t", quote = FALSE)
dat.table ##print nothing
remove sep =
dat.table = write.table(data,"E:/Self Tutorial/R/data.xls", quote = FALSE
dat.table ##print nothing
since its not working, I try read.table
dat.read <- read.table("E:/Self Tutorial/R/data.xls", header = T, sep = "\t")
Data loaded in R console, but as expected with irregular column distribution, (??even though I already use {na.strings = " ", strip.white = TRUE} in read.delim argument)
What should I understand from this mistake, and which is it. Thank you.

Escaping backslashes when using write.table

I have some strings in one of the columns of my data frame that look like:
bem\\2015\black.rec
When I export the data frame into a text file using the next line:
write.table(data, file = "sample.txt", quote = FALSE, row.names = FALSE, sep = '\t')
Then in the text file the text looks like:
bem\2015BELblack.rec
Do you know an easy way to ignore all backslashes when writing the table into a text file so the backslashes are kept.
They way I have resolved this is converting backslashes into forwardslashes:
dataset <- read_delim(dataset.path, delim = '\t', col_names = TRUE, escape_backslash = FALSE)
dataset$columnN <- str_replace_all(dataset$Levfile, "\\\\", "//")
dataset$columnN <- str_replace_all(dataset$Levfile, "//", "/")
write.table(dataset, file = "sample.txt", quote = FALSE, row.names = FALSE, sep = '\t')
This exports the text imported as bem\\2015\black.rec with the required slashes: bem//2015/black.rec

Resources