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 5class(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.

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?

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.

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.

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 framedata_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 objectclass(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 second1 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 framecolnames(data_frame)[1] "first" "second"#column data can accessed using the column names explicitly instead of column indexesdata_frame$first[1] 1 2 3 4data_frame$second[1] a b c dLevels: a b c d#accessing individual values using row and column indexesdata_frame[1,1] # accessing first row first column[1] 1data_frame[2,2] # accessing second row second column[1] bLevels: a b c ddata_frame[3,2] # accessing third row second column[1] cLevels: a b c ddata_frame[3,1] # accessing third row first column[1] 3