Data Structures
Week 2 Summary (DSME5110F)
Three steps to learn a data structure: creating, indexing, and operations (will learn):
– creating: c(), seq, rep; logical (>, !, . . . )
Copyright By PowCoder代写 加微信 powcoder
– indexing: vector1[vector2], logical vector, which()
• Data Frames
– creating: data.frame()
– indexing (one-dimensional): [[]], $ – indexing (two-dimensional): [ , ]
Importing Data Files
• Excel file: Import Dataset -> From Excel • Comma Separated Values (.csv) file
– read.csv – write.csv
Setting Up Your Working Directory
The working directory is the folder on your computer that sets the location of any files you read into R. If you want to know your current R working directory, run the command getwd(), which stands for “get working directory”.
In the path to folders 1) where you install R and 2) the working directory, do not include space or Chinese characters. Otherwise, you may not be able to load the datasets appropriately. If your user name on your computer includes a space or Chinese characters, please create a new user on your computer and then use the new user.
My suggestion for the working directory in this course:
1. Create a folder named CodeData on your computer, and one sub-folder (Data) inside the CodeData folder.
2. Use the CodeData folder as the working directory.
3. Put all the R script in the CodeData folder, and all datasets in the Data sub-folder.
Change your working directory You can change your working directory using 3 methods:
• Method 1 (recommended): From RStudio, use the menu to change your working directory under
Session > Set Working Directory.
• Method 2: It’s also possible to use the R function setwd(), which stands for “set working directory”.
setwd(“path_to_the_your_folder”)
• Method 3: In the bottom-right panel, under Files, you can change the directory there by clicking
“…” (Go to directory). After that, Click More and then Set As Working Directory
Figure 1: Working Directory
Set a default working directory A default working directory is a folder where RStudio goes, every time you open it.
1. If you launch the RStudio directly, the working directory is the default working directory.
1. The default working directory is usually the Document folder. You can change it using the method
2. If you launch the RStudio by opening an R script, the working directory is not the default working
directory (but the folder that the R script locates at).
You can change the default working directory from RStudio menu:
Tools –> Global options –> click on “Browse” to select the default working
directory you want.
Useful Functions
• summary(df): very useful! • head(df, n)
• tail(df, n)
Classification of Data
• Qualitative (categorical): factor, ordered factor • Quantitative (numeric)
Factors and Levels
• Why factors? (save memory space; treat differently from numeric) • levels (the possible categories)
• use the option ordered if you want an ordered factor
Presenting Qualitative Variables
• Frequency distribution (table())
• Relative frequency distribution (prop.table()) • Bar plot (bar graph): barplot()
• Pie chart: pie()
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com