全部版块 我的主页
论坛 计量经济学与统计论坛 五区 计量经济学与统计软件 LATEX论坛
1529 3
2016-02-18


(This article was first published on Data Perspective, and kindly contributed to R-bloggers)
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:
  • Assignment Operator
  • Numeric
  • Integer
  • Complex number
  • logical
  • Character
  • Factor
  • Vector
  • Data Frame
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
复制代码

NumericNumeric data represents decimal data.
复制代码

To check the data type we use class() function:

复制代码

To check if the variable “x” is of numerical or not, we use
复制代码

To convert any compatible data into numeric, we use:
复制代码

IntegerWe use as.integer() function to convert into integers. This converts numeric value to integer values.
复制代码

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.
复制代码

Complex:Complex data types are shown as below, though we use it very less in our day to day data analysis:
复制代码

LogicalLogical 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.
复制代码

CharacterString literals or string values are stored as Character objects in R.
复制代码

We can convert other data types to character data type using as.character() function.
复制代码

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.

VectorHow 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:
复制代码

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.
复制代码

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.
复制代码

Similarly string vectors, logical vectors, integer vectors can be created.
复制代码

MatrixMatrix 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?
复制代码

Knowing the dimension of the matrix is:
复制代码

How do we access elements of matrix m:
复制代码

What happens when we add different data types to a vector?
复制代码

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.

ListWhat 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.
复制代码

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?
复制代码

Note: Observe the below data frame:
复制代码

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() 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.

复制代码

We have View() function to see the values of a data frame in a tabular form.
View(dt_frame)



Try Code×







http://feeds.feedburner.com/DataPerspective

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2016-2-27 11:03:07

Thanks for sharing
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2016-7-7 09:56:28

thanks
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2016-12-1 21:30:27
谢谢分享
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群