Unformatted text preview:

R Data TypesR supports a few basic data types: integer, numeric, logical, character/string, factor, andcomplexLogical – binary, two possible valuesrepresented by TRUE and FALSE> x = c(3, 7, 1, 2)> x > 2[1] TRUE TRUE FALSE FALSE> x == 2[1] FALSE FALSE FALSE TRUE> !(x < 3)[1] TRUE TRUE FALSE FALSE> which(x > 2)[1] 1 2– Typeset by FoilTEX – 1Character vectorsCharacter/string – each element in the vector is a string of one or more characters.Built in character vectors areletters and LETTERS which provide the 26 lower (and upper)case letters, respecitively.> y = c("a", "bc", "def")> length(y)[1] 3> nchar(y)[1] 1 2 3> y == "a"[1] TRUE FALSE FALSE> y == "b"[1] FALSE FALSE FALSE– Typeset by FoilTEX – 2FactorA factor- type vector contains a set of numeric codes with character-valued levels.Example - a family of two girls (1) and four boys (0),> kids = factor(c(1,0,1,0,0,0), levels = c(0, 1),labels = c("boy", "girl"))> kids[1] girl boy girl boy boy boyLevels: boy girl> class(kids)[1] "factor"> mode(kids)[1] "numeric"– Typeset by FoilTEX – 3Regardless of the levels/labels of the factor, the numeric storage is an integer with 1corresponding to the first level (in alph-order).> kids + 1[1] NA NA NA NA NA NA> as.numeric(kids)[1] 2 1 2 1 1 1> 1 + as.numeric(kids)[1] 3 2 3 2 2 2> kids2 = factor(c("boy","girl","boy","girl","boy","boy"))> kids2[1] boy girl boy girl boy boyLevels: boy girl> as.numeric(kids2)[1] 1 2 1 2 1 1– Typeset by FoilTEX – 4Coercion• All elements in a vectors must be of the same type.• R coerces the elements to a common type, in thisc(1.2, 3, TRUE) – In this case all elements are coerced to numeric, 1.2, 3, and 1.> x = c(TRUE, FALSE, TRUE)> c(1.2, x)[1] 1.2 1.0 0.0 1.0> y = c("2", "3", ".2")> c(1.2, y, x)[1] "1.2" "2" "3" ".2" "TRUE" "FALSE" "TRUE"• Sometimes this coercion occurs inorder to perform an arithmetic operation:> 1 + x [1] 2 1 2• Other times we need to perform the coercion> c(1.2, y) [1] "1.2" "2" "3" ".2"> c(1.2, as.numeric(y)) [1] 1.2 2.0 3.0 0.2– Typeset by FoilTEX – 5Functions to Provide Information about Vectors• length(x) - number of elements in a vector or list• Aggregator functions - sum, mean, range, min, max, summary, table, cut, ...• class(x) – returns the type of an object.• is.logical(x) – tells us whether the object is a logical type. There is also is.numeric,is.character, is.integer• is.null – determines whether an object is empty, i.e. has no content. ’NULL’ is used mainlyto represent the lists with zero length, and is often returned by expressions and functionswhose value is undefined.• is.na – NA represents a value that is not available.> x[1] 3 1 NA> is.na(x)[1] FALSE FALSE TRUE• as.numeric(x) – we use the as-type functions to coerce objects from one type (e.g. logical)to another, in this case numeric. There are several of these functions, includingas.integer,as.character, as.logical, as.POSIXct.– Typeset by FoilTEX – 6Missing Values• NA is different from 99999 or -8, which are numeric values that have special meaning in aparticular context• NA is a recognized element in Rx = c(3, 1, NA)• Functions have special actions when they encounter values of NA, and may havearguments to control the handling of NAs.> mean(x)[1] NA> mean(x,na.rm = TRUE)[1] 2• Note that NA is not a character value. In facti, it has meaning for character vectors too.y = c(“A”, “d”, NA, “ab”, “NA”)Notice that the two uses, NA and ”“NA” mean very different things. The first is an NA valueand the second is a character string.• na.omit(), na.exclude(), and na.fail() are for dealling manually with NAs in a dataset.– Typeset by FoilTEX – 7Logical OperatorsLogical operators are extremely useful in subsetting vectors and in controlling program flow.We will cover these ideas soon.• The usual arithemtic operators return logicals >, <, >=, <=, ==, and ! =• Work element-wise on the two inputs.• Output is a vector of logical elements (TRUE and FALSE) where the elements correspondto the test on the relevant elements on the inputs• Conditions can be combined with the use of & for AND and | for OR.• any() returns TRUE if any of the values are TRUE, and all() returns TRUE if all of thevalues are TRUE.– Typeset by FoilTEX – 8The object x versus the character string ”x”> x = c(a = 3, z = 7, 1, 2)> y = c("a", "bc", "NA")> z = c(TRUE, FALSE, FALSE, TRUE)What is the return value for each of the following expressions?• nchar(y)• nchar(”y”)• x + 2• x + z• c(x, NA)• c(x, ”NA”)• x[z]• x[”z”]• x[x]• is.na(y)• is.na(x[x])– Typeset by FoilTEX – 9Return values> nchar(y)[1] 1 2 2> nchar("y")[1] 1> x + 2a z5 9 3 4> x + za z4 7 1 3> c(x, NA)a z3 7 1 2 NA> c(x, "NA")– Typeset by FoilTEX – 10a z"3" "7" "1" "2" "NA"> x[z]a3 2> x["z"]z7> is.na(y)[1] FALSE FALSE FALSE> x[x]<NA> a z1 NA 3 7> is.na(x[x])<NA> a zFALSE TRUE FALSE FALSE– Typeset by FoilTEX – 11Vectors, Matrices, Arrays, Lists, and Data FramesVector – a collection of ordered homogeneous elements.We can think of matrices, arrays, lists and data frames as deviations from a vector. Thedeviaitions are related to the two characteristicsorder and homogeneity.Matrix - a vector with two-dimensional shape information.> xx = matrix(1:6, nrow=3, ncol =2)> xx[,1] [,2][1,] 1 4[2,] 2 5[3,] 3 6> class(x) [1] "numeric"> is.vector(xx) [1] FALSE> is.matrix(xx) [1] TRUE> length(xx) [1] 6> dim(xx) [1] 3 2– Typeset by FoilTEX – 12> yy = array(1:12, c(2,3,2))> yy, , 1[,1] [,2] [,3][1,] 1 3 5[2,] 2 4 6, , 2[,1] [,2] [,3][1,] 7 9 11[2,] 8 10 12> length(yy) [1] 12> dim(yy) [1] 2 3 2> is.matrix(yy) [1] FALSE> is.array(yy) [1] TRUE– Typeset by FoilTEX – 13ListsA vector with possible heterogeneous elements. The elements of a list can be numericvectors, character vectors, matrices, arrays, and lists.myList = list(a = 1:10, b = ”def”, c(TRUE, FALSE, TRUE))$a[1] 1 2 3 4 5 6 7 8 9 10$b[1] "def"[[3]][1] TRUE FALSE TRUE• length(myList) – there are 3 elements in the list• class(myList) – the class is a “list”• names(myList) – are “a”, “b” and the empty character “”• myList[1:2] – returns a list with two elements• myList[1] – returns a list with one element. What is length(myList[1]) ?• myList[[1]] – returns a vector with ten elements, the numbers 1, 2, ..., 10 What islength(myList[[1]]) ?– Typeset by FoilTEX – 14Data FramesA list with possible heterogeneous vector


View Full Document
Download Character vectors
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Character vectors and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Character vectors 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?