Create and analyze data frames

TODO

  • link to dfTransform

Install required packages

car

Create and analyze data frames

Create data frames

From existing variables

   id sex group age  IQ rating
1   1   f     T  25  95      5
2   2   f     T  24  84      5
3   3   f    CG  27  99      3
4   4   m    WL  26 116      5
5   5   f     T  21  98      4
6   6   m    WL  31  83      4
7   7   m    CG  34  88      0
8   8   m    CG  28 110      3
9   9   f     T  24  95      1
10 10   f    WL  29  80      2
11 11   m    CG  32  91      4
12 12   m    WL  27  98      2

Analyze the structure of data frames

[1] 12  6
[1] 12
[1] 6
       id        sex   group       age              IQ             rating     
 Min.   : 1.00   f:6   CG:4   Min.   :21.00   Min.   : 80.00   Min.   :0.000  
 1st Qu.: 3.75   m:6   WL:4   1st Qu.:24.75   1st Qu.: 87.00   1st Qu.:2.000  
 Median : 6.50         T :4   Median :27.00   Median : 95.00   Median :3.500  
 Mean   : 6.50                Mean   :27.33   Mean   : 94.75   Mean   :3.167  
 3rd Qu.: 9.25                3rd Qu.:29.50   3rd Qu.: 98.25   3rd Qu.:4.250  
 Max.   :12.00                Max.   :34.00   Max.   :116.00   Max.   :5.000  
'data.frame':   12 obs. of  6 variables:
 $ id    : int  1 2 3 4 5 6 7 8 9 10 ...
 $ sex   : Factor w/ 2 levels "f","m": 1 1 1 2 1 2 2 2 1 1 ...
 $ group : Factor w/ 3 levels "CG","WL","T": 3 3 1 2 3 2 1 1 3 2 ...
 $ age   : int  25 24 27 26 21 31 34 28 24 29 ...
 $ IQ    : num  95 84 99 116 98 83 88 110 95 80 ...
 $ rating: num  5 5 3 5 4 4 0 3 1 2 ...
  id sex group age  IQ rating
1  1   f     T  25  95      5
2  2   f     T  24  84      5
3  3   f    CG  27  99      3
4  4   m    WL  26 116      5
5  5   f     T  21  98      4
6  6   m    WL  31  83      4
   id sex group age  IQ rating
7   7   m    CG  34  88      0
8   8   m    CG  28 110      3
9   9   f     T  24  95      1
10 10   f    WL  29  80      2
11 11   m    CG  32  91      4
12 12   m    WL  27  98      2
   id sex group age  IQ rating
4   4   m    WL  26 116      5
6   6   m    WL  31  83      4
8   8   m    CG  28 110      3
9   9   f     T  24  95      1
11 11   m    CG  32  91      4

Data types in data frames

'data.frame':   3 obs. of  3 variables:
 $ fac: chr  "CG" "T1" "T2"
 $ DV1: num  14 22 18
 $ DV2: chr  "red" "blue" "blue"
'data.frame':   3 obs. of  3 variables:
 $ fac: Factor w/ 3 levels "CG","T1","T2": 1 2 3
 $ DV1: num  14 22 18
 $ DV2: chr  "red" "blue" "blue"

Names of cases and variables

[[1]]
 [1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10" "11" "12"

[[2]]
[1] "id"     "sex"    "group"  "age"    "IQ"     "rating"
[1] "id"     "sex"    "group"  "age"    "IQ"     "rating"
 [1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10" "11" "12"

Select and change observations

Basic indexing method

 [1] T  T  CG WL T  WL CG CG T  WL CG WL
Levels: CG WL T
[1] T
Levels: CG WL T
 [1] 5 5 3 5 4 4 0 3 1 2 4 2
[1] 26
[1] 27
[1] WL
Levels: CG WL T
  id sex group age IQ rating
2  2   f     T  24 84      5
 [1] 25 24 27 26 21 31 34 28 24 29 32 27
   age
1   25
2   24
3   27
4   26
5   21
6   31
7   34
8   28
9   24
10  29
11  32
12  27

See dfTransform for selecting subsets of data more conveniently

Work with variables from a data frame

    CG     WL      T 
101.75  96.75  93.00 
   group
sex CG WL T
  f  1  1 4
  m  3  3 0

Detach (automatically) loaded packages (if possible)

Get the article source from GitHub

R markdown - markdown - R code - all posts