Chapter 4 Revision of R commands

In this chapter we are going to recap at some basic and useful functions we have used in R. The examples we use here will be helpful in revising few of the functions we studied and would give an baseline of function we would need in the future while programming in R.

List of commands:

  1. c()
  2. data.frame()
  3. subset()
  4. table()
  5. tapply()
  6. cut()
  7. summary(), mean(),length(), max(),min(), sd(),nrow(), ncol()
  8. class()

And also all the plot commands present in section 2.1


4.1 c() & data.frame() & class()

  • c()
    • The c() function is used for combining arguments.
    • The default behavior of the c() method is to combine its arguments to form a vector.
    • All arguments are coerced (forcibly converted) to a common type which is the type of the returned value.
    • For example,the non-character values are coerced to character type if one of the elements is a character.
    • the hierarchy followed is NULL < raw < logical < integer < double < complex < character < list < expression.
  • dataframe()
    • A data frame is a table or a two-dimensional array-like structure in which each column contains values of one variable and each row contains one set of values from each column.
    • Following are the characteristics of a data frame.
      • The column names should be non-empty.
      • The row names should be unique.
      • The data stored in a data frame can be of numeric, factor or character type.
      • Each column should contain same number of data items.
  • class()
    • The class() function has multiple uses, but for here, it is used to check the type of object.
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjTGV0cyBjcmVhdGUgMyB2ZWN0b3JzIHdpdGggdGl0bGUsIGF1dGhvciBhbmQgeWVhci5cbnRpdGxlIDwtIGMoJ0RhdGEgU21hcnQnLCdPcmllbnRhbGlzbScsJ0ZhbHNlIEltcHJlc3Npb25zJywnTWFraW5nIFNvZnR3YXJlJylcbmF1dGhvciA8LSBjKCdGb3JlbWFuLCBKb2huJywnU2FpZCwgRWR3YXJkJywnQXJjaGVyLCBKZWZmZXJ5JywnT3JhbSwgQW5keScpXG55ZWFyIDwtIGMoMjAxMCwyMDExLDIwMTIsMTk5OClcblxuI0xldHMgbG9vayBhdCBob3cgdGhlIGNyZWF0ZWQgdmVjdG9ycyBsb29rLlxudGl0bGVcbmF1dGhvclxueWVhclxuXG4jIEFsc28gbGV0cyBsb29rIGF0IHRoZWlyIHR5cGVzIHVzaW5nIHRoZSBjbGFzcyBmdW5jdGlvbi5cbmNsYXNzKHRpdGxlKVxuY2xhc3MoYXV0aG9yKVxuY2xhc3MoeWVhcilcblxuXG4jIE5vdyBsZXRzIGNyZWF0ZSBhIGRhdGFmcmFtZSB1c2luZyB0aGUgYWJvdmUgY29sdW1uIHZlY3RvcnMuXG5cbmRmIDwtIGRhdGEuZnJhbWUodGl0bGUsIGF1dGhvciwgeWVhcilcbmRmICMgTGV0cyBsb29rIGF0IGhvdyB0aGUgZGF0YWZyYW1lIGxvb2tzLiJ9

4.2 summary(), mean(),length(), max(),min(), sd(),nrow(), ncol(), dim()

The functions in this section are very simple yet are always useful to get more information from data.

  • summary() function computes summary statistics of data.
  • mean() function is used to find the average of the data.
  • sd() fucntion is used to find the standard deviation of the data.
  • length() function is used to get or set the length of data.
  • max() function is used to get the maximum valued element in the data.
  • min() function is used to get the minimum valued element in the data.
  • nrow() function is used to find the number/count of the rows present in data.
  • ncol() function is used to find the number/count of the columns present in data.
  • dim() function is used to find the dimensions of the data.

