楼主: SleepyTom
2612 19

[学术资料] R Cookbook [推广有奖]

11
Lisrelchen(未真实交易用户) 发表于 2016-5-8 02:30:40
  1. 3.12. Running a Script

  2. Problem
  3. You captured a series of R commands in a text file. Now you want to execute them.

  4. Solution
  5. The source function instructs R to read the text file and execute its contents:

  6. > source("myScript.R")
复制代码

12
Lisrelchen(未真实交易用户) 发表于 2016-5-8 02:41:31
  1. 6.1. Splitting a Vector into Groups

  2. Problem
  3. You have a vector. Each element belongs to a different group, and the groups are identified by a grouping factor. You want to split the elements into the groups.

  4. Solution
  5. Suppose the vector is x and the factor is f. You can use the split function:

  6. > groups <- split(x, f)
  7. Alternatively, you can use the unstack function:

  8. > groups <- unstack(data.frame(x,f))
  9. Both functions return a list of vectors, where each vector contains the elements for one group.

  10. The unstack function goes one step further: if all vectors have the same length, it converts the list into a data frame.
复制代码

13
Lisrelchen(未真实交易用户) 发表于 2016-5-8 02:42:06
  1. 6.2. Applying a Function to Each List Element

  2. Problem
  3. You have a list, and you want to apply a function to each element of the list.

  4. Solution
  5. Use either the lapply function or the sapply function, depending upon the desired form of the result. lapply always returns the results in list, whereas sapply returns the results in a vector if that is possible:

  6. > lst <- lapply(lst, fun)
  7. > vec <- sapply(lst, fun)
复制代码

14
Lisrelchen(未真实交易用户) 发表于 2016-5-8 02:42:49
  1. 6.3. Applying a Function to Every Row

  2. Problem
  3. You have a matrix. You want to apply a function to every row, calculating the function result for each row.

  4. Solution
  5. Use the apply function. Set the second argument to 1 to indicate row-by-row application of a function:

  6. > results <- apply(mat, 1, fun)     # mat is a matrix, fun is a function
  7. The apply function will call fun once for each row, assemble the returned values into a vector, and then return that vector.
复制代码

15
Lisrelchen(未真实交易用户) 发表于 2016-5-8 02:43:50
  1. 6.4. Applying a Function to Every Column

  2. Problem
  3. You have a matrix or data frame, and you want to apply a function to every column.

  4. Solution
  5. For a matrix, use the apply function. Set the second argument to 2, which indicates column-by-column application of the function:

  6. > results <- apply(mat, 2, fun)
复制代码

16
Lisrelchen(未真实交易用户) 发表于 2016-5-8 02:44:51
  1. 6.5. Applying a Function to Groups of Data

  2. Problem
  3. Your data elements occur in groups. You want to process the data by groups—for example, summing by group or averaging by group.

  4. Solution
  5. Create a grouping factor (of the same length as your vector) that identifies the group of each corresponding datum. Then use the tapply function, which will apply a function to each group of data:

  6. > tapply(x, f, fun)
  7. Here, x is a vector, f is a grouping factor, and fun is a function. The function should expect one argument, which is a vector of elements taken from x according to their group.
复制代码

17
nieqiang110(真实交易用户) 学生认证  发表于 2016-5-8 06:57:38
xiexielouzhufenxiang

18
albertwishedu(未真实交易用户) 发表于 2016-5-8 08:26:01

19
bailihongchen(未真实交易用户) 发表于 2016-5-8 09:43:16
thanks for shairng

20
三鱼鱼(真实交易用户) 发表于 2016-5-8 21:52:45 来自手机
SleepyTom 发表于 2016-5-8 00:37
R Cookbook By Paul Teetor
Publisher: O'Reilly Media
Final Release Date: March 2011
楼主好人

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

本版微信群
加好友,备注jr
拉您进交流群
GMT+8, 2025-12-26 16:53