Using R Langauge 3. Complete the following tasks. Save the commands in a history
ID: 3736549 • Letter: U
Question
Using R Langauge
3. Complete the following tasks. Save the commands in a history file called
question3.txt.
There is a matrix and a vector in the binary file objects.Rdata (found on D2L).
Load these objects from the file. Determine the names of these objects and
display their contents.
With the vector object:
With the matrix object:
o Create separate variables that contain the maximum, the minimum,
the mean and the sum of the original vector.
o Using row and column names (instead of indices) determine the mean
of the middle row and the mean of the middle column
o Create a new vector object called subV1 that contains all of the values
the first two rows of the matrix.
o Create a new matrix object called subMatrix1 that contains the
“9am” and “9pm” data in separate rows. Don’t worry about the
column and row headers in the matrix. You can use multiple
commands to get this done but it will be more impressive if you can
do it in one!
objects.Rdata
R version 3.4.3 (2017-11-30) -- "Kite-Eating Tree"
Copyright (C) 2017 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
[Previously saved workspace restored]
Explanation / Answer
If you use R a lot, you might find that for some projects or classes you would like your objects for that project/class accessible in separate folders. For example, you might want to remember what that data xinc was for before you deleted it or so you don't have memory problems, particularly if you are working off of Leland and thus may run out of space.
However, this is easier to explain if we first learn about saving objects.
Saving in R
If you want to save on particular object that you made (like a dataset) for use in R later, you can use the command save. If you make the extension of your file ''.rdata'' then Windows recognizes it as an R Data file and will "open" it in R by autolaunching R and making the objects saved in the files the available objects in R.
> Numb<-c(2,4,-1.4)
> save(Numb,file="Numb.Rdata")
If you exit R and look at the files in your working directory, you will see Numb.Rdata. (If you can't find where it is saved, then you probably just don't know your working directory - use getwd()from above to find out). Now this is a file you can move around to different directories or email, etc. just like any other file on your computer - this is a way of saving your information. (dump() is another way, but not as nice and takes up more space). If you move it to a different machine, say, you can get it back within R by using load()
> load(file="Numb.Rdata")
You can save more than 1 object to a file giving a vector of the character names of the objects using the option list. For example if I made two objects named temp and temp2, then I could do either of the following commands
> save(list=c("temp","temp2"),file="Test.Rdata") #saves those 2 objects
> save(list=ls(pat="temp")),file="Test2.Rdata") #saves any object with name containing "temp"
> source("myfun.R")
> history() #default: last 25 lines
> history(120)
You can save the entire history of commands using the command savehistory(). By default, these commands are saved to the default file, ".Rhistory", but you can set your own. You can also load a saved history with loadhistory().
See explanation at the end of my Introduction to R Lab (Scroll way down to the part on Plots)
Of course, if you save at the dialog box prompt when you exit R, you don't need to load the information back in, because the object "Numb" and any other objects you made are saved. Except for any plots, everything is as if you just left it. Sometimes you want to backup your entire session in the middle, in case something crashes, etc.
In fact, when you save when exiting, R is actually calling the function save.image, which saves all of the objects to a file ".RData" in your working directory. So you can actually access all of your data that you saved when you exited by loading the .RData file. So you can also call save.image() in the middle of a session to save everything to the .RData file in your working directory rather than waiting to the end.
If you call save.image on its own, the history of commands will not be saved, however. If you save at the prompt, R also calls savehistory() to save the commands and every time you start R, R opens the file ".Rhistory" if it exists, and makes the commands found there your current history. If you wish to truly backup everything, make sure you call savehistory() as well
load(), save.image(), setwd(), savehistory(), loadhistory(), source() can also be called from the R Gui under "File".
Often times, it is useful to have different R sessions for different projects or classes. By this, I mean different working directories with corresponding different ".RData" files. One advantage of this, for example, is when you type ls(), you will only see the objects in that ".RData" file so you only have objects for that project. Similarly, if you save a plot or an object, they will be by default in that directory. This helps to keep your projects separate, and also helps in not accidentally using the wrong data or function!
If you are working off of Leland, or generally a unix machine, where you type "R" will be where the working directory is automatically assumed to be and thus where a ".RData" file is created when you save and exit. So if you are in a folder "stat" for your class, if you type "R" at the prompt, the objects will be there.
If you would like, you can create separate shortcut menus on your desktop to go different folders which accomplishes this task. This is particularly useful if you are frequently using this folder/project
Otherwise, if you have an Robject saved as a "xxx.rdata" file (see below), then double-clicking on that file will open R with the working directory where the file was. This includes the ".Rdata" file that R generates when you save when you exit. So if you don't want a short-cut for every folder you have, you could alternatively:
This will create a ".RData" file for this directory (with only temp in it). When you double-click it, R will open with that working directory. This is clearly a round-about way to do it, but at least it works.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.