-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLanguage Basics.R
More file actions
109 lines (85 loc) · 1.48 KB
/
Copy pathLanguage Basics.R
File metadata and controls
109 lines (85 loc) · 1.48 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
# R Language Basics
# Assignment operators
x <- "Hello World!"
y = "Hello World!"
#Implicit Printing
print(x)
x
# Creating variable
l <- TRUE
i <- 123L
n <- 1.23
c <- "ABC 123"
d <- as.Date("2001-02-03")
# Displaying Variables
l
i
n
c
d
# Creating a function
f <- function(x) { x + 1 }
# Invoking a function
f(2)
# Creating a vector
v <- c(1, 2, 3);
v
# Creating a sequence
s <- 1:5
s
# Creating a matrix
m <- matrix(
data = 1:6,
nrow = 2,
ncol = 3)
m
# Creating an array
a <- array(
data = 1:8,
dim = c(2,2,2))
a
# Creating a list
l <- list(TRUE, 123L, 2.34, "abc")
l
# Creating a factor
categories <- c("Male", "Female", "Male", "Male", "Female")
factor <- factor(categories)
factor
levels(factor)
unclass(factor)
# Creating a data frame
df <- data.frame(
Name = c("Cat", "Dog", "Cow", "Pig"),
HowMany = c(5, 10, 15, 20),
IsPet = c(TRUE, TRUE, FALSE, FALSE)
)
df
# Indexing data frames by row and column
df[1,2]
# Indexing data frames by row
df[1,]
# Indexing data frames by column
df[,2]
df[["HowMany"]]
df$HowMany
# Subsetting data frames
df[c(2, 4), ]
df[2:4, ]
df[c(TRUE, FALSE, TRUE, FALSE),]
df[df$IsPet == TRUE, ]
df[df$HowMany > 10, ]
df[df$Name %in% c("Cat", "Cow"), ]
# R is a vectorized language
1 + 2
c(1,2,3) + c(2,4,6)
# Named vs. ordered arguments
m <- matrix(data = 1:6, nrow = 2, ncol =3)
n <- matrix(1:6, 2, 3)
m == n
identical(m, n)
# Installing pacakges
install.packages("dplyr")
# Loading packages
library("dplyr")
# Viewing help
?data.frame