楼主: oliyiyi
1056 0

Practical uses of R object modes: some examples [推广有奖]

版主

已卖:2993份资源

泰斗

1%

还不是VIP/贵宾

-

TA的文库  其他...

计量文库

威望
7
论坛币
101070 个
通用积分
31671.0217
学术水平
1454 点
热心指数
1573 点
信用等级
1364 点
经验
384134 点
帖子
9629
精华
66
在线时间
5508 小时
注册时间
2007-5-21
最后登录
2025-7-8

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

楼主
oliyiyi 发表于 2016-2-18 15:22:09 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币


(This article was first published on R-exercises, and kindly contributed to R-bloggers)

[color=rgb(255, 255, 255) !important]

One of our readers commented on our mode exercises post: “What real world tasks are you using mode to solve?” I think it’s an interesting question, from a somewhat larger perspective. Obviously, it’d be a waste of time to learn all kinds of obscure commands that don’t have a clear application in the real world. Fortunately, that’s not the case with the mode function, and its cousins, as we’ll see shortly.

If you’re new to R, you might be particularly interested in getting up to speed quickly, and focus on those commands that produce a graph, table, or prediction, or jump straight to some of the popular packages that offer the latest data science toys.

If this approach works for you, great! Many R beginners will suffer unnecessarily however, by taking this route. Why? Read on…

R and Italian, an analogy

Consider this analogy between learning R and learning a real language (as in, Italian or Chinese). Real languages have words that convey strong, clear messages (“Water”, “Money”, “Sleep”), as well al words that play a more supportive role (“you need”, “A little”, “Enough”), and words that may convey even less information (“Actually”, “Apparently”). (For more analogies like these, read this post on the ideas behindour exercise sets).

Likewise, R has commands that carry out big, important tasks with a lot of immediate practical value (plot, glm, table), as well as commands that play a more supportive role (data.frame, cut,as.Date).

Master the full R vocabulary

My point is this: You need both the strong words/big commands, and the more supportive words/commands to get something done in practice, whether it’s in Italian (like, communicating: “Apparently, you need a little sleep”) or in R (like: reading some data, cleaning it, running a model and plotting the results).

Many basic R functions, like mode, play this supportive role. In and of itself, they don’t have much practical value, but they are important little nuts and bolts that are indispensable to get your ultimate task done. And are therefore important to master, as part of your R vocabulary!

Practical uses of mode, some examples

Ok, so let’s get back to the original question and look at some practical uses of mode. Looking through some of my recent code, I noticed, I mostly use mode to convert characters to numbers and vice versa, and to check input values in functions.

Ex. 1: Data cleaning

For example, your raw data might have typos or wrong characters, as in the following example, where the last element of the input vector contains the letter ‘o’, instead of the zero digit. Such data can only be read as character, so after reading it into R, you’d have something like the vector x below. Using this vector in a calculation would throw an error.

  1. x <- c('20', '30', '4o')   # raw data,  
  2. x <- gsub('o', '0', x)     # cleaning
  3. mode(x) <- 'numeric'       # convert from character to numeric
  4. x + 1                      # we can now use x in calculations
  5. ## [1] 21 31 41
复制代码



Ex. 2: Checking input values

Another example of practical use of mode is to check input values in functions. Consider the sumfunction, which requires arguments of type numeric or logical. It will stop your script if it receives a character, e.g. sum(10, 'a'). Suppose this is not what you want. Instead you want a sum function that would simply return NA when it receives a character, without stopping the script. In that case you can use is.numeric() to check input values:

  1. mysum <- function(a, b) {
  2.   if(is.numeric(a) & is.numeric(b)) a + b
  3.   else NA
  4. }
  5. mysum(10, 20)
  6. ## [1] 30
  7. mysum(10, 'a')
  8. ## [1] NA
复制代码



Ex. 3: Subsetting

Finally, an example that involves subsetting of a table. Here we convert a numeric range (2005:2010) to character, to select a few columns (representing years) of a table:

  1. df <- data.frame(years=1991:2010, v=sample(1:10, 20000, T))
  2. mytable <- with(df, table(v, years))    # a cross-table with counts
  3. mytable[, as.character(2005:2010)]
  4. ##     years
  5. ## v    2005 2006 2007 2008 2009 2010
  6. ##   1    92  108  115  110   90  119
  7. ##   2   101   82   91   98  103   90
  8. ##   3    96   99   93   89   84  100
  9. ##   4    99  107   92  103  108   97
  10. ##   5    99   99  118  118  104   81
  11. ##   6    94   89   93   89   95  108
  12. ##   7   104  125  108  111  115   95
  13. ##   8   116   85  100   97  108  101
  14. ##   9   109   95   94   91   89  102
  15. ##   10   90  111   96   94  104  107
复制代码



Do you have other examples of practical uses of mode? Feel free to share below as a comment!

Image By Martorell – Self-published work by Martorell.









To leave a comment for the author, please follow the link and comment on their blog: R-exercises.



R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data,R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL,Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...


二维码

扫码加我 拉你入群

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

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

关键词:Practical Examples example practic object examples

缺少币币的网友请访问有奖回帖集合
https://bbs.pinggu.org/thread-3990750-1-1.html

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

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2025-12-21 06:55