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("http://www.nlpca.org/data_2x20.csv") , header=TRUE)

 →  working with "data-frame" table

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

write.csv() 

# ‘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)

write.table()

# 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")

writeLines()

# write strings to text file, line by line

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

fid <- file('output_file.txt')

  writeLines( mytext , fid)

close(fid)

  A1

  A2

  A3


read more

http://www.r-tutor.com/r-introduction/data-frame/data-import

https://stat.ethz.ch/R-manual/R-devel/library/utils/html/read.table.html

https://stat.ethz.ch/R-manual/R-devel/library/utils/html/write.table.html