Files: read & write

File Operations

Read a data table

# read .csv data file

myData = read.table("data_2x20.csv", header=TRUE)

# same, but defining explicitly the separator as tabulator: "\t", text is framed by double-quotes "sample01" and all lines must have same number of columns

myData = read.table("data_2x20.csv", header=TRUE, sep="\t", row.names=1, quote="", fill=FALSE)

# same, but using  read.csv

myData = read.csv("mydata_text_file.csv", header=TRUE, sep="\t", row.names=1 )

# download and read .csv data from a web address 

myData = read.table( url("") , header=TRUE)

 →  working with "data-frame" table

Write data to text file (print .csv .tsv table )


# ‘write.csv’ is a simple wrapper of write.table

# ‘write.csv’ always uses "." for the decimal point and a comma for the separator 

write.csv(myData, file='myData.csv', quote = FALSE)


# to define separator, decimal, and other output settings

write.table(myData, file = 'myData.tsv', append = FALSE, quote = FALSE, sep = "\t", eol = "\n", na = "missing", dec = ".", row.names = TRUE, col.names = NA, qmethod = "double")


# write strings to text file, line by line

mytext = c('A1','A2','A3')

fid <- file('output_file.txt')

  writeLines( mytext , fid)





read more