--- Onehack.us e-Learning Platform ---
- [Instructor] So now you've gone through a lot of work
to get your data imported into R.
The thing is, you don't have to do that every time.
Once you get your data in there, you have an option
of saving it as a native R data format.
Now, there's a couple of reasons you might want to do that.
Number one is you don't have to go through
all the transformations again,
which makes it easier and less error prone.
Number two is our data objects are compressed
and they can be dramatically smaller,
a fraction of the original uncompressed data set's size,
and that's going to do you a lot of good.
So let's take a look at how this works.
What I'm going to do is I'm going to start by loading pacman
and a few other packages, including rio and tidyverse.
And then I'm going to open up a data set
that's in our project that I have done before.
It's the state data, and I'm going to import it,
save it as a tibble, select a few things,
change one of 'em as a factor, rename a variable.
So I'm doing some work
and maybe I don't want to do this every single time.
So I'm going to save that into df,
which just stands for data frame.
And then I'm going to save it as one of two different kinds
of R data objects.
The first one is R data.
You can write it as .rdata or .rda,
it's the same thing either way,
but we can use the base R command save.
And all we have to do is say Save.
What are we saving? df.
And then we give it a file name
and you have to write file equals,
and then I'm telling you to put in the data folder
in our project and give it this name with rdata.
By the way, I tried once to be snappy
and use the deep flyer format
where I put the df on the outside and then feed it in.
R did not like that, so you need to do it this way.
But when I run that command, I want you to see
that something is going to get added over here.
These are the data sets
that we have currently in our project data folder.
I'm going to run this one and now we have a new one.
And I want you to see
how much smaller it is than the original Excel file
and the CSV file.
It's a fraction of the size.
Now mind you, this is a very small data set.
It's being measured in kilobytes,
but that difference, that proportional increase
is going to be the same with larger data sets as well.
Now let me show you how this works.
Let's now clear the data set out of the global environment.
Let's get rid of that.
And we're going to reload the rdata file.
We have two choices for doing this.
One is use Base R'S load function.
And what that does is it immediately puts the data
in the environment, the global environment,
right over here, like it was never gone.
The trick is you can't rename it
and you can't display it in the console
with the same command, but it's easy.
So let's do load.
And all I got to say is it's in the data folder
and it's StateData.rdata.
I do that, you can see it showed up right there.
I'm going to check it by just calling its name.
And that's exactly what we had before. Perfect.
Now I'm going to clear that out
and show you the other way to import it.
Import is a command from Rio, a contributed package,
and it doesn't display it immediately in the environment,
but it will show it in the console.
So let's run this one import
and then I'm going to ask it to print.
And then here it is, it's imported it.
The other thing is I can use import
and I can save it into a new object if I want to.
And that'll show up in the global environment.
So here we go, new_DF just stands for new data frame,
and there it is.
So that either load or import.
The other choice for data objects in R is RDS,
which is a serialized, the S is for RData serialized.
And there are certain advantages to that one.
But let's come down here and let's again bring up our df.
I'm going to come back up here and save it again.
I'm going to reimport this so that's there again.
And now let's come down here and we'll use saveRDS.
So that's a built in command
and I can say take the data frame, save it as RDS,
and you can see we just have the different thing down here.
I can do that, and now I have that one here.
And you can see it's also very small, just 2.6 k.
I can also use write_rds from the tidyverse.
Now one thing about this is by default,
this command does not compress the data.
And you may think, well, that's silly.
It's just the idea here is they say
space is generally cheaper than time.
If you're using a very large data set,
maybe you have enough storage space
that you don't need to compress it,
and the time it would take would be more of a problem.
But let's do this one.
We're going to take data frame and write it to RDS
and I'm going to put a little two there to separate it.
And so you can see, yeah, it's bigger, it's twice as big,
although it's still a lot smaller
than the original CSV and Excel files,
but I can use either one.
You can, by the way, add an argument to write_rds
to choose a compression method
to get it down if you want to.
I'm going to clear the data from the environment.
We're going to get rid of that over there.
Now we're going to read it, and we have three different options
on how to read an RDS file.
BaseR's read_rds,
readr, that's part of the tidyverse, has read_rds.
And by the way, capitalization matters on these.
And then Rio just has the same old import
where you don't have to tell it what it is you're importing,
you don't have to specify the format, it can figure it out.
So used on its own, import will show the data
in the console down here.
So I'm just going to do import rds, and there it is down there.
It doesn't show up in the environment,
but it lets me save it to a new object.
If I want to give a destination object,
I can do that with import
and I could also do that with the other rds.
And so there are a few different ways
that you can put it in there.
But really, the important thing here
is any of these functions is going to allow you
to save your data and all the work you did
in preparing your data as a native RData object,
either RData or RDS.
And then you can easily import it
and pick up exactly where you were before
without having to do the import
and transformations over again.
It's quicker, it's less prone to error,
and it's more compact.
All beautiful things when working with data.