Tech Junkie Blog - Real World Tutorials, Happy Coding!: R Programming: Variables

Thursday, May 17, 2018

R Programming: Variables

R is a programming language that is a little different in syntax then your typical mainstream languages like Java or C#. In this post we will go over how to create variables in R.

Let's create some variables about a user:

To create a variable in we use the <- key for example if you want to assign a value to variable firstName, you would write firstName <-  "John" or "John" -> firstName












If you type in the firstName in the console you will "John" as the value









To get the type of the variable you can use the class(firstName)











As you can see the variable firstName has the type character

Now let say you created a new variable call zipCode and you want to convert to a string.  You can do that with the "as" method first lets create the zipCode variable with the statement zipCode <- 90210





Now let's see what the type of zipCode is by using the class method class(zipCode)









Let's convert the zipCode variable to the character type by using the "as" function like the following

zipCode <- as.character(zipCode)



5 comments:

  1. It really takes me back to my own early days of learning new languages. Speaking of daily habits and challenges, I've actually been hooked on playing Heardle lately. It's such a fun way to test your song recognition skills, especially starting from just that first little snippet – brings back so many musical memories! It’s funny how different types of "variables" exist everywhere, whether they're in code or the mental catalog of songs in my head.

    ReplyDelete