Lets look at example of all these functions.

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiJtb29keTwtcmVhZC5jc3YoXCJodHRwczovL3Jhdy5naXRodWJ1c2VyY29udGVudC5jb20vZGVlcGxva2hhbmRlL2RhdGExMDFkZW1vYm9vay9tYWluL2ZpbGVzL2RhdGFzZXQvbW9vZHkyMDIwYi5jc3ZcIikgI3dlYiBsb2FkIGRhdGFzZXRcblxuIyBMZXRzIGxvb2sgYXQgdGhlIHN1bW1hcnlcbnN1bW1hcnkobW9vZHkpXG5cbiNMZXRzIGxvb2sgYXQgdGhlIG51bWJlciBvZiByb3dzIGluIHRoZSBkYXRhc2V0LlxubnJvdyhtb29keSlcblxuI0xldHMgbG9vayBhdCB0aGUgbnVtYmVyIG9mIGNvbHVtbnMgaW4gdGhlIGRhdGFzZXQuXG5uY29sKG1vb2R5KVxuXG4jTGV0cyBsb29rIGF0IHRoZSBkaW1lbnNpb25zIGkuZS4gYm90aCBudW1iZXJzIG9mIHJvd3MgYW5kIGNvbHVtbnMgb2YgdGhlIGRhdGEgdXNpbmcganVzdCBvbmUgY29tbWFuZFxuZGltKG1vb2R5KVxuXG4jTGV0cyBsb29rIGF0IHRoZSBtZWFuIG9mIHNjb3JlIGNvbHVtbi5cbm1lYW4obW9vZHkkc2NvcmUpXG5cbiNMZXRzIGxvb2sgYXQgdGhlIHN0YW5kYXJkIGRldmlhdGlvbiBvZiBzY29yZSBjb2x1bW5cbnNkKG1vb2R5JHNjb3JlKVxuXG4jTGV0cyBsb29rIGF0IHRoZSBsZW5ndGggb2YgdGhlIGdyYWRlIGNvbHVtbiBcbmxlbmd0aChtb29keSRncmFkZSlcblxuI0xldHMgbG9vayBhdCB0aGUgbWluaW11bSB2YWx1ZSBvZiBzY29yZSBpbiB0aGUgc2NvcmUgY29sdW1uLlxubWluKG1vb2R5JHNjb3JlKVxuXG4jbGV0cyBsb29rIGF0IHRoZSBtYXhpbXVtIHZhbHVlIG9mIHRoZSBzY29yZSBpbiB0aGUgc2NvcmUgY29sdW1uXG5tYXgobW9vZHkkc2NvcmUpIn0=

4.3 Table

  • table() function in R Language is used to create a categorical representation of data with variable name and the frequency in the form of a table.
  • More use of table() is when you use multiple categorical columns. For example, we’ll see the count of grade vs asks_questions in example 2.
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiJtb29keSA8LSByZWFkLmNzdihcImh0dHBzOi8vcmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbS9kZWVwbG9raGFuZGUvZGF0YTEwMV90ZXN0L21haW4vTU9PRFktMjAxOS5jc3ZcIilcblxuXG50YWJsZWV4MTwtIHRhYmxlKG1vb2R5JEdSQURFKSAjVXNlIG9mIHRhYmxlICBmdW5jdGlvbiBvbiB0aGUgbmV3IGNvbHVtbi5cbnRhYmxlZXgxXG5iYXJwbG90KHRhYmxlZXgxLGNvbCA9YyhcInJlZFwiLFwicHVycGxlXCIsXCJjeWFuXCIsXCJ5ZWxsb3dcIixcImdyZWVuXCIpLHhsYWIgPSBcIkxhYmVsc1wiLCB5bGFiID0gXCJGcmVxdWVuY3lcIixtYWluID0gXCJ0YWJsZSgpIGV4YW1wbGUgMVwiKSAjcGxvdC5cblxuXG50YWJsZWV4MjwtdGFibGUobW9vZHkkR1JBREUsbW9vZHkkQVNLU19RVUVTVElPTlMpXG50YWJsZWV4MlxubW9zYWljcGxvdCh0YWJsZWV4Mixjb2wgPWMoXCJyZWRcIixcInB1cnBsZVwiLFwiY3lhblwiLFwieWVsbG93XCIsXCJncmVlblwiKSxtYWluID0gXCJ0YWJsZSgpIGV4YW1wbGUgMlwiKSJ9

4.3.1 Question What would R say?

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiJtb29keSA8LSByZWFkLmNzdihcImh0dHBzOi8vcmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbS9kZWVwbG9raGFuZGUvZGF0YTEwMV90ZXN0L21haW4vTU9PRFktMjAxOS5jc3ZcIilcblxudGFibGUobW9vZHlbbW9vZHkkQVNLU19RVUVTVElPTlMhPSdhbHdheXMnLF0kR1JBREUpXG4jV2hhdCB3aWxsIFIgc2F5P1xuXG4jIEEuIGVycm9yXG4jIEIuIGRpc3RyaWJ1dGlvbiBvZiBncmFkZXMgZm9yIHN0dWRlbnRzIHdobyBhbHdheXMgYXNrIHF1ZXN0aW9uc1xuIyBDLiBkaXN0cmlidXRpb24gb2YgZ3JhZGVzIGZvciBzdHVkZW50cyB3aG8gZG8gbm90IGFsd2F5cyBhc2sgcXVlc3Rpb25zICJ9

