楼主: oliyiyi
1204 1

R Programming Notes [推广有奖]

版主

已卖:2994份资源

泰斗

1%

还不是VIP/贵宾

-

TA的文库  其他...

计量文库

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

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

楼主
oliyiyi 发表于 2016-2-18 14:50:36 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币

I’ve been on a note taking binge recently. This post covers a variety of topics related to programming in R. The contents were gathered from many sources and structured in such a way that it provided the author with a useful reference guide for understanding a number of useful R functions.

DO.CALL

The do.call function executes a function call on a list of arguments.

  1. do.call("R_Function", "List_of_Arguments")
复制代码


This is equivilant to telling R which arguments the function should operate on.

  1. R_Function( "List_of_Arguments" ){
  2.   ...
  3. }
复制代码

Consider the following list with four elements. We can use this function to find the total sum across all list elements or bind the rows into a data frame.

  1. x1 <- c(1,2,5)
  2. x2 <- c(1,3,6)
  3. x3 <- c(1,4,7)
  4. x4 <- c(1,5,8)
  5.   
  6. do.call(sum, list(x1,x2,x3,x4))  # sum all list elements
  7. do.call(rbind, list(x1,x2,x3,x4))  # rbind the list elements
复制代码

Let’s consider a scenario where we have a small data frame and want to run a general linear model on different combintions of attributes. One solution to this problem is to create the formula object as a list within a function, and then utilize do.call to run a linear model on the desired attribute.

  1. dat <- data.frame(x1 = rnorm(100, m=50), x2 = rnorm(100, m=50), x3 = rnorm(100, m=50), y = rnorm(100))
  2.   
  3. new_mod <- function(form){
  4.   lstt = list(formula=as.formula(paste("y ~ ", form, sep="")), data=as.name("dat"))
  5.   summary(do.call("lm", lstt))
  6. }
  7.   
  8. new_mod("x1")
  9. new_mod("x2")
  10. new_mod("x3")
复制代码

EVAL

The eval function evaluates an expression, an object that represents an action that can be performed in R. An expression is different from an operation, which refers to the execution of an operation. In the following example, we assigned a value to a variable and performed an operation using that variable.

  1. x <- 4
  2. y <- x * 10
  3. y
复制代码

The expression and quote functions are used to takes an expression as an argument and returns an expression without evaluation.

  1. ee = expression(y~x1)
  2. ee
  3.   
  4. z <- quote(y <- x * 10)
  5. z
  6.   
  7. is.expression(ee)
  8. is.call(z)
复制代码

In the above example, z is referred to as a call object. They are usually created with the call function. For example, the following code pieces together commands into a call object and evaluates them.

  1. mycall <- call("lm", quote(y ~ x1), data=quote(dat))
  2. mod <- eval(mycall)
  3. summary(mod)
复制代码

SUBSTITUTE

Another common procedure is to replace certain variables within a user defined function. This can be achieved with the substitute function. The below code replaces x1 and x2 in the expression with the values from the list.

  1. replace <- list(x1 = as.name("alphabet"), x2 = as.name("zoology"))
  2. substitute(expression(x1 + x2 + log(x1) + x3), replace)
复制代码

SETNAMES

There are a number of commands for working within column and row names in R. It’s generally suggested that the setNames function be used when modifying data structure names within a function.

  1. names_new <- c("one","two","three","four")
  2. new_dat <- expression(data.frame(x1 = rnorm(100, m=50), x2 = rnorm(100, m=50), x3 = rnorm(100, m=50), y = rnorm(100)))
  3. head(setNames(eval(new_dat), names_new))
  4.   
  5. my_lst <- list(lname = as.name("xls"))
  6. setNames(my_lst, "x1")
复制代码

When working with data within a function, it will often be useful to write code that creates the names and data structure, and then evaluates the pieces together.

MISSING

Use the missing function to check whether an argument was supplied by the user.

  1. func <- function(x){
  2.     if(missing(x)){
  3.         x = 5
  4.     }
  5.     y = x*x; y
  6. }
  7.   
  8. func(2)
  9. func()
复制代码

STOP

The stop function is used to halt a function, and is usually used when a particular condition is met.

  1. guess <- function(n){
  2.   if( n >= 6 ){
  3.       stop("WRONG!")
  4. }}
  5.   
  6. guess(5)
  7. guess(10)
复制代码

WARNING

Use the warning function to throw a comment when a condition is met. It does not halt the function.

  1. guess <- function(n){
  2.   if( n >= 6 ){
  3.       warning("BE CAREFUL!")
  4. }}
  5.   
  6. guess(5)
  7. guess(10)
复制代码

ELIPSES (…)

When functions are evaluated, R scans each function argument it can understand. When ellipsis are added as a function argument, it allows for other arguments to be passed into the function. This technique is frequently used when plotting and should be used when the function is designed to take any number of named or unnamed arguments.

  1. plotter <- function(x, y, ...){
  2.   plot(x, y, ...)
  3. }
复制代码

To make use of ellipsis, it’s suggested that the user scan through the … and turn them into a list. This is because some of the arguments in dots may be intended for several different functions.


二维码

扫码加我 拉你入群

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

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

关键词:Programming Program notes Ming note structured reference function recently contents

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

沙发
oliyiyi 发表于 2016-2-18 14:53:48
(This article was first published on R – Mathew Analytics, and kindly contributed to R-bloggers)
缺少币币的网友请访问有奖回帖集合
https://bbs.pinggu.org/thread-3990750-1-1.html

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

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