0% found this document useful (0 votes)
82 views9 pages

Export Data Using R Studio

The document discusses various functions in R for exporting data from R objects to files, including write.table(), write.csv(), and writeLines(). write.table() writes data frames to files with options to set separators, row/column names, and missing values. write.csv() is a wrapper for write.table() that writes to CSV files. writeLines() writes each element of a vector to its own line in a file.

Uploaded by

Bob Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
0% found this document useful (0 votes)
82 views9 pages

Export Data Using R Studio

The document discusses various functions in R for exporting data from R objects to files, including write.table(), write.csv(), and writeLines(). write.table() writes data frames to files with options to set separators, row/column names, and missing values. write.csv() is a wrapper for write.table() that writes to CSV files. writeLines() writes each element of a vector to its own line in a file.

Uploaded by

Bob Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.

Export Data

using
R Studio

Rupak Roy
R Import Functions: [Link]()
Just like import, R also comes with rich features to export the processed data.
1. ?[Link]: prints data frame into a wider range of file formats,
including tab-delimited files.

>[Link](x,file=“ ”, sep=“ ” , [Link] = TRUE, [Link] = TRUE, na=


“NA”, dec=“.”)
Where x = R object to be written
file = a character string naming a file
sep = “space” the field separator string. Values within each row of x will
be separated by this string.
dec = the string to use for decimal points in numeric
na = “NA” the string to use for missing values in the data
[Link] = a logical value indicating whether the row names
of x are to be written along with x
[Link] = a logical value indicating whether the column names
of x are to be written along with x
Rupak Roy
R Import Functions: [Link]()

2. ?[Link]: again it’s a wrapper function of [Link] to print data


frames or a R object into csv formats.
[Link](x, file=“ ”, [Link]= F , na= “NA”)
Where x = R object to be written
na = “NA” the string to use for missing values in the data

 By default, the [Link] and [Link] functions create an extra column in


the file containing the observation numbers. To prevent this, set
the [Link] argument to F.
 > [Link](x, "[Link], [Link]=F)

Rupak Roy
R Import Functions: writeLines()

3. ?writeLines: writes text line to a file i.e. prints the entire row as one line
>writeLines(X, file=“ ”, sep = “,")
Where x = R object to be written, example: product$reviews_column
file = “filename” character string value to used to save the file
sep = “,” character string to be written to the connection after each
line of text.

Rupak Roy
[Link]()
>[Link](bike_sharing_program,file = "[Link]",sep =
",",[Link] = T,na="NA")

We will observe that it creates an extra


column in the file containing the
observation numbers for the
R object (bike_sharing_program) in the
[Link] file
causing the column values shift to the right,
like fueltype should be ‘gas’ instead of ‘1’

To overcome this issue we need to declare


one more function [Link] = F or FALSE
indicating row names already exists
with the R object bike_sharing_program
[Link]()
>[Link](bike_sharing_program,file = "[Link]",sep =
",",[Link] = T, [Link] = F, na="NA")

Now, by default it didn’t create an extra


column in the file containing the
observation numbers for the
R object bike_sharing_program in the
[Link]

Hence proper alignment of all the


variable values
[Link]()
>[Link](bike_sharing_program,file = "[Link]",
[Link] = F, na="NA")

However in [Link] parameters like


[Link], sep= “ ,” will throw an error
as [Link] already contains delimiter.
writeLines()
>str(bike_sharing_program)

To perform writeLines() we need character variables/data type. So we will


convert one of the variables for example ‘aspiration’ into character data type
>bike_sharing_program$aspiration<-
[Link](bike_sharing_program$aspiration)
>str(bike_sharing_program$aspiration)

>writeLines(bike_sharing_program$aspiration,"[Link]", sep = "\n")

We can perform the same using an another function call CAT()


>cat(bike_sharing_program$aspiration, sep = ",", file = "[Link]")
Import & Export Data

Next:
We will see how to perform import and export in the lab
session.

Rupak Roy

You might also like