Variables and Data Types in R Programming
Hello guys, after a short introduction of R programming in my previous post, today I will be discussing about the variables and data types in R. In programming, variable is a named piece in a memory that can store value of different data types. In R variables are case sensitive and it can consist of letters, numbers, periods, and underscores or combination of these. A simple program demonstrating the use of variable in R is:
var1 <- 5
var1
var2 <- "Hello Hive!"
var2
This program will give the following output.
In R unlike other programming languages like Java and C, you don't need to declare the variable. The value is assigned the moment you run the code. There are some naming conventions that you need to keep in mind if you want your program to be more readable. If you are using two words to represent a variable, it's good to do it by using underscore() between them (for example: first_name). It's a good practice to always start the variable name with lowercase letter. And it is also the best practice to avoid using the name of function or reserved words like TRUE, FALSE in order to avoid any confusion. Its a bad practice to start variable name with dot(.) or underscore().
Now we will briefly go over the data types used in R which are: numeric, integer, logical, character and complex data types. A simple example showcasing each data-type is shown in the following code:
# Numeric
x <- 56
typeof(x)
# Integer
y <- 56L
typeof(y)
# Logical
bool_1 <- FALSE
typeof(bool_1)
# Character
z <- "Hello World"
typeof(z)
# Complex
a <- 5 + 6i
typeof(a)
This code will give output as following:
Leave Variables and Data Types in R Programming to:
Read more #code posts
Best Posts From Leo Umesh
We have not curated any of leoumesh's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From Leo Umesh
- The Tattoo of My Newborn Baby Girl Forever Inked on My Skin
- Long Weekend Food Feast With Wife
- String Manipulation in C#: Part 2
- String Manipulation in C#
- Introduction to C# and .NET
- Weekend Escape To Falls
- Evening Barbecue With Favourite Beer 🍺 🍻
- NumPy Broadcasting
- Summer BBQ
- Review of "The Day of The Jackal" Series
- Wonders of Wave Rock, Western Australia
- Trying To Make Authentic Chicken Dum Biryani At Home
- Beautiful Ocean Lookout in South West Australia
- Esperance: Hidden Paradise of Western Australia
- Cruising Through Blue Haven Beach
- My First Tattoo
- Exploring Flavoursome Veggie Indian Cuisines
- Australia's Popular Drink: Homemade
- An Everlasting Flowers
- Unexpected Rainbow At Wellington Dam, Western Australia