-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrbasics.html
More file actions
executable file
·1277 lines (976 loc) · 31.6 KB
/
rbasics.html
File metadata and controls
executable file
·1277 lines (976 loc) · 31.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="" xml:lang="">
<head>
<title>MAT381E-Week 1: Introduction to Data Science</title>
<meta charset="utf-8" />
<meta name="author" content="Gül İnan" />
<meta name="date" content="2021-10-11" />
<script src="rbasics_files/header-attrs-2.11/header-attrs.js"></script>
<link href="rbasics_files/remark-css-0.0.1/default.css" rel="stylesheet" />
<link rel="stylesheet" href="xaringan-themer.css" type="text/css" />
</head>
<body>
<textarea id="source">
class: left, middle, my-title, title-slide
# MAT381E-Week 1: Introduction to Data Science
### Gül İnan
### Department of Mathematics<br/>Istanbul Technical University
### October 11, 2021
---
class: center, middle
# R Basics
---
# Knowing your way around RStudio
- The `RStudio` IDE a free open-source product (under the
[Affero General Public License (AGPL) v3](https://www.gnu.org/licenses/agpl-3.0.en.html)).
- We will use `RStudio` as a graphical front-end to `R`, so that we can access our scripts and data, find help, and preview plots and outputs all in one place.
- To function correctly, `RStudio` needs `R` and therefore **both** need to be installed on your computer.
- You can download the recent version of `R` from [CRAN](https://cran.r-project.org/). Select the link appropriate for your operating system.
- Then, download the recent version of `RStudio` from the [RStudio](https://www.rstudio.com/) (select the free open source desktop version).
- If you are using a Mac, in addition to `R` and `RStudio`, you need to download [XQuartz](https://www.xquartz.org/).
- Visit [R/RStudio set up video](https://tutorials.shinyapps.io/00-setup/).
---
<style type="text/css">
.pull-left {
float: left;
width: 50%;
}
.pull-right {
float: right;
width: 50%;
}
</style>
- `RStudio` is divided into 4 "Panes" (small windows):
- The **Source** for your scripts and documents (top-left, in the default layout),
- The R **Console** (bottom-left),
- Your **Environment/History** (top-right), and
- Your **Files/Plots/Packages/Help/Viewer** (bottom-right).
---
class: middle, center
<div class="figure">
<img src="images/ide.png" alt="RStudio Interface" width="70%" />
<p class="caption">RStudio Interface</p>
</div>
---
# How to start an R project?
- It is **always good practice** to keep a set of related data and analyses in **a single folder**.
* Start `RStudio`.
* Under the `File` menu, click on `New project`, choose `New directory`, then
`New project`.
* As directory (or folder) name enter `r-intro` and create project as sub-directory of your desktop folder: `~/Desktop`.
* Click on `Create project`.
* Under the `Files` tab on the right of the screen, click on `New Folder` and
create a folder named `data` within your newly created working directory (e.g., `~/r-intro/data`).
* On the main menu go to `Files` > `New File` > `R Script` to open a new file and
* Save the empty script as `r-intro-script.R` in your working directory.
---
- A quick side note: If you are interested in file management/data science work flow, I suggest you to read the following article:
http://www.rebeccabarter.com/blog/2019-03-07_reproducible_pipeline/.
---
- If you need to check which working directory `R` thinks it is in:
```r
getwd()
[1] "/Users/gulinan/Desktop/r-intro"
```
- To set a different working directory in `R` go to the `Console` and type:
```r
setwd("Path/To/Your/Newworkingdirectory")
```
---
# RStudio Console and Command Prompt
- The console pane in `RStudio` is the place where **commands written** in the `R`
language can be **typed** and **executed** immediately by the computer.
- It is also where
the **results will be shown** for commands that have been executed.
- You can type
commands directly into the console and press <kbd>`Enter`</kbd> to execute those commands, but they will be **forgotten when you close the session**.
- If `R` is ready to accept commands, the `R` console by default shows a `>` prompt.
- If it
receives a command, `R` will try to execute it, and when
ready, will **show the results** and come back with a new `>` prompt to wait for new
commands.
- If `R` is **still waiting** for you to enter more data because it isn't complete yet,
the console will show a `+` prompt.
---
# RStudio Script Editor
- Because we want to keep our code and workflow properly, it is better to type
the commands in the **script editor**.
- We can create a new `R` script as follows: `File > New File > R Script` and then **save the script**.
- We can now write **commands** directly in the `R Script`.
```r
getwd()
[1] "/Users/gulinan/Desktop/r-intro"
```
- The command on the current line in the
script (indicated by the cursor) or all of the commands in the **currently
selected text** will be sent to the console and executed when you press
<kbd>`Ctrl`</kbd> + <kbd>`Enter`</kbd> (<kbd>`Cmd`</kbd> +
<kbd>`Enter`</kbd> on Mac).
---
# Creating objects in R
- To **create an `R` object**, we need to give it a name followed by the
assignment operator `<-` and the value we want to give it:
```r
my_age <- 25
```
- `<-` is the **assignment operator** and as name suggests it assigns values on the right to objects on
the left.
- You can also use `=`
for assignments, but not in every context (there are
[slight](http://blog.revolutionanalytics.com/2008/12/use-equals-or-arrow-for-assignment.html) [differences](https://web.archive.org/web/20130610005305/https://stat.ethz.ch/pipermail/r-help/2009-March/191462.html)).
- Just a note: What are known as `objects` in `R` are known as `variables` in many other
programming languages. Depending on the context, `object` and `variable` can
have drastically different meanings. However, in this lesson, the two words are
used synonymously.
---
# How to name objects in R?
- Object names must start with a letter or a dot (if you start a name with a dot, the second character cannot be a number).
- Names should contain only letters, numbers, underscore characters (_), and dots (.).
- Objects can be given any name such as `x`, `current_temperature`, or
`subject_id`.
- Object names should be **explicit** and **not too long**.
- R is **case sensitive**
(e.g., `weight_kg` is different from `Weight_kg`).
- There are some names that
cannot be used because they are the names of fundamental functions in `R` (e.g., `if`, `else`, `for`, see
[here](https://stat.ethz.ch/R-manual/R-devel/library/base/html/Reserved.html) for a complete list).
- In general, even if it is allowed, it's **best to not use other function names** (e.g., `c`, `T`, `mean`, `data`, `df`, `weights`). If in doubt, check the help to see if the name is already in use.
- It's also best to
avoid dots (`.`) within a variable name as in `my.dataset`. There are many
functions in R with dots in their names for historical reasons, but because dots have a special meaning in `R` (for methods) and other programming languages, it is best to avoid them.
---
- When assigning a value to an object, `R` does not print anything.
- We can force `R` to print the value by **using parentheses** or by **typing the object name**:
```r
weight_kg <- 55 # doesn't print anything
```
```r
(weight_kg <- 55) # but putting parenthesis around the call prints the value of `weight_kg`
```
```
#> [1] 55
```
```r
weight_kg # and so does typing the name of the object
```
```
#> [1] 55
```
- Now that `R` has `weight_kg` in memory, we can do arithmetic with it.
- For instance, we may want to convert this weight into pounds (weight in pounds is 2.2 times the weight in kg):
```r
weight_kg * 2.2
```
```
#> [1] 121
```
---
# Comments
<img src="images/commenting.png" width="80%" height="100%" />
--
- The **comment character** in `R` is hashtag (`#`).
- Anything to the right of a `#` in a script
will be ignored by `R`.
- If you only want to comment
out one line, you can put the cursor at any location of that line (i.e. no need
to select the whole line), then press <kbd>Ctrl</kbd> + <kbd>Shift</kbd> +
<kbd>C</kbd>.
- `RStudio` makes it easy to comment or uncomment a paragraph: after selecting the
lines you want to comment, press at the same time on your keyboard
<kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>C</kbd>.
---
# Modes
- Objects in `R` are classified according to their basic structure which is called as **modes**.
- Table below shows the **most common ones**.
|Name |Example |
|:---------|:-----------------------|
|numeric |`1`, `3`, `48` |
|character |`Steve'`, `'a'`, `'78'` |
|logical |`TRUE`, `FALSE` |
- For characters we can either use `" "` or `' '`.
---
# Data types
- `R` has a number of different **data types**.
- Table below shows the ones you're most likely to come across (taken from [this source](https://www.statmethods.net/input/datatypes.html)):
|Name |Description |
|:----------|:-----------------------------------------------------------------------------|
|vector |list of values with of the same variable mode |
|matrix |2D data structure |
|array |same as matrix for higher dimensional data |
|data frame |matrices in that different columns can have different modes |
|list |flexible type that can contain different variable types with diffrent lenghts |
---
# Vectors
- A vector is **composed by a series of values** where all of the values are the **same type of data**.
- We build vectors using the **concatenate function** `c()`.
```r
weight_g <- c(21, 34, 39, 54, 55) # make a vector
weight_g
```
```
#> [1] 21 34 39 54 55
```
- You can use the `c()` function to add other elements to your vector:
```r
weight_g <- c(weight_g, 90) # add to the end of the vector
weight_g
```
```
#> [1] 21 34 39 54 55 90
```
```r
weight_g <- c(30, weight_g) # add to the beginning of the vector
weight_g
```
```
#> [1] 30 21 34 39 54 55 90
```
---
- The function `str()` provides an overview of the internal **str**ucture of an `R` object and its
elements.
- It is a useful function when working with large and complex
objects:
```r
str(weight_g)
```
```
#> num [1:7] 30 21 34 39 54 55 90
```
- The function `class()` indicates the class (the type of element) of an object:
```r
class(weight_g)
```
```
#> [1] "numeric"
```
- Note: `class` is a property assigned to an object that determines how **functions** operate with it.
---
- A vector can also contain **characters**:
```r
animals <- c("mouse", "rat", "dog", "frog")
class(animals)
```
```
#> [1] "character"
```
- A vector can also contain **logical values** (the boolean data type).
```r
has_tail <- c(TRUE, TRUE, TRUE, FALSE)
class(has_tail)
```
```
#> [1] "logical"
```
---
<img src="images/index02.png" width="80%" height="100%" />
--
- `R` **indices start at 1**.
- Programming languages like `Fortran`, `MATLAB`, `Julia`, and `R` start
counting at 1, because that's what human beings typically do.
- Languages in the `C` family (including `C++`, `Java`, `Perl`, and `Python`) **count from 0** because that's simpler for computers to do.
---
# Subsetting vectors
- If we want to **extract one or several values from a vector**, we must provide one
or several indices in square brackets ([]).
```r
animals[2]
```
```
#> [1] "rat"
```
```r
animals[c(3, 2)]
```
```
#> [1] "dog" "rat"
```
- The **colon operator** `:` is a special operator that **creates numeric vectors of integers** in increasing
or decreasing order, test `1:10` and `10:1` for instance.
- We can use this to select a sequence, like this:
```r
animals[2:4]
```
```
#> [1] "rat" "dog" "frog"
```
---
- You can **exclude** elements of a vector using the "`-`" sign:
```r
animals[-2]
```
```
#> [1] "mouse" "dog" "frog"
```
```r
animals[-c(1:3)]
```
```
#> [1] "frog"
```
- We can also repeat the indices to create an object with more elements than the
original one:
```r
more_animals <- animals[c(1, 2, 3, 2, 1, 4)]
more_animals
```
```
#> [1] "mouse" "rat" "dog" "rat" "mouse" "frog"
```
---
# Comparison Operators
- Table below shows the **comparison operators** that result in logical outputs.
<img src="images/comparison.png" width="80%" />
- The double equal sign `==` is a test for
numerical equality between the left and right hand sides, and should not be
confused with the single `=` sign, which performs variable assignment (similar
to `<-`).
---
# Boolean Operators
- Table below shows the **Boolean operators** that are used to narrow or broaden search outputs.
<img src="images/boolean.png" width="80%" />
---
# Conditional subsetting
- Another common way of subsetting is by using a logical vector.
- `TRUE` will
select the element with the same index, while `FALSE` will not.
```r
animals
```
```
#> [1] "mouse" "rat" "dog" "frog"
```
```r
logical_index <- c(TRUE, TRUE, TRUE, FALSE)
animals[logical_index]
```
```
#> [1] "mouse" "rat" "dog"
```
- Typically, these logical vectors are not typed out by hand, but are the output of
other functions or logical tests.
---
- We can select only the weights above 50:
```r
weight_g
```
```
#> [1] 30 21 34 39 54 55 90
```
```r
weight_g > 50 # will return logicals with TRUE for the indices that meet the condition
```
```
#> [1] FALSE FALSE FALSE FALSE TRUE TRUE TRUE
```
```r
## so we can use this to select only the values above 50
weight_g[weight_g > 50]
```
```
#> [1] 54 55 90
```
- We can combine multiple tests using `&` (both conditions are true, AND) or `|`
(at least one of the conditions is true, OR):
```r
weight_g[weight_g < 30 | weight_g > 50]
```
```
#> [1] 21 54 55 90
```
```r
weight_g[weight_g >= 30 & weight_g <= 90]
```
```
#> [1] 30 34 39 54 55 90
```
---
- The **(in) operator** `%in%` is very useful for searching **certain strings in a vector**.
```r
x <- c(1, 2, 3)
2 %in% x #is 2 in x vector
```
```
#> [1] TRUE
```
```r
c(3, 4) %in% x #is elements of c(3, 4) available in x vector
```
```
#> [1] TRUE FALSE
```
```r
animals
```
```
#> [1] "mouse" "rat" "dog" "frog"
```
```r
animals %in% c("rat", "cat", "dog") #is elements of animals vector available in that vector
```
```
#> [1] FALSE TRUE TRUE FALSE
```
---
# Some often used functions
|Name |Description |
|:-------------|:---------------------------------------------------------|
|`length()` |length of an object |
|`dim()` |dimensions of an object (e.g. number of rows and columns) |
|`seq()` |generate a sequence of numbers |
|`rep()` |repeat something n times |
|`max()` |maximum |
|`min()` |minimum |
|`which.max()` |index of the maximum |
|`which.min()` |index of the maximum |
|`mean()` |mean |
|`median()` |median |
|`sum()` |sum |
|`var()` |variance |
|`sd()` |standard deviation |
---
# Missing data
- **Missing data** is represented as `NA`.
- When doing operations on numbers, most functions will return `NA` if the data
you are working with include missing values.
- We can add the argument `na.rm = TRUE` to calculate the result **while ignoring
the missing values**.
```r
heights <- c(2, 4, 4, NA, 6)
max(heights)
```
```
#> [1] NA
```
```r
sum(heights)
```
```
#> [1] NA
```
```r
max(heights, na.rm = TRUE)
```
```
#> [1] 6
```
```r
sum(heights, na.rm = TRUE)
```
```
#> [1] 16
```
---
- If the data include missing values, we may want to become familiar with the
functions `is.na()`, `na.omit()`, `complete.cases()`.
```r
# Extract elements which are not missing values.
heights
```
```
#> [1] 2 4 4 NA 6
```
```r
!is.na(heights)
```
```
#> [1] TRUE TRUE TRUE FALSE TRUE
```
```r
heights[!is.na(heights)]
```
```
#> [1] 2 4 4 6
```
---
```r
# Extract elements which are not missing values.
na.omit(heights)
```
```
#> [1] 2 4 4 6
#> attr(,"na.action")
#> [1] 4
#> attr(,"class")
#> [1] "omit"
```
```r
# Extract elements which are complete cases.
heights[complete.cases(heights)]
```
```
#> [1] 2 4 4 6
```
---
# Matrix
- If we arrange data elements of a vector in a **two-dimensional rectangular layout**, then we have a **matrix**.
- To construct a matrix, we use a function conveniently called `matrix()`.
```r
y <- matrix(data = c(1:20), nrow = 5, ncol = 4) # generates 5 x 4 numeric matrix
y
```
```
#> [,1] [,2] [,3] [,4]
#> [1,] 1 6 11 16
#> [2,] 2 7 12 17
#> [3,] 3 8 13 18
#> [4,] 4 9 14 19
#> [5,] 5 10 15 20
```
- We can subset a matrix with [row `,` column]:
```r
y[,4] # 4th column of matrix
```
```
#> [1] 16 17 18 19 20
```
```r
y[3,] # 3rd row of matrix
```
```
#> [1] 3 8 13 18
```
---
```r
y[-1, ] # a matrix which excludes the first row
```
```
#> [,1] [,2] [,3] [,4]
#> [1,] 2 7 12 17
#> [2,] 3 8 13 18
#> [3,] 4 9 14 19
#> [4,] 5 10 15 20
```
```r
y[2:4,1:3] # rows 2,3,4 of columns 1,2,3
```
```
#> [,1] [,2] [,3]
#> [1,] 2 7 12
#> [2,] 3 8 13
#> [3,] 4 9 14
```
- Note how we use an **empty placeholder** to indicate that we want to **select all the values** in a row or column, and `-` to indicate that we want to **remove** something.
---
# Array
- Arrays work the same was as matrices with data of **more than two dimensions**.
---
# Data frame
- Data frames in `R`, are matrix type data types which can have elements of any type, but they have to **all be of the same length**.
- A data frame is the most common way of storing tabular data in `R` and something you will likely deal with a lot.
- As a first approximation, which holds true, probably in the most cases, you can really think of it as a table or a spreadsheet.
- We use `data.frame()` function to construct a data frame.
```r
mydf <- data.frame("ID" = c(1:4),
"Color" = c("red", "white", "red", NA),
"Passed" = c(TRUE,TRUE,TRUE,FALSE),
"Weight" = c(99, 54, 85, 70),
"Height" = c(1.78, 1.67, 1.82, 1.59))
mydf
```
```
#> ID Color Passed Weight Height
#> 1 1 red TRUE 99 1.78
#> 2 2 white TRUE 54 1.67
#> 3 3 red TRUE 85 1.82
#> 4 4 <NA> FALSE 70 1.59
```
---
- We can access the elements of a data frame as in matrix:
```r
mydf[1, 2] # a single element using numbers
```
```
#> [1] "red"
```
```r
mydf[, 1] # first column in the data frame (as a vector)
```
```
#> [1] 1 2 3 4
```
```r
mydf[3, ] # the 3rd row (as a data.frame)
```
```
#> ID Color Passed Weight Height
#> 3 3 red TRUE 85 1.82
```
```r
mydf[, -1] # the whole data frame, excluding the first column
```
```
#> Color Passed Weight Height
#> 1 red TRUE 99 1.78
#> 2 white TRUE 54 1.67
#> 3 red TRUE 85 1.82
#> 4 <NA> FALSE 70 1.59
```
---
- As well as using numeric values to subset a `data.frame` (or `matrix`), columns
can be accessed by name:
```r
mydf[, "ID"] # all IDs
```
```
#> [1] 1 2 3 4
```
```r
*mydf$ID #all IDs
```
```
#> [1] 1 2 3 4
```
```r
mydf$Color[2] # color of the second one
```
```
#> [1] "white"
```
---
# Some often used functions
|Name |Description |
|:------------|:----------------------------------|
|`dim()` |returns dimensions of the object |
|`nrow()` |returns the number of rows |
|`ncol()` |returns the number of columns |
|`head()` |shows the first 6 rows |
|`tail()` |shows the last 6 rows |
|`colnames()` |returns the number of columns |
|`rownames()` |returns the number of rows |
|`str()` |structure of the object |
|`summary()` |summary statistics for each column |
---
- Here is a list of
functions to get a sense of the content/structure of the data.
- Size:
```r
dim(mydf) #returns a vector with the number of rows in the first element,
```
```
#> [1] 4 5
```
```r
#and the number of columns as the second element (the **dimensions of
#the object)
nrow(mydf) #returns the number of rows
```
```
#> [1] 4
```
```r
ncol(mydf) #returns the number of columns
```
```
#> [1] 5
```
---
- Content:
```r
head(mydf) #shows the first 6 rows
```
```
#> ID Color Passed Weight Height
#> 1 1 red TRUE 99 1.78
#> 2 2 white TRUE 54 1.67
#> 3 3 red TRUE 85 1.82
#> 4 4 <NA> FALSE 70 1.59
```
```r
tail(mydf) #shows the last 6 rows
```
```
#> ID Color Passed Weight Height
#> 1 1 red TRUE 99 1.78
#> 2 2 white TRUE 54 1.67
#> 3 3 red TRUE 85 1.82
#> 4 4 <NA> FALSE 70 1.59
```
---
- Names:
```r
colnames(mydf) #returns the column names
```
```
#> [1] "ID" "Color" "Passed" "Weight" "Height"
```
```r
rownames(mydf) #returns the row names
```
```
#> [1] "1" "2" "3" "4"
```
---
- Summary:
```r
str(mydf) #structure of the object and information about the class, length and
```
```
#> 'data.frame': 4 obs. of 5 variables:
#> $ ID : int 1 2 3 4
#> $ Color : chr "red" "white" "red" NA
#> $ Passed: logi TRUE TRUE TRUE FALSE
#> $ Weight: num 99 54 85 70
#> $ Height: num 1.78 1.67 1.82 1.59
```
```r
# content of each column
summary(mydf) #summary statistics for each column
```
```
#> ID Color Passed Weight
#> Min. :1.00 Length:4 Mode :logical Min. :54.0
#> 1st Qu.:1.75 Class :character FALSE:1 1st Qu.:66.0
#> Median :2.50 Mode :character TRUE :3 Median :77.5
#> Mean :2.50 Mean :77.0
#> 3rd Qu.:3.25 3rd Qu.:88.5
#> Max. :4.00 Max. :99.0
#> Height
#> Min. :1.590
#> 1st Qu.:1.650
#> Median :1.725
#> Mean :1.715
#> 3rd Qu.:1.790
#> Max. :1.820
```
- Note: most of these functions are "generic", they can be used on other types of
objects besides `data.frame`.
---
# Some other used functions
|name |description |
|:------------|:--------------------------------------------------------------|
|`glimpse()` |data is shown in a transposed way with columns as rows |
|`distinct()` |shows all the distinct values for a character or factor column |
|`count()` |shows a count of all the different distinct values in a column |
---