Directories, Packages and Saving Data

Directory and Importing data:

getwd() gets our current working directory as the name of the functions suggests.

getwd()
[1] “C:/Users/Vaibhav/Documents”

So if you want to import a file from your desktop to your R environment, you can directly write the code below, provided that file is in the location of output of getwd

read.csv("filename.csv")
Above code will give us bunch of output and will occupy the whole screen. Its a good idea to store read.csv(“filename.csv”) in a variable like below:
a1 <-read.csv("filename.csv")
Now our screen does not get filled by all the output from above.

If we want to know what is in a1 variable we can write yet another function as follows:

str(a1)

This function will give the structure of the dataset a1.

 

So, we see how advantageous it is to use getwd() function but what if we want to change the location of our current working directory?

We have a function named setwd()
setwd("C:/Users/Vaibhav/Desktop")
This function sets our current working directory to “C:/Users/Vaibhav/Desktop”

Notice that we use “/” instead of “\” in path assigning.

Now if we want to import a file from the current location we can write code:
a2 <-read.csv("sample.csv")
str(a2)

Do not worry about the output for now we will discuss it as we move on to new concepts.

Packages:

There are 2 ways to install a package.

1. By going to the menu bar and clicking packages –> Install Package(s) –>  Choose the region (Cloud 0) will work –> choose package you want to install

2. By writing command install.packages(“name of package”)

We will discuss more on packages in later sessions when we start using one.

Saving the code:

After wrting code you can save the code written on screen in 3 ways:

1. You can press CTRL + S in your screen’

2. You can go to file and click Save History as shown in picture

3. you can click the Cross and the program will ask you to save your file or not

 

 

 

 


 

 

 

 

 

 

 

To close R via keyboard we can use q() function and it will close the screen for us.

Congratulations on learning this lesson. Go ahead and do one more.

Leave a Reply

avatar
  Subscribe  
Notify of