4.3.2 Question What would R say?

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiJtb29keSA8LSByZWFkLmNzdihcImh0dHBzOi8vcmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbS9kZWVwbG9raGFuZGUvZGF0YTEwMV90ZXN0L21haW4vTU9PRFktMjAxOS5jc3ZcIilcblxudGFibGUobW9vZHlbbW9vZHkkQVNLU19RVUVTVElPTlM9PWxpc3QoJ2Fsd2F5cycsJ25ldmVyJyksXSRHUkFERSlcbiNXaGF0IHdpbGwgUiBzYXk/XG5cbiMgQS4gZXJyb3IuXG4jIEIuIGRpc3RyaWJ1dGlvbiBvZiBncmFkZXMgZm9yIHN0dWRlbnRzIHdobyBhbHdheXMgb3IgbmV2ZXIgYXNrIHF1ZXN0aW9ucy4gIFxuIyBDLiBkaXN0cmlidXRpb24gb2YgZ3JhZGVzIGZvciBzdHVkZW50cyB3aG8gZG8gbm90IGFzayBxdWVzdGlvbnMgYWx3YXlzIG9yIG5ldmVyLiAifQ==

4.4 Subset

  • subset() function in R programming is used to create a subset of vectors, matrices or data frames based on the conditions provided in the parameters.

  • NOTE: To create a subset, not only can you use the subset() function, but also:

    • You can use [ ] operator. Ex: dataFrameName[‘columnName’]
    • Even $ operator is a subset operator. Ex: dataFrameName$columnName
  • Also, subsetting in R (commonly called “subscripting”) is done with square brackets. When subscripting a data frame there will be two places inside the square brackets separated by a comma.

  • The first part inside the square brackets corresponds to rows. The second part corresponds to columns.

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiJtb29keSA8LSByZWFkLmNzdihcImh0dHBzOi8vcmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbS9kZWVwbG9raGFuZGUvZGF0YTEwMV90ZXN0L21haW4vTU9PRFktMjAxOS5jc3ZcIilcblxuXG4jU3Vic2V0IG9mIHJvd3Ncbm1vb2R5X25ldmVyX3NtYXJ0cGhvbmU8LXN1YnNldChtb29keSxPTl9TTUFSVFBIT05FPT1cIm5ldmVyXCIpXG5ucm93KG1vb2R5KVxubnJvdyhtb29keV9uZXZlcl9zbWFydHBob25lKVxudGFibGUobW9vZHlfbmV2ZXJfc21hcnRwaG9uZSRPTl9TTUFSVFBIT05FKSAjIFlvdSBjYW4gc2VlIG9ubHkgc3R1ZGVudCBuZXZlciBvbiBzbWFydHBob25lIGFyZSBpbiB0aGUgc3Vic2V0LlxuXG4jQWx0ZXJuYXRlIHdheSB0byBzdWJzZXQuXG5tb29keV9uZXZlcl9zbWFydHBob25lX2FsdDw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ldCBzdWJzZXQgb2YgYWxsIGNvbHVtbnMgd2hpY2ggY29udGFpbnMgc3R1ZGVudHMgd2hvIHNjb3JlZCBtb3JlIHRoYW4gZXF1YWwgdG8gOTBcbiMgQi4gZXJyb3JcbiMgQy4gZ2V0IGFsbCBzY29yZSB2YWx1ZXMgd2hpY2ggYXJlIG1vcmUgdGhhbiBlcXVhbCB0byA5MFxuIyBELiBnZXQgc3Vic2V0IG9mIG9ubHkgdGhlIGdyYWRlcyBvZiBzdHVkZW50cyB3aXRoIHNjb3JlIGdyZWF0ZXIgdGhhbiBlcXVhbCB0byA5MCJ9

4.4.2 Question What would R say?

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiJtb29keSA8LSByZWFkLmNzdihcImh0dHBzOi8vcmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbS9kZWVwbG9raGFuZGUvZGF0YTEwMV90ZXN0L21haW4vTU9PRFktMjAxOS5jc3ZcIilcblxubW9vZHlbbW9vZHkkU0NPUkU+PTgwLjAgJiBtb29keSRHUkFERSA9PSdCJyxdIFxuIyBXaGF0IHdpbGwgUiBzYXk/XG5cbiMgQS4gc3Vic2V0IG9mIG1vb2R5IGRhdGEgZnJhbWUgd2hvIGdvdCBCIGdyYWRlLlxuIyBCLiBlcnJvci5cbiMgQy4gc3Vic2V0IG9mIG1vb2R5IGRhdGEgZnJhbWUgd2l0aCBzY29yZSBncmVhdGVyIHRoYW4gODAuXG4jIEQuIHN1YnNldCBvZiBtb29keSBkYXRhIGZyYW1lIHdpdGggc2NvcmUgbW9yZSB0aGFuIDgwIGFuZCBnb3QgQiBncmFkZS4ifQ==

4.5 tapply

  • tapply() function in R Language is used to apply a function over a subset of vectors given by a combination of factors
  • This is a very versatile function, as we’ll see from the use case.
  • Note : There are different aggregate functions that can be used. For example, Mean, Median, Variance, Sum etc.
  • We can also factor it on multiple attributes.
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiJtb29keSA8LSByZWFkLmNzdihcImh0dHBz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gY2F0ZWdvcnkgb2YgYXNrX3F1ZXN0aW9ucyBvbmx5IFxuIyBELiBlcnJvci4ifQ==

