ECON 413
Data types and data objects
Erol
Taymaz
Department of Economics
Middle East Technical
University
Base R and most R packages are available at cran.r-project.org
There are thousands of R packages
Objects
Everything in R is an object
## [1] 1 2 3 4 5
## [1] 15
## function (..., na.rm = FALSE) .Primitive("sum")
##
## Call:
## lm(formula = a ~ b)
##
## Coefficients:
## (Intercept) b
## 0.06506 0.42404
## List of 12
## $ coefficients : Named num [1:2] 0.0651 0.424
## ..- attr(*, "names")= chr [1:2] "(Intercept)" "b"
## $ residuals : Named num [1:100] 0.5182 1.0284 0.7242 0.0114 -0.062 ...
## ..- attr(*, "names")= chr [1:100] "1" "2" "3" "4" ...
## $ effects : Named num [1:100] 0.2518 -5.9784 0.6507 -0.082 -0.0843 ...
## ..- attr(*, "names")= chr [1:100] "(Intercept)" "b" "" "" ...
## $ rank : int 2
## $ fitted.values: Named num [1:100] -0.00219 0.40228 0.14876 0.2787 -0.18484 ...
## ..- attr(*, "names")= chr [1:100] "1" "2" "3" "4" ...
## $ assign : int [1:2] 0 1
## $ qr :List of 5
## ..$ qr : num [1:100, 1:2] -10 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 ...
## .. ..- attr(*, "dimnames")=List of 2
## .. .. ..$ : chr [1:100] "1" "2" "3" "4" ...
## .. .. ..$ : chr [1:2] "(Intercept)" "b"
## .. ..- attr(*, "assign")= int [1:2] 0 1
## ..$ qraux: num [1:2] 1.1 1.07
## ..$ pivot: int [1:2] 1 2
## ..$ tol : num 1e-07
## ..$ rank : int 2
## ..- attr(*, "class")= chr "qr"
## $ df.residual : int 98
## $ xlevels : Named list()
## $ call : language lm(formula = a ~ b)
## $ terms :Classes 'terms', 'formula' language a ~ b
## .. ..- attr(*, "variables")= language list(a, b)
## .. ..- attr(*, "factors")= int [1:2, 1] 0 1
## .. .. ..- attr(*, "dimnames")=List of 2
## .. .. .. ..$ : chr [1:2] "a" "b"
## .. .. .. ..$ : chr "b"
## .. ..- attr(*, "term.labels")= chr "b"
## .. ..- attr(*, "order")= int 1
## .. ..- attr(*, "intercept")= int 1
## .. ..- attr(*, "response")= int 1
## .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
## .. ..- attr(*, "predvars")= language list(a, b)
## .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
## .. .. ..- attr(*, "names")= chr [1:2] "a" "b"
## $ model :'data.frame': 100 obs. of 2 variables:
## ..$ a: num [1:100] 0.516 1.431 0.873 0.29 -0.247 ...
## ..$ b: num [1:100] -0.159 0.795 0.197 0.504 -0.589 ...
## ..- attr(*, "terms")=Classes 'terms', 'formula' language a ~ b
## .. .. ..- attr(*, "variables")= language list(a, b)
## .. .. ..- attr(*, "factors")= int [1:2, 1] 0 1
## .. .. .. ..- attr(*, "dimnames")=List of 2
## .. .. .. .. ..$ : chr [1:2] "a" "b"
## .. .. .. .. ..$ : chr "b"
## .. .. ..- attr(*, "term.labels")= chr "b"
## .. .. ..- attr(*, "order")= int 1
## .. .. ..- attr(*, "intercept")= int 1
## .. .. ..- attr(*, "response")= int 1
## .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
## .. .. ..- attr(*, "predvars")= language list(a, b)
## .. .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
## .. .. .. ..- attr(*, "names")= chr [1:2] "a" "b"
## - attr(*, "class")= chr "lm"
## (Intercept) b
## 0.06506165 0.42403757
## [1] -2.488895e-17
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## -2.35313 -0.63566 0.05574 -0.02518 0.63855 1.58078
##
## Call:
## lm(formula = a ~ b)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.70619 -0.47993 0.00742 0.49945 1.31451
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.06506 0.06712 0.969 0.335
## b 0.42404 0.04707 9.008 1.72e-14 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.6637 on 98 degrees of freedom
## Multiple R-squared: 0.453, Adjusted R-squared: 0.4474
## F-statistic: 81.14 on 1 and 98 DF, p-value: 1.719e-14
A function’s output depends on the object!
Object type: typeof()
is functions: is.xx()
as functions: as.xx()
``
All objects belong to one or more classes. There is no limit on the number of classes.
The class of an object defines how the object will be treated by functions.
## [1] "integer"
## [1] "character"
## [1] "logical"
## [1] "character"
## [1] "numeric"
## [1] "data.frame"
## [1] "integer"
## [1] "matrix" "array"
## [1] "matrix" "array"
## [1] 1 2
## [1] 1 2 3 NA Inf -Inf NaN
c
, rep
, seq
,
sample
and runif
functions
## [1] "numeric"
## num [1:3] 1 2 4
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1.000 1.500 2.000 2.333 3.000 4.000
## [1] 1 2 3 4 5
## [1] 5 4 3 2 1
## [1] 1 2 3 4 5 5 4 3 2 1
a <- rep(c(1:2), times = 5)
b <- rep(c(1:2), each = 3)
d <- rep(c(1:2), times = 2, each = 3)
e <- rep(c(1:2), len = 5)
a
## [1] 1 2 1 2 1 2 1 2 1 2
## [1] 1 1 1 2 2 2
## [1] 1 1 1 2 2 2 1 1 1 2 2 2
## [1] 1 2 1 2 1
## [1] 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0
## [1] 1.000000 1.166667 1.333333 1.500000 1.666667 1.833333 2.000000
## [1] 2 4 1 2 3 1 2 1 1 3
## [1] 2 7 8 6 10
## [1] 0.8401158 0.7025864 0.0280010 0.9007178 0.8202461 0.3120585 0.8927049
## [8] 0.9495726 0.5212997 0.6776818
## [1] 0.2875775 0.7883051 0.4089769 0.8830174 0.9404673 0.0455565 0.5281055
## [8] 0.8924190 0.5514350 0.4566147
## [1] 0 0 0 0 0 0 0 0 0 0
## [1] 0 0 0 0 0 0 0 0 0 0
## [1] TRUE
## [1] "" "" "" "" "" "" "" "" "" ""
## [1] NA NA NA NA NA NA NA NA NA NA
## [1] TRUE TRUE TRUE TRUE
## [1] 7 11 17 19
## [1] 4 16 10 20
## Warning in mt2 + fin: longer object length is not a multiple of shorter object
## length
## [1] 10 13 18 19 14
## [1] 9
## [1] 10
## [1] 4 6 10 10
## [1] 5 5 7 9
## [1] 1
## [1] 1 NA
## [1] 1 40
## [1] 1 2 4 40 NA NA NA NA NA 10
## Ali Ayşe Fatma
## 3 5 7
## Ayşe
## 5
## Ayşe Ali
## 5 3
## $a
## [1] 1 2 3 4 5 6 7 8 9 10
##
## $b
## [1] "a"
##
## $c
## [1] TRUE FALSE TRUE FALSE
## Length Class Mode
## a 10 -none- numeric
## b 1 -none- character
## c 4 -none- logical
## $a
## [1] 1 2 3 4 5 6 7 8 9 10
## [1] 1 2 3 4 5 6 7 8 9 10
## [1] "list"
## [1] "integer"
## [1] 3
## [1] TRUE
a <- matrix(1:15, ncol = 3, nrow = 5)
b <- matrix(c("a", "b", "c", "d", "e", "f"), ncol = 3, nrow = 2)
a
## [,1] [,2] [,3]
## [1,] 1 6 11
## [2,] 2 7 12
## [3,] 3 8 13
## [4,] 4 9 14
## [5,] 5 10 15
## [,1] [,2] [,3]
## [1,] "a" "c" "e"
## [2,] "b" "d" "f"
## [1] "matrix" "array"
## int [1:5, 1:3] 1 2 3 4 5 6 7 8 9 10 ...
## V1 V2 V3
## Min. :1 Min. : 6 Min. :11
## 1st Qu.:2 1st Qu.: 7 1st Qu.:12
## Median :3 Median : 8 Median :13
## Mean :3 Mean : 8 Mean :13
## 3rd Qu.:4 3rd Qu.: 9 3rd Qu.:14
## Max. :5 Max. :10 Max. :15
## [,1] [,2] [,3]
## [1,] 1 6 11
## [2,] 2 7 12
## [3,] 3 8 13
## [4,] 4 9 14
## [5,] 5 10 15
## [,1] [,2] [,3]
## [1,] 1 6 11
## [2,] 3 8 13
## [1] 1 15
## [1] 6 7 8 9 10
## [1] 6 8
An array is a multidimensional object. A matrix is an nxm dimensional array.
## [,1] [,2]
## [1,] 1 7
## [2,] 2 8
## [3,] 3 9
## [4,] 4 10
## [5,] 5 11
## [6,] 6 12
## [1] "matrix" "array"
## , , 1
##
## [,1] [,2] [,3]
## [1,] 1 5 9
## [2,] 2 6 10
## [3,] 3 7 11
## [4,] 4 8 12
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 13 17 21
## [2,] 14 18 22
## [3,] 15 19 23
## [4,] 16 20 24
## [1] "array"
## [1] 7
aa <- data.frame(a = 1:4, b = c("a", "b", "c", "d"),
z = c(1, 3, 5, NA))
bb <- data.frame(a = 1, b = c("A", "B", "C", "D"), z = "Z",
stringsAsFactors = FALSE)
aa
## a b z
## 1 1 a 1
## 2 2 b 3
## 3 3 c 5
## 4 4 d NA
## a b z
## 1 1 A Z
## 2 1 B Z
## 3 1 C Z
## 4 1 D Z
## [1] "data.frame"
## [1] "a" "b" "z"
## 'data.frame': 4 obs. of 3 variables:
## $ a: int 1 2 3 4
## $ b: chr "a" "b" "c" "d"
## $ z: num 1 3 5 NA
## a b z
## Min. :1.00 Length:4 Min. :1
## 1st Qu.:1.75 Class :character 1st Qu.:2
## Median :2.50 Mode :character Median :3
## Mean :2.50 Mean :3
## 3rd Qu.:3.25 3rd Qu.:4
## Max. :4.00 Max. :5
## NA's :1
## a b z a b z
## 1 1 a 1 1 A Z
## 2 2 b 3 1 B Z
## 3 3 c 5 1 C Z
## 4 4 d NA 1 D Z
## a b z
## 1 1 a 1
## 2 2 b 3
## 3 3 c 5
## 4 4 d <NA>
## 5 1 A Z
## 6 1 B Z
## 7 1 C Z
## 8 1 D Z
Use the merge function to merge two data frames
## [1] 1 2 3 4
## [1] 1 2 3 4
## [1] 1 2 3 4
## a
## 1 1
## 2 2
## 3 3
## 4 4
## [1] 1 2 3 4
## [1] 1 2 3 4
## a
## 1 1
## 2 2
## 3 3
## 4 4
## a b z
## 1 1 a 1
## a b z
## 1 1 a 1
## 2 2 b 3
## a b z
## 1 1 a 1
## 3 3 c 5
## [1] "a" "b"
## [1] "a" "b"
## [1] "a" "b"
## [1] "a" "b"
## a b z x y
## 1 1 a 1 4.0 4.0
## 2 2 b 3 8.0 16.0
## 3 3 c 5 1.5 4.5
## 4 4 d NA 7.0 28.0
## a b z y
## 1 1 a 1 4.0
## 2 2 b 3 16.0
## 3 3 c 5 4.5
## 4 4 d NA 28.0
## a b z y
## 1 1 a 1 2.000000
## 2 2 b 3 4.000000
## 3 3 c 5 2.121320
## 4 4 d NA 5.291503
## a b z y
## 1 1 a 1 10.000000
## 2 2 b 3 4.000000
## 3 3 c 5 2.121320
## 4 4 d NA 5.291503
## a b z y
## 1 1 aaa 1 10.000000
## 2 2 b 3 4.000000
## 3 3 c 5 2.121320
## 4 4 d NA 5.291503
## a b z y
## 3 3 c 5 2.121320
## 4 4 d NA 5.291503
## a b z
## 3 3 c 5
## 4 4 d NA
## a b z
## 1 1 aaa 1
## 2 2 b 3
## a b z y
## 1 1000 1000 1000 10.000000
## 2 2 b 3 4.000000
## 3 3 c 5 2.121320
## 4 4 d NA 5.291503
## a b z y
## 1 1000 1000 1000 10.00000
## 2 2 b 3 4.00000
## 3 3 c 5 2.12132
Be very careful in using na.omit!
GDP growth rate data
Country | 2000 | 2001 | 2002 | 2003 | 2004 |
---|---|---|---|---|---|
Germany | 0.0409 | 0.0013 | 0.0126 | 0.0126 | 0.0001 |
Korea | 0.0262 | 0.0232 | 0.0186 | 0.0244 | 0.0146 |
Turkey | 0.0384 | 0.0011 | 0.0099 | 0.0399 | 0.0316 |
US | 0.0220 | 0.0107 | 0.0143 | 0.0318 | 0.0274 |
How many variables are there in the data?
How many observations?
How many variables? 3 variables (country, year, gdp growth rate)
How many observations? 4x5 = 20 observations
GDP growth rate data - long format
Country | year | gdpgr |
---|---|---|
Germany | 2000 | 0.0013 |
Germany | 2001 | 0.0013 |
Germany | 2002 | 0.0126 |
Germany | 2003 | 0.0116 |
Use the reshape function to convert wide-to-long and long-to-wide