-
Notifications
You must be signed in to change notification settings - Fork 4
/
R Features.R
100 lines (67 loc) · 1.12 KB
/
R Features.R
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
# Built-In R Features
## Seq
seq(0,100,by=10)
seq(0,10,by=2)
help("seq")
## Sort
v <- c(1,4,7,2,13,3,11)
v
sort(v)
sort(v,decreasing=TRUE)
cv <- c('b','d','a')
cv
sort(cv)
cv <- c('b','d','a','c')
cv
sort(cv)
## Rev
v<- 1:10
v
rev(v)
str(v)
str(mtcars)
summary(mtcars)
## Append
v1 <- 1:10
v2 <- 35:40
v
append(v,v2)
## Checking Data Type
v <- c(1,2,3)
is.vector(v)
is.data.frame(v)
is.data.frame(mtcars)
## as.
v
as.list(v)
as.matrix(v) # Converting
### Apply
sample(x = 1:100, 3)
1:10
v <- c(1,2,3,4,5)
addrand <- function(x){
ran <- sample(1:100,1)
return(x+ran)
}
print(addrand(10))
result <- lapply(v,addrand)
print(result)
v <- 1:5
times2 <- function(num){
return(num*2)
}
print(v)
result <- sapply(v,times2)
print(result)
### Anonymous Functions
v <- 1:5
times2 <- function(num){num*2}
result <- sapply(v,function(num){num*2})
print(result)
### Apply with Multiple Inputs
v <- 1:5
add_choice <- function(num,choice){
return(num+choice)
}
print(add_choice(2,10))
print(sapply(v,add_choice,choice=100))