0% found this document useful (0 votes)
13 views2 pages

Saving and Generating Data

The document explains how to save data in R, highlighting the use of the save() function for RData files and write.csv() for text files, allowing for easy sharing and access. It also introduces the sample() function for generating random samples from a dataset, detailing its syntax and parameters. Examples are provided to demonstrate both saving data and creating samples.

Uploaded by

gjob54530
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

Saving and Generating Data

The document explains how to save data in R, highlighting the use of the save() function for RData files and write.csv() for text files, allowing for easy sharing and access. It also introduces the sample() function for generating random samples from a dataset, detailing its syntax and parameters. Examples are provided to demonstrate both saving data and creating samples.

Uploaded by

gjob54530
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Saving Data

As you work with your data in R you will eventually want to save it to disk. This will allow
you to work with the data later and still retain the original dataset. It can also allow you to
share your dataset with other analysts.

One of the simplest ways to save your data is by saving it into an RData file with the
function save( ). R saves your data to the working folder on your computer disk in a binary
file. This storage method is efficient and the only drawback is that, because it is stored in an
R binary format, you can only open it in R
You can save the data frame df using this command:

save(df, file = "df.RData")


Text files

There are other options for saving your data from your R session. You can save your data as
text file. One advantage of saving your data into a text file is that you can open it in another
application, such as a text editor or Excel, and work with it there.

The simplest way to save your data into a text file is by using the write.csv( ) command. You
may recall from the learning infrastructure topic about reading data files that a csv file is a
text file that uses commas to separate each item of data from the other items of data. You can
experiment saving the data frame df using the command:

write.csv(df, file = "df.csv")

Generating Data

sample() function in R Language creates random sample based on the parameters provided
in the function call. It takes either a vector or a positive integer as the object in the function
parameter.
Syntax:
sample(x, size, replace)
Parameters:
x: indicates either vector or a positive integer or data frame
size: indicates size of sample to be taken
replace: indicates logical value. If TRUE, sample may have more than one same value

Example 1:

# Create sample

x <- sample(1:100, 10, replace=TRUE)


# Print

# Output may differ each time the command is


executed

print(x)

Output:
[1] 47 52 22 98 75 94 91 94 42 53
Example 2:

# Create sample

pos <- sample(1:nrow(mtcars), 5, replace = TRUE)

# Print sample observations

# Output may differ each time the command is


executed

print(mtcars[pos, ])

Output:
mpg cyl disp hp drat wt qsec vs am gear carb
Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
Hornet 4 Drive.1 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1

You might also like