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'

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

# 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 a data table

# write data to text file, using write.csv2()

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

# write data to text file, using write.table()

write.table(myData, file = 'myData.txt', 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)

close(fid)

A1

A2

A3


read more

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