Edited for clarity (#23034)

Minor changes to wording to stay consistent with professional standards.
This commit is contained in:
DataCowboy
2018-11-29 20:34:56 -05:00
committed by Christopher McCormack
parent fe204ef8cb
commit abeb1af705

View File

@ -1,103 +1,103 @@
--- ---
title: Data Types in R title: Data Types in R
--- ---
## Scalars ## Scalars
Scalar refers to an atomic quantity that can hold only one value at a time. Scalars are the most basic data types. Some common types of scalars : Scalar refers to an atomic quantity that can hold only one value at a time. Scalars are the most basic data types. Some common types of scalars :
1. Number 1. Number
```r ```r
> x <- 5 > x <- 5
> y <- 5.5 > y <- 5.5
> class(x) > class(x)
[1] "numeric" [1] "numeric"
> class(y) > class(y)
[1] "numeric" [1] "numeric"
> class(x+y) > class(x+y)
[1] "numeric" [1] "numeric"
``` ```
2. Logical value 2. Logical value
```r ```r
> m <- x > y # Used to check, Is x larger than y? > m <- x > y # Used to check, Is x larger than y?
> n <- x < y # Used to check, Is x smaller than y? > n <- x < y # Used to check, Is x smaller than y?
> m > m
[1] FALSE [1] FALSE
> n > n
[1] TRUE [1] TRUE
> class(m) > class(m)
[1] "logical" [1] "logical"
> class(NA) # NA is another logical value: 'Not Available'/Missing Values > class(NA) # NA is another logical value: 'Not Available'/Missing Values
[1] "logical" [1] "logical"
``` ```
3. Character(string) 3. Character(string)
```r ```r
> a <- "1"; b <- "2.5" > a <- "1"; b <- "2.5"
> a;b > a;b
[1] "1" [1] "1"
[1] "2.5" [1] "2.5"
> a+b > a+b
Error in a + b : non-numeric argument to binary operator Error in a + b : non-numeric argument to binary operator
> class(a) > class(a)
[1] "character" [1] "character"
> class(as.numeric(a)) > class(as.numeric(a))
[1] "numeric" [1] "numeric"
> class(as.character(x)) > class(as.character(x))
[1] "character" [1] "character"
``` ```
## Vector ## Vector
It is a sequence of data elements of the same basic type. For example: Vectors are sequences of data elements of the same basic type. For example:
> o <- c(1,2,5.3,6,-2,4) # Numeric vector > o <- c(1,2,5.3,6,-2,4) # Numeric vector
> p <- c("one","two","three","four","five","six") # Character vector > p <- c("one","two","three","four","five","six") # Character vector
> q <- c(TRUE,TRUE,FALSE,TRUE,FALSE,TRUE) # Logical vector > q <- c(TRUE,TRUE,FALSE,TRUE,FALSE,TRUE) # Logical vector
> o;p;q > o;p;q
[1] 1.0 2.0 5.3 6.0 -2.0 4.0 [1] 1.0 2.0 5.3 6.0 -2.0 4.0
[1] "one" "two" "three" "four" "five" "six" [1] "one" "two" "three" "four" "five" "six"
[1] TRUE TRUE FALSE TRUE FALSE [1] TRUE TRUE FALSE TRUE FALSE
## Matrix ## Matrix
It is a two-dimensional rectangular data set. The components in a matrix also must be of the same basic type like vector. For example: A matrix is a two-dimensional rectangular data set. The components in a matrix must be of the same basic type. For example:
> m = matrix( c('a','a','b','c','b','a'), nrow = 2, ncol = 3, byrow = TRUE) > m = matrix( c('a','a','b','c','b','a'), nrow = 2, ncol = 3, byrow = TRUE)
> m > m
>[,1] [,2] [,3] >[,1] [,2] [,3]
[1,] "a" "a" "b" [1,] "a" "a" "b"
[2,] "c" "b" "a" [2,] "c" "b" "a"
## Data Frame ## Data Frame
It is more general than a matrix, in that different columns can have different basic data types. For example: A data frame is more general than a matrix, in that different columns can have different basic data types. For example:
> d <- c(1,2,3,4) > d <- c(1,2,3,4)
> e <- c("red", "white", "red", NA) > e <- c("red", "white", "red", NA)
> f <- c(TRUE,TRUE,TRUE,FALSE) > f <- c(TRUE,TRUE,TRUE,FALSE)
> mydata <- data.frame(d,e,f) > mydata <- data.frame(d,e,f)
> names(mydata) <- c("ID","Color","Passed") > names(mydata) <- c("ID","Color","Passed")
> mydata > mydata
ID Color Passed ID Color Passed
1 1 red TRUE 1 1 red TRUE
2 2 white TRUE 2 2 white TRUE
3 3 red TRUE 3 3 red TRUE
4 4 <NA> FALSE 4 4 <NA> FALSE
## Lists ## Lists
It is an R-object which can contain many different types of elements inside it like vectors, functions and even another list inside it. For example: Lists are R-objects which can contain many different types of elements inside them like vectors, functions and even another list. For example:
> list1 <- list(c(2,5,3),21.3,sin) > list1 <- list(c(2,5,3),21.3,sin)
> list1 > list1
[[1]] [[1]]
[1] 2 5 3 [1] 2 5 3
[[2]] [[2]]
[1] 21.3 [1] 21.3
[[3]] [[3]]
function (x) .Primitive("sin") function (x) .Primitive("sin")
## Reference: ## Reference:
* [Official Docs](https://cran.r-project.org/manuals.html) * [Official Docs](https://cran.r-project.org/manuals.html)
* [Data Types in R by r-bloggers](https://www.r-bloggers.com/classes-and-objects-in-r/) * [Data Types in R by r-bloggers](https://www.r-bloggers.com/classes-and-objects-in-r/)