0% found this document useful (0 votes)
9 views11 pages

Lab-3 Selection-2

This lesson teaches how to navigate the local workspace in R and its relationship with the file system. It covers commands for checking the working directory, listing objects, creating and manipulating files and directories, and emphasizes the importance of understanding file paths across different operating systems. The lesson also encourages exploring help pages for functions and maintaining organized workspaces.

Uploaded by

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

Lab-3 Selection-2

This lesson teaches how to navigate the local workspace in R and its relationship with the file system. It covers commands for checking the working directory, listing objects, creating and manipulating files and directories, and emphasizes the importance of understanding file paths across different operating systems. The lesson also encourages exploring help pages for functions and maintaining organized workspaces.

Uploaded by

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

Selection: 2

| | 0%

| In this lesson, you'll learn how to examine your local workspace in R and begin
| to explore the relationship between your workspace and the file system of your
| machine.

...

|== | 3%
| Because different operating systems have different conventions with regards to
| things like file paths, the outputs of these commands may vary across machines.

...

|==== | 5%
| However it's important to note that R provides a common API (a common set of
| commands) for interacting with files, that way your code will work across
| different kinds of computers.

...

|====== | 8%
| Let's jump right in so you can get a feel for how these special functions work!

...

|======== | 10%
| Determine which directory your R session is using as its current working
| directory using getwd().

> getwd()
[1] "C:/Users/User"

| Great job!

|========= | 13%
| List all the objects in your local workspace using ls().

> ls()
[1] "my_div" "my_sqrt" "newdata" "x" "y" "z"

| That's correct!

|=========== | 15%
| Some R commands are the same as their equivalents commands on Linux or on a Mac.
| Both Linux and Mac operating systems are based on an operating system called
| Unix. It's always a good idea to learn more about Unix!

...

|============= | 18%
| Assign 9 to x using x <- 9.

> x<-9

| That's the answer I was looking for.


|=============== | 21%
| Now take a look at objects that are in your workspace using ls().

> ls()
[1] "my_div" "my_sqrt" "newdata" "x" "y" "z"

| Keep up the great work!

|================= | 23%
| List all the files in your working directory using list.files() or dir().

> files()
Error in files() : could not find function "files"
> dir()
[1] "3D Objects"

[2] "AppData"

[3] "Application Data"

[4] "Contacts"

[5] "Cookies"

[6] "Desktop"

[7] "Documents"

[8] "Downloads"

[9] "empid.csv"

[10] "Ex 1.R"

[11] "Ex 2.R"

[12] "Ex 3.R"

[13] "Favorites"

[14] "h3cse.csv"

[15] "h3cse.txt"

[16] "IBM"

[17] "InstallAnywhere"

[18] "IntelGraphicsProfiles"

[19] "Links"

[20] "Local Settings"

[21] "Music"

[22] "My Documents"

[23] "NetHood"
[24] "NTUSER.DAT"

[25] "ntuser.dat.LOG1"

[26] "ntuser.dat.LOG2"

[27] "NTUSER.DAT{53b39e88-18c4-11ea-a811-000d3aa4692b}.TM.blf"

[28] "NTUSER.DAT{53b39e88-18c4-11ea-a811-
000d3aa4692b}.TMContainer00000000000000000001.regtrans-ms"
[29] "NTUSER.DAT{53b39e88-18c4-11ea-a811-
000d3aa4692b}.TMContainer00000000000000000002.regtrans-ms"
[30] "ntuser.ini"

[31] "OneDrive"

[32] "output.csv"

[33] "Pictures"

[34] "PrintHood"

[35] "R"

[36] "Recent"

[37] "Saved Games"

[38] "Searches"

[39] "SendTo"

[40] "Start Menu"

[41] "Templates"

[42] "Untitled.ipynb"

[43] "Untitled1.ipynb"

[44] "Untitled2.ipynb"

[45] "Untitled3.ipynb"

[46] "Untitled4.ipynb"

[47] "Untitled5.ipynb"

[48] "Untitled6.ipynb"

[49] "Untitled7.ipynb"

[50] "Untitled8.ipynb"

[51] "Untitled9.ipynb"

[52] "Videos"
| Your dedication is inspiring!

|=================== | 26%
| As we go through this lesson, you should be examining the help page for each new
| function. Check out the help page for list.files with the command ?list.files.

> list.files
function (path = ".", pattern = NULL, all.files = FALSE,
full.names = FALSE, recursive = FALSE, ignore.case = FALSE,
include.dirs = FALSE, no.. = FALSE)
.Internal(list.files(path, pattern, all.files, full.names, recursive,
ignore.case, include.dirs, no..))
<bytecode: 0x0000013135b9de58>
<environment: namespace:base>

| Try again. Getting it right on the first try is boring anyway! Or, type info()
| for more options.

| Type ?list.files to see the help page.

> list.files()
[1] "3D Objects"

[2] "AppData"

