楼主: Lisrelchen
5439 58

R Cookbook [推广有奖]

11
Lisrelchen 发表于 2015-9-6 12:00:40
  1. Performing Vector Arithmetic

  2. Problem
  3. You want to operate on an entire vector at once.

  4. Solution
  5. The usual arithmetic operators can perform element-wise operations on entire vectors. Many functions operate on entire vectors, too, and return a vector result.

  6. Discussion
  7. Vector operations are one of R’s great strengths. All the basic arithmetic operators can be applied to pairs of vectors. They operate in an element-wise manner; that is, the operator is applied to corresponding elements from both vectors:

  8. > v <- c(11,12,13,14,15)
  9. > w <- c(1,2,3,4,5)
  10. > v + w
  11. [1] 12 14 16 18 20
  12. > v - w
  13. [1] 10 10 10 10 10
  14. > v * w
  15. [1] 11 24 39 56 75
  16. > v / w
  17. [1] 11.000000  6.000000  4.333333  3.500000  3.000000
  18. > w ^ v
  19. [1]           1        4096     1594323   268435456 30517578125
复制代码

12
Lisrelchen 发表于 2015-9-6 12:04:09
  1. Getting and Setting the Working Directory

  2. Problem
  3. You want to change your working directory. Or you just want to know what it is.

  4. Solution
  5. Command line
  6. Use getwd to report the working directory, and use setwd to change it:

  7. > getwd()
  8. [1] "/home/paul/research"
  9. > setwd("Bayes")
  10. > getwd()
  11. [1] "/home/paul/research/Bayes"
复制代码

13
Lisrelchen 发表于 2015-9-6 12:04:53
  1. Saving Your Workspace

  2. Problem
  3. You want to save your workspace without exiting from R.

  4. Solution
  5. Call the save.image function:

  6. > save.image()
复制代码

14
Lisrelchen 发表于 2015-9-6 12:05:37
  1. Saving Your Workspace

  2. Problem
  3. You want to save your workspace without exiting from R.

  4. Solution
  5. Call the save.image function:

  6. > save.image()
复制代码

15
Lisrelchen 发表于 2015-9-6 12:06:42
  1. Viewing Your Command History

  2. Problem
  3. You want to see your recent sequence of commands.

  4. Solution
  5. Scroll backward by pressing the up arrow or Ctrl-P. Or use the history function to view your most recent input:

  6. > history()
复制代码

16
Lisrelchen 发表于 2015-9-6 12:07:39
  1. Saving the Result of the Previous Command

  2. Problem
  3. You typed an expression into R that calculated the value, but you forgot to save the result in a variable.

  4. Solution
  5. A special variable called .Last.value saves the value of the most recently evaluated expression. Save it to a variable before you type anything else.

  6. Discussion
  7. It is frustrating to type a long expression or call a long-running function but then forget to save the result. Fortunately, you needn’t retype the expression nor invoke the function again—the result was saved in the .Last.value variable:

  8. > aVeryLongRunningFunction()     # Oops! Forgot to save the result!
  9. [1] 147.6549
  10. > x <- .Last.value               # Capture the result now
  11. > x
  12. [1] 147.6549
复制代码

17
tbs20 发表于 2015-9-6 12:10:14
just a look

18
Lisrelchen 发表于 2015-9-6 12:11:51
  1. Displaying the Search Path

  2. Problem
  3. You want to see the list of packages currently loaded into R.

  4. Solution
  5. Use the search function with no arguments:

  6. > search()
复制代码

19
Lisrelchen 发表于 2015-9-6 12:13:35
  1. ccessing the Functions in a Package

  2. Problem
  3. A package installed on your computer is either a standard package or a package downloaded by you. When you try using functions in the package, however, R cannot find them.

  4. Solution
  5. Use either the library function or the require function to load the package into R:

  6. > library(packagename)
复制代码

20
Lisrelchen 发表于 2015-9-6 12:15:06
  1. Accessing Built-in Datasets

  2. Problem
  3. You want to use one of R’s built-in datasets.

  4. Solution
  5. The standard datasets distributed with R are already available to you, since the datasets package is in your search path.

  6. To access datasets in other packages, use the data function while giving the dataset name and package name:

  7. > data(dsname, package="pkgname")
复制代码

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

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2026-1-19 23:23