楼主: oliyiyi
1193 3

Basic Data Types in r [推广有奖]

版主

泰斗

0%

还不是VIP/贵宾

-

TA的文库  其他...

计量文库

威望
7
论坛币
271951 个
通用积分
31269.3519
学术水平
1435 点
热心指数
1554 点
信用等级
1345 点
经验
383775 点
帖子
9598
精华
66
在线时间
5468 小时
注册时间
2007-5-21
最后登录
2024-4-18

初级学术勋章 初级热心勋章 初级信用勋章 中级信用勋章 中级学术勋章 中级热心勋章 高级热心勋章 高级学术勋章 高级信用勋章 特级热心勋章 特级学术勋章 特级信用勋章

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币


(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
  1. x = 'welcome to R programming' # assigning string literal to variable x
  2. x
  3. [1] "welcome to R programming"
  4. typeof(x) #to check the data type of the variable x
  5. [1] "character"
复制代码

NumericNumeric data represents decimal data.
  1. x = 1.5 #assigning decimal value1.5 to x
  2. x
  3. [1] 1.5
复制代码

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

  1. class(x)
  2. [1] "numeric"
复制代码

To check if the variable “x” is of numerical or not, we use
  1. is.numeric(x)
  2. [1] TRUE
复制代码

To convert any compatible data into numeric, we use:
  1. x = '1' #assigning value 1 to variable x
  2. class(x)
  3. [1] "character"
  4. x = as.numeric(x)
  5. [1] 1
  6. class(x)
  7. [1] "numeric"

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

  9. x= 'welcome to R programming'
  10. as.numeric(x)
  11. [1] NA
  12. Warning message:
  13. NAs introduced by coercion
复制代码

IntegerWe use as.integer() function to convert into integers. This converts numeric value to integer values.
  1. x = 1.34
  2. [1] 1.34
  3. class(x)
  4. [1] "numeric"
  5. y = as.integer(x)
  6. class(y)
  7. [1] "integer"
  8. y
  9. [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.
  1. is.integer(y)
  2. [1] TRUE
  3. is.integer(x)
  4. [1] FALSE
复制代码

Complex:Complex data types are shown as below, though we use it very less in our day to day data analysis:
  1. c = 3.5+4i
  2. [1] 3.5+4i
  3. is.complex(c)
  4. [1] TRUE
  5. class(c)
  6. [1] "complex"
复制代码

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.
  1. logical = T
  2. logical
  3. [1] TRUE
  4. l = FALSE
  5. l
  6. [1] FALSE
复制代码

CharacterString literals or string values are stored as Character objects in R.
  1. str = "R Programming"
  2. str
  3. [1] "R Programming"
  4. class(str)
  5. [1] "character"
  6. is.character(str)
  7. [1] TRUE
复制代码

We can convert other data types to character data type using as.character() function.
  1. x = as.character(1)
  2. x
  3. [1] "1"
  4. class(x)
  5. [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.

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:
  1. num_vec = c(1,2,3,4,5)
  2. num_vec
  3. [1] 1 2 3 4 5
  4. class(num_vec)
  5. [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.
  1. length(num_vec)
  2. [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.
  1. num_vec[1]
  2. [1] 1
  3. num_vec[2]
  4. [1] 2
  5. num_vec[3]
  6. [1] 3
复制代码

Similarly string vectors, logical vectors, integer vectors can be created.
  1. char_vec = c("A", "Course","On","Data science","R rprogramming")
  2. char_vec
  3. [1] "A"  "Course" "On" "Data science" "R rprogramming"
  4. length(char_vec)
  5. [1] 5
  6. char_vec[1]
  7. [1] "A"
  8. char_vec[2]
  9. [1] "Course"
  10. char_vec[4]
  11. [1] "Data science"
复制代码

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?
  1. m = matrix(c(1,2,3,6,7,8),nrow = 2,ncol = 3)
  2. m
  3.      [,1] [,2] [,3]
  4. [1,]    1    3    7
  5. [2,]    2C    6    8
  6. class(m)
  7. [1] "matrix"
复制代码

Knowing the dimension of the matrix is:
  1. dim(m)
  2. [1] 2 3
复制代码

How do we access elements of matrix m:
  1. #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.
  2. m[1]
  3. [1] 1
  4. m[2]
  5. [1] 2
  6. m[6]
  7. [1] 8
  8. m[2,3] #  here we accessing 2nd row 3rd column element.
  9. [1] 8
  10. # accessing all elements of rows of the matrix m shown below.
  11. m[1,]
  12. [1] 1 3 7
  13. m[2,]
  14. [1] 2 6 8
  15. #accessing all elements of each column
  16. m[,1]
  17. [1] 1 2
  18. m[,2]
  19. [1] 3 6
  20. m[,3]
  21. [1] 7 8
复制代码

What happens when we add different data types to a vector?
  1. v = c("a","b",1,2,3,T)
  2. v
  3. [1] "a"    "b"    "1"    "2"    "3"    "TRUE"
  4. class(v)
  5. [1] "character"
  6. v[6]
  7. [1] "TRUE"
  8. class(v[6])
  9. [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.

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.
  1. list_exp = list("r programming","data perspective",12345,67890,TRUE,F)
  2. list_exp
  3. [[1]]
  4. [1] "r programming"
  5. [[2]]
  6. [1] "data perspective"
  7. [[3]]
  8. [1] 12345
  9. [[4]]
  10. [1] 67890
  11. [[5]]
  12. [1] TRUE
  13. [[6]]
  14. [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.
  1. str(list_exp)
  2. List of 6
  3. $ : chr "r programming"
  4. $ : chr "data perspective"
  5. $ : num 12345
  6. $ : num 67890
  7. $ : logi TRUE
  8. $ : logi FALSE
  9. #accessing the  data type of list_exp
  10. class(list_exp)
  11. [1] "list"
  12. length(list_exp)
  13. [1] 6
  14. list_exp[1]
  15. [[1]]
  16. [1] "r programming"
  17. #accessing the list elements using indexing.
  18. list_exp[[1]]
  19. [1] "r programming"
  20. list_exp[[6]]
  21. [1] FALSE
  22. list_exp[[7]] # when we try accessing not existing elements we get the below error.
  23. Error in list_exp[[7]] : subscript out of bounds
  24. # finding the class of individual list element
  25. class(list_exp[[6]])
  26. [1] "logical"
  27. class(list_exp[[3]])
  28. [1] "numeric"
  29. class(list_exp[[1]])
  30. [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?
  1. #creating a data frame
  2. data_frame = data.frame(first=c(1,2,3,4),second=c("a","b","c","d"))
  3. data_frame
  4.   first second
  5.      1      a
  6.      2      b
  7.      3      c
  8.      4      d
  9. #accessing  the data type of the object
  10. class(data_frame)
  11. [1] "data.frame"
  12. #finding out the row count of data_frame using nrow()
  13. nrow(data_frame)
  14. [1] 4
  15. #finding out the column count of data_frame using ncol()
  16. ncol(data_frame)
  17. [1] 2
  18. #finding out the dimensions of data_frame using dim()
  19. dim(data_frame)
  20. [1] 4 2
  21. #finding the structure of the data frame using str()
  22. str(data_frame)
  23. 'data.frame': 4 obs. of  2 variables:
  24. $ first : num  1 2 3 4
  25. $ second: Factor w/ 4 levels "a","b","c","d": 1 2 3 4
  26. #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.
  27. data_frame[1,]
  28.   first second
  29. 1     1      a
  30. #similarly to access only 1st column values without row information use data_frame[,1]
  31. data_frame[,1]
  32. [1] 1 2 3 4
  33. #accessing the row names of the data frame.
  34. rownames(data_frame)
  35. [1] "1" "2" "3" "4"
  36. #accessing the column names of the data frame
  37. colnames(data_frame)
  38. [1] "first"  "second"
  39. #column data can accessed using the column names explicitly instead of column indexes
  40. data_frame$first
  41. [1] 1 2 3 4
  42. data_frame$second
  43. [1] a b c d
  44. Levels: a b c d
  45. #accessing individual values using row and column indexes
  46. data_frame[1,1] # accessing first row first column
  47. [1] 1
  48. data_frame[2,2] # accessing second row second column
  49. [1] b
  50. Levels: a b c d
  51. data_frame[3,2]  # accessing third row second column
  52. [1] c
  53. Levels: a b c d
  54. data_frame[3,1] # accessing third row first column
  55. [1] 3
复制代码

Note: Observe the below data frame:
  1. 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"))
  2. dt_frame
  3.   first           second
  4.      1         Big data
  5.      2           Python
  6.      3                R
  7.      4              NLP
  8.      5 machine learning
  9.      6     data science
  10.      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.
  1. head(dt_frame)
  2.   first           second
  3.      1         Big data
  4.      2           Python
  5.      3                R
  6.      4              NLP
  7.      5 machine learning
  8.      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.

  1. tail(dt_frame)
  2.   first           second
  3.      2           Python
  4.      3                R
  5.      4              NLP
  6.      5 machine learning
  7.      6     data science
  8.      7 data perspective
复制代码

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

二维码

扫码加我 拉你入群

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

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

关键词:Types Basic ASIC Data type published practice provided article about

已有 1 人评分学术水平 热心指数 信用等级 收起 理由
janyiyi + 3 + 3 + 3 精彩帖子

总评分: 学术水平 + 3  热心指数 + 3  信用等级 + 3   查看全部评分

缺少币币的网友请访问有奖回帖集合
https://bbs.pinggu.org/thread-3990750-1-1.html
沙发
benji427 在职认证  发表于 2016-2-27 11:03:07 |只看作者 |坛友微信交流群

Thanks for sharing
已有 1 人评分经验 收起 理由
oliyiyi + 10 精彩帖子

总评分: 经验 + 10   查看全部评分

使用道具

藤椅
kyle2014 发表于 2016-7-7 09:56:28 |只看作者 |坛友微信交流群

thanks
已有 1 人评分经验 收起 理由
oliyiyi + 10 精彩帖子

总评分: 经验 + 10   查看全部评分

使用道具

板凳
janyiyi 发表于 2016-12-1 21:30:27 |只看作者 |坛友微信交流群
谢谢分享
已有 1 人评分论坛币 收起 理由
oliyiyi + 10 精彩帖子

总评分: 论坛币 + 10   查看全部评分

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-19 20:47