-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathbasic.r
More file actions
81 lines (68 loc) · 1.34 KB
/
Copy pathbasic.r
File metadata and controls
81 lines (68 loc) · 1.34 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
# single line comment
'
multiline comment
'
# types
10.5 55 # numeric
1L 55L # integer
4i 9+3i # complex
'foo' # character (string)
TRUE FALSE # logical (boolean)
class(TRUE) # check type
# operator
+ - * / # arithmetic
^ # exponent
%% # remainder
%/% # integer division
<- -> <<- ->> # assignment
== != > < >= <= # comparison
! && || & | # logical
: # sequence generator
%in% # does elem belong to vector
%*% # matrix multiplication
# structs
c(1,2,3,4) # vector
c('a','b','c') # ...
list('a','b','c') # list
matrix(c(1,2,3,4),2,2) # matrix
array(1:10) # array
array(c('a','b')) # ...
array(1:10,c(5,5)) # ... 2d
# some arr ops
array(c('green','yellow'),c(2,4))
foo <- list(1,2,3)
foo <- append(foo,4)
foo <- list(1,2,3)
foo <- foo[-1]
seq(1,4)
seq(-4,4,length=100)
# var
a <- 'foo'
a <- 2
a <- 3
3 -> a
g <<- 3 # global assignment
3 ->> g
# cond
if () {}
if () {} else if () {}
if () {} else if () {} else {}
# loop
for (i in 1:20) {
if (i %% 2 == 0) { next }
if (i == 17) { break }
print(i)
}
i <- 0
while (i < 20) {
i <- i + 1
print(i)
if (i %% 2 == 0) { next }
if (i == 17) { break }
}
# fn
myfn <- function(a, b=25, name='john') {
num <- a * b
res <- paste(c(name, 'is', num, 'years old'), collapse=' ')
return(res)
}