Tuesday, February 16, 2016

Basic Data Types in R

As part of tutorial series on Data Science with R from Data Perspective, this first tutorial introduces the very basics of R programming language about basic data types in R.

What we learn:
After the end of the chapter, you are provided with R console so that you can practice what you have learnt in this chapter.







R assignment operator
x = 'welcome to R programming' # assigning string literal to variable x 
x
[1] "welcome to R programming"
typeof(x) #to check the data type of the variable x
[1] "character"
Numeric Numeric data represents decimal data.
x = 1.5 #assigning decimal value1.5 to x
x
[1] 1.5 
To check the data type we use class() function:
class(x)
[1] "numeric"
To check if the variable “x” is of numerical or not, we use
is.numeric(x)
[1] TRUE
To convert any compatible data into numeric, we use:
 x = '1' #assigning value 1 to variable x
 class(x)
[1] "character"
 x = as.numeric(x)
[1] 1
 class(x)
[1] "numeric"

Note: if we try to convert a string literal to numeric data type we get the following result.

x= 'welcome to R programming'
as.numeric(x)
[1] NA
Warning message:
NAs introduced by coercion
Integer We use as.integer() function to convert into integers. This converts numeric value to integer values.
x = 1.34
 [1] 1.34
class(x)
[1] "numeric"
y = as.integer(x)
class(y)
[1] "integer"
y
[1] 1
Note: to check if the value is integer or not we use is.integer() function.
In the below example ‘y’ is numerical or decimal value whereas x is integer.
 is.integer(y)
[1] TRUE
 is.integer(x)
[1] FALSE
Complex: Complex data types are shown as below, though we use it very less in our day to day data analysis:
c = 3.5+4i
[1] 3.5+4i
is.complex(c)
[1] TRUE
class(c)
[1] "complex"
Logical Logical data type is one of the frequently used data type usually used for comparing two values. Values a logical data type takes is TRUE or FALSE.
logical = T
logical
[1] TRUE
l = FALSE
l
[1] FALSE
Character String literals or string values are stored as Character objects in R.
str = "R Programming"
str
[1] "R Programming"
class(str)
[1] "character"
is.character(str)
[1] TRUE
We can convert other data types to character data type using as.character() function.
x = as.character(1)
x
[1] "1"
class(x)
[1] "character"
Note: There are a variety of operations that can be applied on characters such as substrings, finding lengths; etc will be dealt as when appropriate.
So far we learnt about the basic data types in R, let’s get into a bit complex data types.
Vector How do we hold collection of same data types? We come across this requirement very frequently. We have vector data type to solve this problem.
Consider a numerical vector below:
num_vec = c(1,2,3,4,5)
num_vec
[1] 1 2 3 4 5
class(num_vec)
[1] "numeric"
We can apply many operations on the vector variables such as length, accessing values or members of the vector variable.
Length of the vector can be found using length() function.
length(num_vec)
[1] 5
We access each element or member of the vector num_vec using its indexes starting from
In the below example we can access the members at 1st,2nd,3rd positons.
num_vec[1]
[1] 1
num_vec[2]
[1] 2
num_vec[3]
[1] 3
Similarly string vectors, logical vectors, integer vectors can be created.
char_vec = c("A", "Course","On","Data science","R rprogramming")
char_vec
[1] "A"  "Course" "On" "Data science" "R rprogramming"
length(char_vec)
[1] 5
char_vec[1]
[1] "A"
char_vec[2]
[1] "Course"
char_vec[4]
[1] "Data science"
Matrix Matrix data type is used when we want to represent the data as collection of numerical values in mXn, m by n, dimensions. Matrices are used mostly when dealing with mathematical equations, machine learning, text mining algorithms.
Now how do we create a matrix?
m = matrix(c(1,2,3,6,7,8),nrow = 2,ncol = 3)
m
     [,1] [,2] [,3]
