楼主: Lisrelchen
4238 58

R Cookbook [推广有奖]

21
Lisrelchen 发表于 2015-9-6 12:15:48 |只看作者 |坛友微信交流群
  1. Viewing the List of Installed Packages

  2. Problem
  3. You want to know what packages are installed on your machine.

  4. Solution
  5. Use the library function with no arguments for a basic list. Use installed.packages to see more detailed information about the packages.
复制代码

使用道具

22
Lisrelchen 发表于 2015-9-6 12:17:29 |只看作者 |坛友微信交流群
  1. Reading Fixed-Width Records

  2. Problem
  3. You are reading data from a file of fixed-width records: records whose data items occur at fixed boundaries.

  4. Solution
  5. Read the file using the read.fwf function. The main arguments are the file name and the widths of the fields:

  6. > records <- read.fwf("filename", widths=c(w1, w2, ..., wn
  7. ))
复制代码

使用道具

23
Lisrelchen 发表于 2015-9-6 12:18:25 |只看作者 |坛友微信交流群
  1. Reading Tabular Data Files

  2. Problem
  3. You want to read a text file that contains a table of data.

  4. Solution
  5. Use the read.table function, which returns a data frame:

  6. > dfrm <- read.table("filename")
复制代码

使用道具

24
Lisrelchen 发表于 2015-9-6 12:19:02 |只看作者 |坛友微信交流群
  1. Reading from CSV Files

  2. Problem
  3. You want to read data from a comma-separated values (CSV) file.

  4. Solution
  5. The read.csv function can read CSV files. If your CSV file has a header line, use this:

  6. > tbl <- read.csv("filename")
  7. If your CSV file does not contain a header line, set the header option to FALSE:

  8. > tbl <- read.csv("filename", header=FALSE)
复制代码

使用道具

25
Lisrelchen 发表于 2015-9-6 12:19:57 |只看作者 |坛友微信交流群
  1. Writing to CSV Files

  2. Problem
  3. You want to save a matrix or data frame in a file using the comma-separated values format.

  4. Solution
  5. The write.csv function can write a CSV file:

  6. > write.csv(x, file="filename", row.names=FALSE)
复制代码

使用道具

26
Lisrelchen 发表于 2015-9-6 12:20:41 |只看作者 |坛友微信交流群
  1. Reading Tabular or CSV Data from the Web

  2. Problem
  3. You want to read data directly from the Web into your R workspace.

  4. Solution
  5. Use the read.csv, read.table, and scan functions, but substitute a URL for a file name. The functions will read directly from the remote server:

  6. > tbl <- read.csv("http://www.example.com/download/data.csv")
  7. You can also open a connection using the URL and then read from the connection, which may be preferable for complicated files.
复制代码

使用道具

27
Lisrelchen 发表于 2015-9-6 12:21:55 |只看作者 |坛友微信交流群
  1. Reading Data from HTML Tables

  2. Problem
  3. You want to read data from an HTML table on the Web.

  4. Solution
  5. Use the readHTMLTable function in the XML package. To read all tables on the page, simply give the URL:

  6. > library(XML)
  7. > url <- 'http://www.example.com/data/table.html'
  8. > tbls <- readHTMLTable(url)
  9. To read only specific tables, use the which parameter. This example reads the third table on the page:

  10. > tbl <- readHTMLTable(url, which=3)
复制代码

使用道具

28
Lisrelchen 发表于 2015-9-6 12:22:38 |只看作者 |坛友微信交流群
  1. Reading Files with a Complex Structure

  2. Problem
  3. You are reading data from a file that has a complex or irregular structure.

  4. Solution
  5. Use the readLines function to read individual lines; then process them as strings to extract data items.

  6. Alternatively, use the scan function to read individual tokens and use the argument what to describe the stream of tokens in your file. The function can convert tokens into data and then assemble the data into records.
复制代码

使用道具

29
Lisrelchen 发表于 2015-9-6 12:24:18 |只看作者 |坛友微信交流群
  1. Reading from MySQL Databases

  2. Problem
  3. You want access to data stored in a MySQL database.

  4. Solution
  5. Install the RMySQL package on your computer.

  6. Open a database connection using the dbConnect function.

  7. Use dbGetQuery to initiate a SELECT and return the result sets.

  8. Use dbDisconnect to terminate the database connection when you are done.

  9. Discussion
  10. This recipe requires that the RMySQL package be installed on your computer. That package requires, in turn, the MySQL client software. If the MySQL client software is not already installed and configured, consult the MySQL documentation or your system administrator.

  11. Use the dbConnect function to establish a connection to the MySQL database. It returns a connection object which is used in subsequent calls to RMySQL functions:

  12. library(RMySQL)
  13. con <- dbConnect(MySQL(), user="userid", password="pswd",
  14.                  host="hostname", client.flag=CLIENT_MULTI_RESULTS)
复制代码

使用道具

30
Lisrelchen 发表于 2015-9-6 12:25:22 |只看作者 |坛友微信交流群

Appending Data to a Vector

  1. Appending Data to a Vector

  2. Problem
  3. You want to append additional data items to a vector.

  4. Solution
  5. Use the vector constructor (c) to construct a vector with the additional data items:

  6. > v <- c(v,newItems)
  7. For a single item, you can also assign the new item to the next vector element. R will automatically extend the vector:

  8. > v[length(v)+1] <- newItem
复制代码

使用道具

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

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-5-27 20:23