31 4 2MB
R Notes Chapter 1. Data Type and Data Entry
C.Y. Ng
Chapter 1
Data Type and Data Entry
L earning Objectives Data types, simple functions, importing data
1
Getting Started with Vectors and Functions
Before we do any kinds of data analysis in R, we need to import or enter data. R has a wide variety of data types. Data Numeric Integer Complex Logical Character
Example 12.3, 7, 8e2 2L, 34L, 0L 3 - 2i TRUE, FALSE 'a' , "good", "TRUE", '23.4'
We assign a variable with a particular value using the symbol “ a 12.3 -> a
You can use whatever variable names you like as long as they fulfill the following conditions. Reserved words are those reserved for use as names for functions or other special purposes, and cannot be used as variable names. A valid variable name consists of letters, numbers and the dot or underline characters and starts with a letter or the dot not followed by a number. Names such as .2way are not valid. The function class() returns the class of a variable. You can verify the class of a variable by using the following: > class(a) [1] "numeric"
(The funny [1] indicates that the output is a vector, and “numeric” is the first element.) FINA3295 | Predictive Analytics
1
R Notes Chapter 1. Data Type and Data Entry
C.Y. Ng
In R, the most basic data type is the vector. In this section we start with vectors and simple operations and functions that works on vectors. Other commonly used data types (matrices, arrays, data frames and lists) will be introduced in the next section. Vector The combine function c() is used to form a vector. The following are numeric, character and logical vector assignments: > a b c