[1,]    1    3    7
[2,]    2C    6    8
class(m)
[1] "matrix"
Knowing the dimension of the matrix is:
dim(m)
[1] 2 3
How do we access elements of matrix m:
#accessing individual elements are done using the indexes shown as below. In the below example we are accessing 1st, 2nd, 6th element of matrix m.
m[1]
[1] 1
m[2]
[1] 2
m[6]
[1] 8
m[2,3] #  here we accessing 2nd row 3rd column element.
[1] 8
# accessing all elements of rows of the matrix m shown below. 
m[1,]
[1] 1 3 7
m[2,]
[1] 2 6 8
#accessing all elements of each column
m[,1]
[1] 1 2
m[,2]
[1] 3 6
m[,3]
[1] 7 8 
What happens when we add different data types to a vector?
v = c("a","b",1,2,3,T)
v
[1] "a"    "b"    "1"    "2"    "3"    "TRUE"
class(v)
[1] "character"
v[6]
[1] "TRUE"
class(v[6])
[1] "character"
What happened in the above example is, R coerced all different data types into a single data type of character type to maintain the condition of single data type.
List What if we want to handle different data types in a single object?
List data type helps us in storing elements of different data types in a single object.
We create list objects using list() function.
In the below example I have created a list object “list_exp” with 6 different elements of character, numeric and logical data types.
list_exp = list("r programming","data perspective",12345,67890,TRUE,F)
list_exp
[[1]]
[1] "r programming"
[[2]]
[1] "data perspective"
[[3]]
[1] 12345
[[4]]
[1] 67890
[[5]]
[1] TRUE
[[6]]
[1] FALSE
Using str() function, we can know the structure of the list object, i.e. the internal structure of the list objects can be known. This is one of the very important functions which we use in our day to day analysis.
In the below example we can see a list of 6 elements of character, numerical and logical data types.
str(list_exp)
List of 6
 $ : chr "r programming"
 $ : chr "data perspective"
 $ : num 12345
 $ : num 67890
 $ : logi TRUE
 $ : logi FALSE
#accessing the  data type of list_exp
class(list_exp)
[1] "list"
length(list_exp)
[1] 6
list_exp[1]
[[1]]
[1] "r programming"
#accessing the list elements using indexing.
list_exp[[1]]
[1] "r programming"
list_exp[[6]]
[1] FALSE
list_exp[[7]] # when we try accessing not existing elements we get the below error.
Error in list_exp[[7]] : subscript out of bounds
# finding the class of individual list element
class(list_exp[[6]])
[1] "logical"
class(list_exp[[3]])
[1] "numeric"
class(list_exp[[1]])
[1] "character"
Data Frame: Most of us would be from a bit of SQL background and we would be very much comfortable in handling data in the form of SQL table because of the functionalities which a SQL table offers while working the data.
How would it be if we have such data type object available in R which can be used to store the data and manipulate data in very easy, efficient and convenient way?
R offers a data frame data type object. It is another way that information is stored as data frames. We can treat a data frame similar to a SQL table.
How do we create a data frame?
#creating a data frame
data_frame = data.frame(first=c(1,2,3,4),second=c("a","b","c","d"))
data_frame
  first second
     1      a
     2      b
     3      c
     4      d
#accessing  the data type of the object
class(data_frame)
[1] "data.frame"
#finding out the row count of data_frame using nrow()
nrow(data_frame)
[1] 4
#finding out the column count of data_frame using ncol()
ncol(data_frame)
[1] 2
#finding out the dimensions of data_frame using dim()
dim(data_frame)
[1] 4 2
#finding the structure of the data frame using str()
str(data_frame)
'data.frame': 4 obs. of  2 variables:
 $ first : num  1 2 3 4
 $ second: Factor w/ 4 levels "a","b","c","d": 1 2 3 4
#accessing the entire row of data frame using row index number. Observe below that if we use data_frame[1,] without specifying the column number it means that we want to access all the columns of row 1.
data_frame[1,]
  first second
