<jupyter_start><jupyter_text>Variables                             Variable Name     A `variable` provides us with `named storage` that our programs can manipulate. A variable in R can store an atomic vector, group of atomic vectors or a combination of many Robjects.  1. A valid variable name consists of `letters`, `number`s and the `dot` or `underline` characters.2. The variable name starts with a `letter` or the `dot not followed by a number`.                     Example:<jupyter_code># valid: Has letters, numbers, dot and underscore
var_name.2 = 1
print(var_name.2)
# Invalid: Has the character '%'. Only dot(.) and underscore allowed
var_name% = 1
print(var_name%)
# Invalid: Starts with a number
2var_name = 1
print(2var_name)
# Valid: Can start with a dot(.) but the dot(.)should not be followed by a number
.var_name = 1
print(.var_name)

var.name = 2
print(var.name)
# Invalid: The starting dot is followed by a number making it invalid
.2var_name = 1
print(.2var_name)
# Invalid: Starts with _ which is not valid
_var_name = 1
print(_var_name)
# Invalid: R is case-sensitive
Name = 1
print(name)<jupyter_output><empty_output><jupyter_text>Variable Assignment     The `variables` can be `assigned values` using `leftward`, `rightward` and `equal` to operator. The values of the variables can be printed using `print()` or `cat()` function. **The cat() function combines multiple items into a continuous print output**. ---                    Assignment Using Equal Operator:<jupyter_code># Assignment using equal operator.
var_1 = c(0, 1, 2, 3)

print(var_1)

cat("var_1 is ", var_1 ,"\n")<jupyter_output><empty_output><jupyter_text>---                    Assignment Using Leftward Operator:<jupyter_code># Assignment using leftward operator.
var_2  <- c(TRUE, 1)       

print(var_2)

cat ("var_2 is ", var_2 ,"\n")<jupyter_output><empty_output><jupyter_text>---                    Assignment Using Rightward Operator:<jupyter_code># Assignment using rightward operator.   
c(TRUE, 1, "a") -> var_3           

print(var_3)

cat ("var_3 is ", var_3 ,"\n")<jupyter_output><empty_output><jupyter_text>>Note − The vector `c(TRUE, 1)` and `c(TRUE, 1, "a")` has a mix of **logical** and **numeric** and **character** class.                 Data Type of a Variable     In R, a variable itself is not declared of any data type, rather it `gets the data type` of the `R-object assigned` to it. So R is called a **dynamically typed language**, which means that we can change a variable’s data type of the same variable again and again when using it in a program.<jupyter_code>var_x <- "Hello"
cat("The class of var_x is ",class(var_x),"\n")

var_x <- 34.5
cat("  Now the class of var_x is ",class(var_x),"\n")

var_x <- 27L
cat("   Next the class of var_x becomes ",class(var_x),"\n")<jupyter_output><empty_output><jupyter_text>Finding Variables     To know all the variables currently available in the `workspace` we use the `ls() function`. Also the ls() function can use patterns to match the variable names.<jupyter_code>print(ls())<jupyter_output><empty_output><jupyter_text>>Note − It is a sample output depending on what variables are declared in your environment. The `ls() function` can use patterns to match the variable names.<jupyter_code># List the variables starting with the pattern "var".
print(ls(pattern = "var"))<jupyter_output><empty_output><jupyter_text>The variables `starting with dot(.)` are `hidden`, they can be listed using `"all.names = TRUE"` argument to `ls() function`.<jupyter_code>.x <- 2
print(.x)
print(ls())
print(ls(all.name = TRUE))<jupyter_output><empty_output><jupyter_text>Deleting Variables     Variables can be deleted by using the `rm() function`. Below we delete the variable var_1. On printing the value of the variable error is thrown.<jupyter_code>var_1 = 1
print("print var_1:")
print(var_1)
print("Remove var_1")
rm(var_1)
print("print var_1")
print(var_1)<jupyter_output><empty_output><jupyter_text>All the variables can be deleted by using the `rm()` and `ls()` function together.<jupyter_code>rm(list = ls())
print(ls())
a = 1
b = 2
c = 3

rm(b, c)

print(a)
print(b)
print(c)<jupyter_output><empty_output><jupyter_text>R Reserved Words                         Reserved words in R programming are a set of words that have special meaning and cannot be used as an identifier (variable name, function name etc).Here is a list of reserved words in the R’s parser.                                        This list can be viewed by typing help(reserved) or ?reserved at the R command prompt as follows.<jupyter_code>?reserved<jupyter_output><empty_output><jupyter_text>Among these words, if, else, repeat, while, function, for, in, next and break are used for conditions, loops and user defined functions. They form the basic building blocks of programming in R.                    TRUE and FALSE are the logical constants in R.NULL represents the absence of a value or an undefined value.Inf is for “Infinity”, for example when 1 is divided by 0 whereas NaN is for “Not a Number”, for example when 0 is divided by 0.NA stands for “Not Available” and is used to represent missing values.                    R is a case sensitive language. Which mean that TRUE and True are not the same. While the first one is a reserved word denoting a logical constant in R, the latter can be used a variable name.<jupyter_code>TRUE <- 1
True <- 1
print(True)<jupyter_output>[1] 1<jupyter_text>Constants in R                         Constants, as the name suggests, are entities whose value cannot be altered. Basic types of constant are numeric constants and character constants.                         1. Numeric Constants                                         All numbers fall under this category. They can be of type integer, double or complex.It can be checked with the typeof() function.Numeric constants followed by L are regarded as integer and those followed by i are regarded as complex.<jupyter_code>class(5)
typeof(5)
typeof(5L)
typeof(5i)<jupyter_output><empty_output><jupyter_text>2. Character Constants                                         Character constants can be represented using either single quotes (') or double quotes (") as delimiters.<jupyter_code>name <- "pooya"
typeof(name)
class("5")
typeof("5")<jupyter_output><empty_output><jupyter_text>3. Built-in Constants                                         Some of the built-in constants defined in R along with their values is shown below.<jupyter_code>LETTERS
letters
pi
month.name
month.abb<jupyter_output><empty_output><jupyter_text>But it is not good to rely on these, as they are implemented as variables whose values can be changed.<jupyter_code>rm(pi)

cat("'pi' Before Assignment:", pi, "\n")

pi <- 5

cat("'pi' After Assignment:", pi, "\n")

rm(pi)

cat("'pi' After Assignment:", pi, "\n")<jupyter_output>Warning message in rm(pi):
"object 'pi' not found"
{'fraction_non_alphanumeric': 0.09702251270878721, 'fraction_numerical': 0.01045751633986928, 'mean_word_length': 3.8288920056100983, 'pattern_counts': {'":': 0, '<': 72, '<?xml version=': 0, '>': 66, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '143962', 'license': 'MIT', 'n_tokens_mistral': 2095, 'n_tokens_neox': 2003, 'n_words': 834, 'path': 'Week 01/R_04_Variables.ipynb', 'repo_name': 'shirazipooya/RCourse', 'size': '6885'}