[3] "Application Data"

[4] "Contacts"

[5] "Cookies"

[6] "Desktop"

[7] "Documents"

[8] "Downloads"

[9] "empid.csv"

[10] "Ex 1.R"

[11] "Ex 2.R"

[12] "Ex 3.R"

[13] "Favorites"

[14] "h3cse.csv"

[15] "h3cse.txt"

[16] "IBM"

[17] "InstallAnywhere"

[18] "IntelGraphicsProfiles"

[19] "Links"
[20] "Local Settings"

[21] "Music"

[22] "My Documents"

[23] "NetHood"

[24] "NTUSER.DAT"

[25] "ntuser.dat.LOG1"

[26] "ntuser.dat.LOG2"

[27] "NTUSER.DAT{53b39e88-18c4-11ea-a811-000d3aa4692b}.TM.blf"

[28] "NTUSER.DAT{53b39e88-18c4-11ea-a811-
000d3aa4692b}.TMContainer00000000000000000001.regtrans-ms"
[29] "NTUSER.DAT{53b39e88-18c4-11ea-a811-
000d3aa4692b}.TMContainer00000000000000000002.regtrans-ms"
[30] "ntuser.ini"

[31] "OneDrive"

[32] "output.csv"

[33] "Pictures"

[34] "PrintHood"

[35] "R"

[36] "Recent"

[37] "Saved Games"

[38] "Searches"

[39] "SendTo"

[40] "Start Menu"

[41] "Templates"

[42] "Untitled.ipynb"

[43] "Untitled1.ipynb"

[44] "Untitled2.ipynb"

[45] "Untitled3.ipynb"

[46] "Untitled4.ipynb"

[47] "Untitled5.ipynb"

[48] "Untitled6.ipynb"
[49] "Untitled7.ipynb"

[50] "Untitled8.ipynb"

[51] "Untitled9.ipynb"

[52] "Videos"

| One more time. You can do it! Or, type info() for more options.

| Type ?list.files to see the help page.

> ?list.files

| Excellent work!

|===================== | 28%
| One of the most helpful parts of any R help file is the See Also section. Read
| that section for list.files. Some of these functions may be used in later
| portions of this lesson.

...

|======================= | 31%
| Using the args() function on a function name is also a handy way to see what
| arguments a function can take.

...args()

|========================= | 33%
| Use the args() function to determine the arguments to list.files().

> args()
Error in args() : argument "name" is missing, with no default
> args(list.files)
function (path = ".", pattern = NULL, all.files = FALSE,
full.names = FALSE, recursive = FALSE, ignore.case = FALSE,
include.dirs = FALSE, no.. = FALSE)
NULL

| You're the best!

|=========================== | 36%
| Assign the value of the current working directory to a variable called "old.dir".

> old.dir
Error: object 'old.dir' not found
> old<-dir()

| Keep trying! Or, type info() for more options.

| Type old.dir <- getwd() to assign the value of the current working directory to a
| variable called "old.dir".

> old.dir <- getwd()

| You nailed it! Good job!


|============================ | 38%
| We will use old.dir at the end of this lesson to move back to the place that we
| started. A lot of query functions like getwd() have the useful property that they
| return the answer to the question as a result of the function.

...getwd()

|============================== | 41%
| Use dir.create() to create a directory in the current working directory called
| "testdir".

> dir.create("testdir")

| You are doing so well!

|================================ | 44%
| We will do all our work in this new directory and then delete it after we are
| done. This is the R analog to "Take only pictures, leave only footprints."

...

|================================== | 46%
| Set your working directory to "testdir" with the setwd() command.

> setwd("testdir")

| Excellent work!

|==================================== | 49%
| In general, you will want your working directory to be someplace sensible,
| perhaps created for the specific project that you are working on. In fact,
| organizing your work in R packages using RStudio is an excellent option. Check
| out RStudio at http://www.rstudio.com/

...http://www.rstudio.com/

|====================================== | 51%
| Create a file in your working directory called "mytest.R" using the file.create()
| function.

> file.create("mytest.R")
[1] TRUE

| You are really on a roll!

|======================================== | 54%
| This should be the only file in this newly created directory. Let's check this by
| listing all the files in the current directory.

> list.files()
[1] "mytest.R"

| You got it!

|========================================== | 56%
| Check to see if "mytest.R" exists in the working directory using the
| file.exists() function.

> file.exists("mytest.R")
[1] TRUE

| That's a job well done!

|============================================ | 59%
| These sorts of functions are excessive for interactive use. But, if you are
| running a program that loops through a series of files and does some processing
| on each one, you will want to check to see that each exists before you try to
| process it.

...

|============================================== | 62%
| Access information about the file "mytest.R" by using file.info().

> file.info("mytest.R")
size isdir mode mtime ctime
mytest.R 0 FALSE 666 2021-11-05 11:48:06 2021-11-05 11:48:06
atime exe
mytest.R 2021-11-05 11:48:06 no