1     1      a
#similarly to access only 1st column values without row information use data_frame[,1] 
data_frame[,1]
[1] 1 2 3 4
#accessing the row names of the data frame.
rownames(data_frame)
[1] "1" "2" "3" "4"
#accessing the column names of the data frame
colnames(data_frame)
[1] "first"  "second"
#column data can accessed using the column names explicitly instead of column indexes
data_frame$first
[1] 1 2 3 4
data_frame$second
[1] a b c d
Levels: a b c d
#accessing individual values using row and column indexes
data_frame[1,1] # accessing first row first column
[1] 1
data_frame[2,2] # accessing second row second column
[1] b
Levels: a b c d
data_frame[3,2]  # accessing third row second column
[1] c
Levels: a b c d
data_frame[3,1] # accessing third row first column
[1] 3
Note: Observe the below data frame:
dt_frame = data.frame(first=c(1,2,3,4,5,6,7),second=c("Big data","Python","R","NLP","machine learning","data science","data perspective"))
dt_frame
  first           second
     1         Big data
     2           Python
     3                R
     4              NLP
     5 machine learning
     6     data science
     7 data perspective
Assume we have a dataset with 1000 rows instead of 6 rows shown in above data frame. If we want to see a sample of data of the data frame, how do we do?
Using head() function.
head(dt_frame)
  first           second
     1         Big data
     2           Python
     3                R
     4              NLP
     5 machine learning
     6     data science
head() function returns us the first six rows of any data frame so that we can have a look of what the data frame is like.
Also we can use tail() function to see the last six rows of the data frame.
tail(dt_frame)
  first           second
     2           Python
     3                R
     4              NLP
     5 machine learning
     6     data science
     7 data perspective
We have View() function to see the values of a data frame in a tabular form.
View(dt_frame)


