4 Object assignment and manipulation

The concept of object (or variable) assignment is a fundamental concept when working in a scripting environment; indeed, the ability to easily assign values to objects is what allows us to easily and intuitively manipulate and process our data in a programmatic setting. To better understand the mechanics of object assignment, consider the following:

# assign value 5 to new object named x
x<-5

In the code above, we use R’s assignment operator, <-, to assign the value 5 to an object named x. Now that an object named x has been created and assigned the value 5, printing x in our console (or printing x in our script and running it) will return the value that has been assigned to the x object, i.e. 5:

# prints value assigned to "x"
x
## [1] 5

More generally, the process of assignment effectively equates the output created by the code on the right side of the assignment operator (<-) to an object with a name that is specified on the left side of the assignment operator. Whenever we want to look at the contents of an object (i.e. the output created by the code to the right side of the assignment operator), we simply print the name of the object in the R console (or print the name and run it within a script).

Let’s create another object, named y, and assign it the value “12”:

# assign value 12 to new object named y
y<-12

As we noted above, we can print the value that was assigned to y by printing its name:

# prints value assigned to "y"
y
## [1] 12

It’s possible to use existing objects to assign values to new ones. For example, we can assign the sum of x and y to a new object that we’ll name xy_sum:

# creates a new object, named "xy_sum" whose value is the sum of "x" and "y"
xy_sum<-x+y

Now, let’s print the contents of xy_sum

# prints contents of "xy_sum"
xy_sum
## [1] 17

As expected, we see that the value assigned to xy_sum is “17” (i.e. the sum of the values assigned to x and y).

It is possible to change the value assigned to a given object. For example, let’s say we want to change the value assigned to x from “5” to “8”:

# assign value of "8" to object named "x"
x<-8

We can now confirm that x is now associated with the value “8”

# prints updated value of "x"
x
## [1] 8

It’s worth noting that updating the value assigned to x will not automatically update the value assigned to xy_sum (which, recall, is the sum of x and y). If we print the value assigned to xy_sum, note that it is still “17”):

xy_sum
## [1] 17

In order for the value assigned to xy_sum to be updated with the new value of x, we must run the assignment operation again:

# assigns sum of "y" and newly updated value of "x" to "xy_sum" object
xy_sum<-x+y

Now, the value of xy_sum should reflect the updated value of x, which we can confirm by printing the value of xy_sum:

# prints value of "xy_sum"
xy_sum
## [1] 20

Note that the value assigned to xy_sum is now “20” (the sum of “8” and “12”), rather than “17” (the sum of “5” and “12”).

While the examples above were very simple, we can assign virtually any R code, and by extension, the data structure(s) generated by that code (such as datasets, vectors, graphs/plots etc.) to an R object. When naming your objects, try to be descriptive, so that the name of the object signifies something about its corresponding value.

Below, consider a simple example of an object, named our_location that has been assigned a non-numeric value. It’s value is a string, or textual information:

# assigns text string "Boulder, CO" to 
our_location<-"Boulder, CO"

We can print string that has been assigned to the location object by typing the name of the object in our console, or running it from our script:

# prints value of "our_location" object
our_location
## [1] "Boulder, CO"

Note that generally speaking, you have a lot of flexibility in naming your R objects, but there are certain rules. For example, object names must start with a letter, and cannot contain any special symbols (they can only contain letters, numbers, underscores, and periods). Also, object names cannot contain multiple unconnected words; if you’d like to use multiple words or phrases, connect the discrete elements with an underscore (_), or use camel case (where different words are distinguished by beginning each discrete word begins with a capitalized letter).

It is also worth emphasizing that object names are case sensitive; in order to print the value assigned to an object, that object’s name must be printed exactly as it was created. For example, if we were to type our_Location, we would get an error, since there is no our_Location object (only an our_location object):

our_Location
## Error in eval(expr, envir, enclos): object 'our_Location' not found

In order to keep track of the objects we have created, we can use the handy ls() function, which will print the names of all the objects that are in memory:

# prints objects in memory
ls()
##  [1] "africa_summary"                     "arbitrary_values"                  
##  [3] "arbitrary_values_2x"                "asia_europe_africa"                
##  [5] "celsius_outputs_df"                 "celsius_outputs_list"              
##  [7] "celsius_outputs_vector"             "cgexp_africa"                      
##  [9] "cgexp_africa_ascending"             "cgexp_africa_ascending_inverted"   
## [11] "cgexp_africa_descending"            "cor_matrix"                        
## [13] "country_df"                         "countryA_fahrenheit"               
## [15] "countryB_fahrenheit"                "countryC_fahrenheit"               
## [17] "countryD_fahrenheit"                "crosstab_federal_continent"        
## [19] "desired_variables"                  "df"                                
## [21] "example_list"                       "fahrenheit_input_vector"           
## [23] "fahrenheit_to_celsius_converter"    "fahrenheit_to_celsius_converter_df"
## [25] "fdi"                                "filenames"                         
## [27] "high_revenues"                      "marginal_effect_federalism"        
## [29] "minority_catholic"                  "model_list"                        
## [31] "months_four"                        "name_vector"                       
## [33] "oecd_countries"                     "oecd_federal_countries"            
## [35] "our_location"                       "processed_temperature_data_list"   
## [37] "pt"                                 "pt_africa"                         
## [39] "pt_cloud"                           "pt_copy"                           
## [41] "pt_copy_sans_africa"                "pt_copy_selection"                 
## [43] "pt_copy_selection_modified"         "pt_copy_summarystats1"             
## [45] "regression1"                        "regression2"                       
## [47] "regression2_alt"                    "scatter_cgexp_trade"               
## [49] "scatter_cgexp_trade_facets"         "scatter_cgexp_trade_grouped"       
## [51] "scatter_cgexp_trade_line"           "summary_stats_by_continent"        
## [53] "temperature_input_list"             "test"                              
## [55] "trade_age_by_continent"             "trade_federal_interaction"         
## [57] "wb"                                 "WB_debt"                           
## [59] "WB_fdi"                             "wb_file_list"                      
## [61] "WB_filenames_export"                "wb_files"                          
## [63] "wb_modified"                        "world_bank_list"                   
## [65] "world_bank_list_cleaned"            "worldbank_cleaning_function"       
## [67] "worldbank_filenames"                "worldbank_filenames_base"          
## [69] "x"                                  "xy_sum"                            
## [71] "y"