

If you have a named vector, it’s also possible to use the name for selecting an element: friend_ages Bernard My_friends "Thierry" "Jerome" # Select my friends number 1 to 3 My_friends "Thierry" # Select my friends number 2 and 4

It’s possible to use the function is.na() to check whether a data contains missing value. Have_child Nicolas Thierry Bernard Jerome In R missing values (or missing information) are represented by NA: have_child <- c(Nicolas = "yes", Thierry = "yes", But this information is not available (NA) for the remaining friends (Bernard and Jerome). I know that some of my friends (Nicolas and Thierry) have 2 child. Find the length of a vector (i.e., the number of elements in a vector).For example, you cannot have a vector that contains both characters and numeric values. Note that a vector can only hold elements of the same type. Names(friend_ages) <- c("Nicolas", "Thierry", "Bernard", "Jerome")įriend_ages Nicolas Thierry Bernard JeromeĢ7 25 29 26 # You can also create a named vector as followįriend_ages <- c(Nicolas = 27, Thierry = 25, # Vector without element namesįriend_ages 27 25 29 26 # Vector with element names It’s possible to give a name to the elements of a vector using the function names().
#BASIC R STUDIO COMMANDS HOW TO#
R doesn’t know how to convert a numeric variable to a character variable.Ī vector is created using the function c() (for concatenate), as follow: # Store your friends'age in a numeric vectorĪre_married <- c(TRUE, FALSE, TRUE, TRUE) Note that, the conversion of a character to a numeric will output NA (for not available).

my_age 28 # Convert my_age to a character variable If you want to change the type of a variable to another one, use the as.* functions, including: as.numeric(), as.character(), as.logical(), etc. For instance: is.numeric(my_age) TRUE is.numeric(my_name) FALSE You can also use the functions is.numeric(), is.character(), is.logical() to check whether a variable is numeric, character or logical, respectively. It’s possible to use the function class() to see what type a variable is: class(my_age) "numeric" class(my_name) "character" "My friend's name is \"Jerome\"" "My friend's name is \"Jerome\"" 'My friend\'s name is "Jerome"' "My friend's name is \"Jerome\"" # or use this If your text contains quotes, you should escape them using”\" as follow. Note that, character vector can be created using double (“) or single (’) quotes. Ls() "area" "info" "lemon_price" "PACKAGES" "R_VERSION"īasic data types are numeric, character and logical. To remove a variable, use the function rm(): # Remove height and width variable If you work on a big project, it’s good to clean up your workspace. Note that, each variable takes some place in the computer memory. The collection of objects currently stored is called the workspace. The function ls() can be used to see the list of objects we have created: ls() "area" "height" "info" "lemon_price" "PACKAGES" "R_VERSION" These two variables will be used to compute of the rectangle.
#BASIC R STUDIO COMMANDS CODE#
The following R code creates two variables holding the width and the height of a rectangle. You can change the value of the object: # Change the value It’s possible to make some operations with it. R saves the object lemon_price (also known as a variable) in memory. Or use the function print(): print(lemon_price) 2 To print the value of the created object, just type its name: lemon_price 2 This means that lemon_price is different from Lemon_Price. Note that, it’s possible to use <- or = for variable assignments. For example, the R code below will store the price of a lemon in a variable, say “lemon_price”: # Price of a lemon = 2 euros
