-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasic datatypes in R.Rmd
134 lines (104 loc) · 2.32 KB
/
Basic datatypes in R.Rmd
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
---
title: "Basic Datatypes in R"
date: "05/08/2021"
output: html_document
---
#### Name : Sara Kulkarni
#### Reg. no : 19BCE1567
1. Assign a numeric value to a variable ‘a’ and convert ‘a’ into an integer.
```{r}
a <- 15
as.integer(a)
```
2. Make this quote into an R string – “Do you think this is a game?”, he said. “No, I think Jenga’s a game”, Archer responded.
```{r}
quote <- "Do you think this is a game?”, he said. “No, I think Jenga’s a game”, Archer responded."
print(quote)
```
3. Create two numeric vectors p & q
```{r}
p <- c(0.2,0.4)
p
q <- c(1.2,1.4)
q
```
4. Use R as a calculator to do basic arithmetic operations on p &q and store each result in different variables.
```{r}
a=p+q
a
```
```{r}
b=p-q
b
```
```{r}
c=p*q
c
```
```{r}
d=p/q
d
```
5. Calculate sqrt(a) and perform log2 transformation on the result.
```{r}
a <- 49
sqrt(a)
```
```{r}
result=log(a,2)
result
```
6. Calculate the 10-based logarithm of 100 and multiply the result with cosine of π.
```{r}
l=log(100,10)
l
```
```{r}
res=l*-1 #cos(pi)=-1
res
```
7. Create a vector ‘x’ using : operator from -2 to 2.
```{r}
x <- -2:2
x
```
8. Print the dimension and length of x.
```{r}
dim(x)
```
```{r}
length(x)
```
9. Create two vectors small and caps with last 5 alphabets in lower case and first 5 alphabets in upper case respectively. Bind it row and columnwise.
```{r}
small <- c('A','B','C','D','E','f','g','h','i','j')
caps <- c('P','Q','R','S','T','u','v','w','x','y')
rbind(small,caps)
```
```{r}
cbind(small,caps)
```
10. Create matrix M=(1,2,5;-4,8,6;3,-1,7) and find matrix MxMxM.
```{r}
M <- matrix(c(1,2,5,-4,8,6,3,-1,7),nrow=3,ncol=3)
resm=M*M*M #does atrix multiplication
resm
```
11. Find the result of elementwise multiplication of M by M.
```{r}
element=M*M
element
```
12. Find the transpose, inverse and determinant of M.
```{r}
t(M) #transpose of matrix
```
```{r}
solve(M) #inverse of matrix
```
```{r}
det(M) #determinant of a matrix
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document.