27 comments:

  1. Hey there! Do you use Twitter? I'd like to follow you if that would be okay. I'm undoubtedly enjoying your blog and look forward to new updates. best webdesign

    ReplyDelete
    Replies
    1. R has several basic data types that are fundamental to the language. Understanding these data types is essential for effective data manipulation and analysis. Here are the primary data types in R:

      1. Numeric
      Numeric data types represent numbers and can be either integers or floating-point numbers.

      Integers are whole numbers and are a specific type of numeric data. You can specify an integer by appending an L to the number.

      3. Complex
      Complex numbers have both a real and an imaginary part.

      4. Logical
      Logical values are TRUE or FALSE.

      5. Character
      Character data types represent text strings.

      R will often automatically convert (coerce) data types to accommodate operations. For example, combining numeric and character data will coerce the numeric to character.

      Type Checking and Conversion
      You can check the type of a variable using functions like class(), typeof(), and is.* functions (e.g., is.numeric(), is.character()). You can convert between types using functions like as.numeric(), as.character(), etc.

      Deep Learning Projects for Final Year

      Machine Learning Projects for Final Year

      Artificial Intelligence Projects For Final Year

      Delete
  2. You made some good points there. I looked on the internet for the subject matter and found most guys will consent with your blog. simple web design singapore

    ReplyDelete
  3. Pretty component of content. I simply stumbled upon your site and in accession capital to say that I acquire in fact enjoyed account your blog posts. Anyway I will be subscribing to your feeds and even I achievement you get right of entry to persistently fast. google pay per click singapore

    ReplyDelete
  4. Hi! I just wanted to ask if you ever have any problems with hackers? My last blog (wordpress) was hacked and I ended up losing several weeks of hard work due to no backup. Do you have any methods to protect against hackers? Letter of Recommendation

    ReplyDelete
  5. you are really a good webmaster. The website loading speed is incredible. It seems that you are doing any unique trick. In addition, The contents are masterpiece. you have done a magnificent job on this topic! office cleaning company singapore

    ReplyDelete
  6. great post, very informative. I wonder why the other experts of this sector don't notice this. You must continue your writing. I'm confident, you have a great readers' base already! Top E-commerce Website Design Trends for 2019

    ReplyDelete
  7. When I initially commented I clicked the "Notify me when new comments are added" checkbox and now each time a comment is added I get three emails with the same comment. Is there any way you can remove people from that service? Cheers! ct scan singapore

    ReplyDelete
  8. Wonderful blog! I found it while searching on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I've been trying for a while but I never seem to get there! Many thanks. best divorce lawyer Singapore

    ReplyDelete
  9. Hi there! This post could not be written any better! Reading this post reminds me of my previous room mate! He always kept chatting about this. I will forward this page to him. Pretty sure he will have a good read. Thank you for sharing! Google Home

    ReplyDelete
  10. Depending on your case, you might need a federal attorney versus a state attorney. Anyone that broke state law must look for criminal defense attorneys that specialize in that state’s law. helpingclients.com

    ReplyDelete
  11. Howdy! Do you know if they make any plugins to protect against hackers? I'm kinda paranoid about losing everything I've worked hard on. Any recommendations? drop earrings

    ReplyDelete
  12. I really like your blog.. very nice colors & theme. Did you design this website yourself or did you hire someone to do it for you? Plz respond as I'm looking to design my own blog and would like to know where u got this from. appreciate it.Customer loyalty programs

    ReplyDelete
  13. Hey! I'm at work browsing your blog from my new apple iphone! Just wanted to say I love reading through your blog and look forward to all your posts! Keep up the excellent work! solidworks price

    ReplyDelete
  14. Best New Love Quotes Everyone searches the internet and you find many websites like our website LoveQuotesG.Com

    Quotes About Being Tired

    Retirement Best Wishes Quotes

    Inspiring Quotes For College Students

    Surprise Love Quotes For Him/Her

    ReplyDelete
  15. Thank you for slowly explaining all these different topics. Need a logo for you business make sure to check the link below:
    Custom Logo Design

    ReplyDelete
  16. Wow! Thank you! I continuously wanted to write on my site something like that. Can I include a portion of your post to my blog? Buy Art Online and Art For Sale

    ReplyDelete
  17. This is very interesting, You are a very skilled blogger. I've joined your feed and look forward to seeking more of your wonderful post. Also, I have shared your website in my social networks! safety vest Singapore

    ReplyDelete
  18. Thanks for another informative site. Where else could I get that type of info written in such an ideal way? I have a project that I am just now working on, and I've been on the look out for such info. bed bug treatment

    ReplyDelete
  19. Welcome to Our Call Girls contact number in Chanakyapuri offers a wide range of sexual offerings to everyone looking for the best Low budget Call Girls in Faridabad.
    Carnal pleasure with Low budget Call Girls in Faridabad who know the most unforgettable arts of loving like no one else does.
    Arousing women with a lot of experience who will enjoy seeing you fulfil all those desires you’ve been repressing for so long, even the most daring and kinky ones.
    Beautiful High Class Call Girls in Agra, sought after by demanding men who want to enjoy aunties, housewives or a bhabhi for the best sex session of their lives, and let their kisses and caresses explore the most sensitive parts of their bodies.
    Without a doubt, at High Class Call Girls in Agra you’ll enjoy the most involving, friendly with High Class Call Girls in Ajmer in all of India and you won’t hesitate to come back for more.

    ReplyDelete
  20. I loved as much as you'll receive carried out right here. The sketch is attractive, your authored subject matter stylish. nonetheless, you command get bought an shakiness over that you wish be delivering the following. unwell unquestionably come further formerly again as exactly the same nearly a lot often inside case you shield this increase. shock absorber malaysia

    ReplyDelete
  21. the current business problem in consideration, hire someone write my assignment identifying the data sources which would be required for data mining. Understanding the key features required for modeling.

    ReplyDelete
  22. Best casino bonus codes 2021 | Free spins no deposit
    Find a list of the worrione casino bonus codes and promotions for United Kingdom goyangfc players. Discover bonus codes for casinos with free spins no deposit on registration.‎How many kadangpintar free spins do you casinosites.one receive from the casino? https://septcasino.com/review/merit-casino/ · ‎What are the bonuses for United Kingdom players? · ‎What are the free spins and promotions for United Kingdom players?

    ReplyDelete
  23. Don't miss out on the opportunity to become a proficient data scientist with APTRON Solutions. Enroll in our Data Science Training Course in Noida today and take the first step towards a successful career in one of the most in-demand fields of the 21st century. Let us empower you to harness the power of data and drive meaningful insights that transform businesses and industries. Unlock your potential with APTRON Solutions!

    ReplyDelete