4.6 Cut

  • cut() function in R Language is used to divide a numeric vector into different ranges
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiJtb29keSA8LSByZWFkLmNzdihcImh0dHBzOi8vcmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbS9kZWVwbG9raGFuZGUvZGF0YTEwMV90ZXN0L21haW4vTU9PRFktMjAxOS5jc3ZcIilcblxuXG4jIFdlIGFjY2VzcyB0aGUgU2NvcmUgY29sdW1uIGZyb20gbW9vZHkgZGF0YXNldC5cbnNjb3JlMCA8LSBjdXQobW9vZHkkU0NPUkUsMTApXG50YWJsZShzY29yZTApICNsZXRzIGNoZWNrIHRoZSBkaXN0cmlidXRpb24gb2YgcGVvcGxlIGluIGVhY2ggcGFydGl0aW9uLlxuXG4jIEN1dCBFeGFtcGxlIHVzaW5nIGJyZWFrcyAtIEN1dHRpbmcgZGF0YSB1c2luZyBkZWZpbmVkIHZlY3Rvci4gXG5zY29yZTEgPC0gY3V0KG1vb2R5JFNDT1JFLGJyZWFrcz1jKDAsNTAsMTAwKSxsYWJlbHM9YyhcIkZcIixcIlBcIikpXG50YWJsZShzY29yZTEpIn0=

4.6.1 QuestionWhat would R say?

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiJtb29keSA8LSByZWFkLmNzdihcImh0dHBzOi8vcmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbS9kZWVwbG9raGFuZGUvZGF0YTEwMV90ZXN0L21haW4vTU9PRFktMjAxOS5jc3ZcIilcblxuY3V0KG1vb2R5JFNDT1JFLCBicmVha3M9YygwLDI1LDcwLDEwMCksbGFiZWxzPWMoXCJsb3dcIiwgXCJtZWRpdW1cIiwgXCJoaWdoXCIpKVxuI1doYXQgd291bGQgUiBzYXk/XG5cbiMgQS4gNSBpbnRlcnZhbHMgb2YgYXR0cmlidXRlIHNjb3JlXG4jIEIuIDM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5KG1vb2R5JGNvbmRpdGlvbmFsKVxuXG5ib3hwbG90KG1vb2R5JGNvbmRpdGlvbmFsLGNvbCA9IGMoXCJyZWRcIiksbWFpbj1cIkNvbXBsZXggRXhhbXBsZVwiKSJ9

4.7 What would R say?

In this section we will look at few examples based on the question “What do you think would R say?” All the questions are based on what we have studied in the sections above.

INSTRUCTIONS: Do not run the following examples directly, first ask yourself and note down, what do you think would R say? Only then run them. This is the only way to learn simple commands - and have them memorized so you can write code without having to check every single command.

4.7.1 Question

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiJ3ZWF0aGVyID1kYXRhLmZyYW1lKERheT1jKCd3ZWVrZGF5JywgJ3dlZWtlbmQnKSwgQ29uZGl0aW9ucyA9Yygnc3VubnknLCdyYWlueScsJ2Nsb3VkeScsICdzbm93JywgJ3N0b3JtJywnaWNlJykpXG5kaW0od2VhdGhlcilcbiN3aGF0IHdvdWxkIFIgc2F5P1xuXG4jIEEpIDYgNlxuIyBCKSAyIDZcbiMgQykgNiAyXG4jIEQpIEVycm9yIn0=

4.7.2 Question

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiJ3ZWF0aGVyID1kYXRhLmZyYW1lKERheT1jKCd3ZWVrZGF5JywgJ3dlZWtlbmQnKSwgQ29uZGl0aW9ucyA9Yygnc3VubnknLCdyYWlueScsJ2Nsb3VkeScsICdzbm93JywgJ3N0b3JtJywnaWNlJykpXG53ZWF0aGVyJHRlbXBlcmF0dXJlID1jKDgwLCA3MCwgNjUsIDQwLCAzMCwyNSlcbndlYXRoZXJbd2VhdGhlciR0ZW1wZXJhdHVyZSA+IDQwLF1cbmRpbSh3ZWF0aGVyKVxuI3doYXQgd291bGQgUiBzYXk/XG5cbiMgQSkgNiAzXG4jIEIpIHN1YnNldCBvZiB0aGUgZGF0YWZyYW1lIHdpdGggdGVtcGVyYXR1cmUgPiA0MC5cbiMgQykgQm90aCBBIGFuZCBCXG4jIEQpIEVycm9yIn0=

4.7.3 Question

eyJs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