-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVariable_expressions_statements-R.qmd
More file actions
120 lines (85 loc) · 2.67 KB
/
Variable_expressions_statements-R.qmd
File metadata and controls
120 lines (85 loc) · 2.67 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
---
title: "R: Variables, expression and statements"
format:
html:
toc: true
self-contained: true
editor_options:
chunk_output_type: console
---
## Data Types
Some of the commonly used objects in R are numbers - integer and double (or numeric), character and logical (TRUE/FALSE). The data type of the object can be identified using the in-built R function `class()` or `typeof()`. For example, see the following objects and their types:
```{r}
class(4)
```
```{r}
typeof(4)
```
```{r}
class(4.4)
```
```{r}
typeof(4.4)
```
```{r}
class(4L)
```
```{r}
typeof(4L)
```
```{r}
class('4')
```
```{r}
typeof('4')
```
```{r}
class(TRUE)
```
```{r}
typeof(FALSE)
```
## Variable names
We have the following rules for a R variable name:
- A variable name must start with a letter and can be a combination of letters, digits, period(.) and underscore(_). If it starts with period(.), it cannot be followed by a digit.
- A variable name cannot start with a number or underscore (_)
- Variable names are case-sensitive (age, Age and AGE are three different variables)
- Reserved words cannot be used as variables (TRUE, FALSE, NULL, if...)
## Converting datatypes
Sometimes a value may have a datatype that is not suitable for using it. For example, consider the variable called `annual_income` in the code below:
```{r}
annual_income = "80000"
```
Suppose we wish to divide `annual_income` by 12 to get the monthly income. We cannot use the variable `annual_income` directly as its datatype is a string and not a number. Thus, numerical operations cannot be performed on the variable `annual_income`.
We’ll need to convert annual_income to an integer. For that we will use the R’s in-built as.integer() function:
```{r}
annual_income = as.integer(annual_income)
monthly_income = annual_income/12
print(paste0("monthly income = ", monthly_income))
```
Similarly, datatypes can be converted from one type to another using in-built R functions as shown below:
```{r}
#Converting integer to character
as.character(9)
```
```{r}
#Converting character to numeric
as.numeric('9.4')
```
```{r}
#Converting logical to integer
as.numeric(FALSE)
```
Sometimes, conversion of a value may not be possible. For example, it is not possible to convert the variable greeting defined below to a number:
```{r}
greeting = "hello"
```
However, strings can be concatenated using the `paste0()` function:
```{r}
paste0("hello", " there!")
```
R’s in-built readline() function can be used to accept an input from the user. For example, suppose we wish the user to input their age:
```{r}
age = readline("Enter your age:")
```
The entered value is stored in the variable `age` and can be used for computation.