Import and Export CSV file in R

Created by

Rischan Mafrur

Chonnam National University of South Korea

http://rischanlab.github.io

May 28, 2014

Oke, Let's see the example for exporting data to csv file and then import again.

First one we need to assign the data to variable, in this case I will use data frame

x <- data.frame(name = c("rischan","mafrur"),age= c(23,24),address= c("Gwangju","Seoul"))

Show data in variable x

x
##      name age address
## 1 rischan  23 Gwangju
## 2  mafrur  24   Seoul

or you can assign the value one by one like this example:

name = c("rischan","mafrur")
age = c(23,24)
address = c("Gwangju","Seoul")
y <- data.frame(name,age,address)

Show data in varibale y

y
##      name age address
## 1 rischan  23 Gwangju
## 2  mafrur  24   Seoul

Save variable x to csv file using command write.csv , use another parameter row.names =FALSE (do not saving row names)

write.csv(x,"/Users/macintosh/Rdata/x.csv", row.names = FALSE)

Remove variable x

rm(x)

Show variable x (now we don't have variable x)

x
## Error: object 'x' not found

Import variable x from csv file

x <- read.csv("/Users/macintosh/Rdata/x.csv")

Print value of x

x
##      name age address
## 1 rischan  23 Gwangju
## 2  mafrur  24   Seoul