What does ‘as’ mean in R? (These are the droids you are looking for)

Pity the poor stormtroopers. So many droids, so little time. We’ve got the same challenge in data science – so many formats, so little time. And R code tends to get cranky when you feed it the wrong data types. Enter the “as” function – a quick way to convert the data object you have into the data object you need…. (but perhaps, not the data object we deserve?)

There are many times when you’re working on a project and the format of the data you have available doesn’t match the statistical procedure you want to use. Or the data you have doesn’t match the implementation of a specific R object. The “as” function makes it easy to navigate these little bumps. Put simple, the as function allows you to forcibly cast an object into the most similar form of the desired data. If you have a numeric variable one, it can be recast into the string variable “1”. Matrices can be forcibly converted into data frames (or the reverse). It’s a quick way to get past R’s data type errors.

Function Location

The “as” function can be accessed as part of Base R and doesn’t require you to load a specific R package.

Switching Variables

There are a multitude of reasons that people would want to switch how variables are viewed. Let us get into little nerdy ways this can be used. Say someone is wanting to make a database dealing with their favorite droids in Star Wars. Someone could want to understand how may C3 type droids are involved from the Old Republic to the comics dealing with Darth Krayt. This will have a numeric that is being used as names, so the database will have to understand the switching of years before Yavin and after while still treating the designations as names. This could even mean doing a manipulation on the date function.

A simple example of this is:

as(x, numeric, strict=TRUE)

Manipulating Lists

There are different ways that lists of data can be viewed, and the same data can be switched between an object, a data frame, or a matrix. This requires the different “as” commands to get the right expression within the R code to manipulate the data correctly. While a data frame is a table of data, after it is created it could be necessary to change it to a matrix to use for many uses for manipulations to get the answers needed. Determine how the data is needed and cater the coding to the right category.

This will look like this:

as.data.frame(x, row.names=NULL, optional=FALSE)

Making Functions

Sometimes functions may need to be made from a character object, which is another type of “as” capability. The programmer will find a list that they want to use within a specific environment, which may need to be put into a mode such as “call”. Programmers will need to make sure that the manipulation that is done will meet the needs of a viable function. Translation can cause issues, so understanding how to switch may take practice. The R programming language has several variables to work with to make sure that there are no hits when debugging code.

An example of how this should look is this:

as.function(x, ..)

This is an extremely powerful capability (similar to Python’s lambda command or Lisp Macros) and should be used very cautiously. Errors can be notoriously hard to trace, since this adds more moving parts to the system (the code as well as the data is changing during program execution). However, this unlocks many options via functional programming.

Arguments Involved

To be able to properly formulate the “as” function, the programmer needs to get the arguments correctly. The strict argument is one that the programmer must determine to be true or false. True means that the switched object, variable, or other coding follows a strict construction, while false ignores this. Value is the argument that determines what will be used to coerce the manipulation the way it needs to be. Where tells the function where to place the new creation when done with completion. Ext will provide the extensions that are needed, especially if working with non-public classes.

Each argument that is available and the way they are coded will give the way that each need to translate each part that needs to be changed. A categorical variable needs to be looked at before to determine how it will be switched within the context of these arguments. A character vector needs to be understood to then make sure that it will not cause headaches when the program is run. The arguments that are available are strong enough to coerce what needs to be changed. This will allow for more efficient data manipulations for databases and programs that data scientists need. Using the R as can be powerful.

Programming Considerations

All of these different abilities of “as” for R programming can at first cause someone to be a little hesitant at first without practice. This means that when finding that information needs to be changed in the context of what is being studied, a person should work to understand what they are needing to make it happen efficiently. The “as” function will be of great use to those making use of the same types of data that may have a smaller set of variables to work with. Practice means that people will have to plan that this will not be a fast process. Do not think that the R as will be a quick fix.

Scroll to top