9
WHAT IS R? 6.Command-line style If you are working on some more complicated or longer scripts, or if you want to save the scripts you are working on, it’s a good practice to write your code in a script editor. (In R, go to File > “New Document” (Mac) or “New Script” (Windows)). 9

15
DATA OBJECTS 1.Vectors 2.Matrices 3.Data frames (tables) a.Dimensionless b.Data points of the same type: e.g., numeric or character string, but not both. How do we create vectors? Use c(…) 15

16
DATA OBJECTS EXERCISE 2: Creating vectors Ex2.1: Create a vector named v1 that stores the following values: 2, 4, 1, 4, 6, 1 Ex2.2: Create a vector named v2 that stores the following character strings: "apple", "pear", "kiwi", ”plum” Ex2.3: Create a vector named v3 that stores the following values: 1.3, 0.2, 3.2, 5.1, 4.3, 6.7 Ex2.4: Create a vector named v4 that stores the following Booleans: TRUE, FALSE, FALSE, TRUE Ex2.5: Concatenate v1 and v3, and name the resulting vector v5. Ex2.6: Check the number of elements in a vector using length(). 16

17
DATA OBJECTS 1.Vectors 2.Matrices 3.Data frames (tables) a.2-dimensional b.Data points of the same type: e.g., numeric or character string, but not both. How do we create matrices? 17

23
IMPORT DATA  Natively supported data files:.txt,.dat,.csv  Some R packages extend support to data formats of other popular statistical programs, such as SPSS, STATA, and SAS. e.g., the R package `foreign` and the R package `RODBC` (Excel) (There are additional ways to import data that are not discussed here) 23

25
IMPORT DATA: DATA FRAMES 2.Import data frames using read.table(). read.table(file, header = FALSE, sep = "",...) file: path and the name of the file to be read in.* header: whether the 1 st row contains column names. sep: a character that separates values in a row. *You can use file.choose() instead typing out the file path and file name. 1. Let’s import the dataset vocab.txt and save it as vocab. First, open the text file using a text editor to see what the dataset looks like. vocab
{
"@context": "http://schema.org",
"@type": "ImageObject",
"contentUrl": "http://images.slideplayer.com/14/4194177/slides/slide_25.jpg",
"name": "IMPORT DATA: DATA FRAMES 2.Import data frames using read.table().",
"description": "read.table(file, header = FALSE, sep = ,...) file: path and the name of the file to be read in.* header: whether the 1 st row contains column names. sep: a character that separates values in a row. *You can use file.choose() instead typing out the file path and file name. 1. Let’s import the dataset vocab.txt and save it as vocab. First, open the text file using a text editor to see what the dataset looks like. vocab

26
IMPORT DATA: DATA FRAMES 2.Import data frames using read.table(). read.table(file, header = FALSE, sep = "",...) file: path and the name of the file to be read in.* header: whether the 1 st row contains column names. sep: a character that separates values in a row. *You can use file.choose() instead typing out the file path and file name. 2. Let’s import another set of data, called pima.csv and save it as pima. First, open the text file using a text editor to see what the dataset looks like. pima
{
"@context": "http://schema.org",
"@type": "ImageObject",
"contentUrl": "http://images.slideplayer.com/14/4194177/slides/slide_26.jpg",
"name": "IMPORT DATA: DATA FRAMES 2.Import data frames using read.table().",
"description": "read.table(file, header = FALSE, sep = ,...) file: path and the name of the file to be read in.* header: whether the 1 st row contains column names. sep: a character that separates values in a row. *You can use file.choose() instead typing out the file path and file name. 2. Let’s import another set of data, called pima.csv and save it as pima. First, open the text file using a text editor to see what the dataset looks like. pima

27
IMPORT DATA: DATA FRAMES 3.Import datasets stored in formats not natively supported, using the package `foreign`. `foreign` must be installed. In R, installing a package can be done using install.packages("pkg_name") After installing a package, we need to load it using library(pkg_name) when we want to use it. So to install `foreign`, we do install.packages("foreign") To use the functions in `foreign`, we do library(foreign) 27

41
MANIPULATE DATA OBJECTS  Subsetting data frames using subset() subset(x, subset, select) x: data frame subset: logical expr. indicating elements or rows to keep. select: column(s) to be selected; default: all columns. Ex5.16: Select a subset of pima for women who were either never pregnant or women who had more than 12 pregnancies, and we only want the first 3 cols. Ex5.17: Select a subset of pima for women who had more than 10 pregnancies and did not have diabetes. 41