| That's correct!

|=============================================== | 64%
| You can use the $ operator --- e.g., file.info("mytest.R")$mode --- to grab
| specific items.

...file.info("mytest.R")$mode

|================================================= | 67%
| Change the name of the file "mytest.R" to "mytest2.R" by using file.rename().

> file.rename("mytest2.R")
Error in file.rename("mytest2.R") :
argument "to" is missing, with no default
> file.rename("mytest.R" to "mytest2.R")
Error: unexpected symbol in "file.rename("mytest.R" to"
> file.rename("mytest.R","mytest2.R")
[1] TRUE

| You got it!

|=================================================== | 69%
| Your operating system will provide simpler tools for these sorts of tasks, but
| having the ability to manipulate files programatically is useful. You might now
| try to delete mytest.R using file.remove('mytest.R'), but that won't work since
| mytest.R no longer exists. You have already renamed it.

...file.remove('mytest.R')

|===================================================== | 72%
| Make a copy of "mytest2.R" called "mytest3.R" using file.copy().

> file.copy("mytest2.R","mytest3.R")
[1] TRUE

| Excellent work!

|======================================================= | 74%
| You now have two files in the current directory. That may not seem very
| interesting. But what if you were working with dozens, or millions, of individual
| files? In that case, being able to programatically act on many files would be
| absolutely necessary. Don't forget that you can, temporarily, leave the lesson by
| typing play() and then return by typing nxt().

...nxt()

|========================================================= | 77%
| Provide the relative path to the file "mytest3.R" by using file.path().

> file.path("mytest3.R")
[1] "mytest3.R"

| That's a job well done!

|=========================================================== | 79%
| You can use file.path to construct file and directory paths that are independent
| of the operating system your R code is running on. Pass 'folder1' and 'folder2'
| as arguments to file.path to make a platform-independent pathname.

> file.path("folder1")
[1] "folder1"

| Try again. Getting it right on the first try is boring anyway! Or, type info()
| for more options.

| file.path("folder1", "folder2") works.

> file.path("folder1", "folder2")


[1] "folder1/folder2"

| You are really on a roll!

|============================================================= | 82%
| Take a look at the documentation for dir.create by entering ?dir.create . Notice
| the 'recursive' argument. In order to create nested directories, 'recursive' must
| be set to TRUE.

> ?dir.create

| You are doing so well!

|=============================================================== | 85%
| Create a directory in the current working directory called "testdir2" and a
| subdirectory for it called "testdir3", all in one command by using dir.create()
| and file.path().

>
> dir.create("testdir3")

| Not quite, but you're learning! Try again. Or, type info() for more options.

| dir.create(file.path('testdir2', 'testdir3'), recursive = TRUE) will do the


| trick. If you forgot the recursive argument, the command may have appeared to
| work, but it didn't create the nested directory.

> dir.create(file.path('testdir2', 'testdir3'), recursive = TRUE)


| That's the answer I was looking for.

|================================================================= | 87%
| Go back to your original working directory using setwd(). (Recall that we created
| the variable old.dir with the full path for the orginal working directory at the
| start of these questions.)

> setwd(old.dir)

| Nice work!

|================================================================== | 90%
| It is often helpful to save the settings that you had before you began an
| analysis and then go back to them at the end. This trick is often used within
| functions; you save, say, the par() settings that you started with, mess around a
| bunch, and then set them back to the original values at the end. This isn't the
| same as what we have done here, but it seems similar enough to mention.

...

|==================================================================== | 92%
| After you finish this lesson delete the 'testdir' directory that you just left
| (and everything in it)

...delete_progress("testdir")

|====================================================================== | 95%
| Take nothing but results. Leave nothing but assumptions. That sounds like 'Take
| nothing but pictures. Leave nothing but footprints.' But it makes no sense!
| Surely our readers can come up with a better motto . . .

...motto

|======================================================================== | 97%
| In this lesson, you learned how to examine your R workspace and work with the
| file system of your machine from within R. Thanks for playing!

...

|==========================================================================| 100%
| Would you like to receive credit for completing this course on Coursera.org?

1: No
2: Yes

Selection: 1

| You nailed it! Good job!

| You've reached the end of this lesson! Returning to the main menu...

| Please choose a course, or type 0 to exit swirl.

1: R Programming
2: Take me to the swirl course repository!

Selection: 1

| Please choose a lesson, or type 0 to return to course menu.


1: Basic Building Blocks 2: Workspace and Files
3: Sequences of Numbers 4: Vectors
5: Missing Values 6: Subsetting Vectors
7: Matrices and Data Frames 8: Logic
9: Functions 10: lapply and sapply
11: vapply and tapply 12: Looking at Data
13: Simulation 14: Dates and Times
15: Base Graphics

Selection: 3

| | 0%

| In this lesson, you'll learn how to create sequences of numbers in R.

...

You might also like