Input of R language data

Source: Internet
Author: User

Keyboard input

Call the edit function, for example, if we want the user to enter a vector of length 5 and assign it to variable A, then you can: A<-vector ("integer",5)
A<-edit (a) Alternatively, you can edit the variable directly with the function fix, without needing to assign a value variable. So the command to edit the a variable above can be changed to: A<-vector ("integer",5)
Fix (a)reading text FilesThe Read.table function can read a CSV file, or it can read a text file separated by other separators. If the TAB key is split, then "\ T" for example: y<-read.table ("Hw1_data.txt", header=true,sep= "\ t") if it is a standard CSV file, you can also use read.table to read with the Read.csv function: X<-read.csv ("Hw1_data.csv")read files in Excel formatExcel format is divided into the old XLS and the new xlsx two, in fact, the reading method is the same, generally now using the xlsx format of the Excel file, to read this format of the file, need to install PACKAGE:XLSX. Library (XLSX)
excelfile<-"test1.xlsx"
Excel<-read.xlsx (Excelfile,1The last parameter, 1, means to read the first sheet, and if you want to read the second sheet, change the parameter to 2.Read URLIf we want to read a URL file directly, we can use the URL function to create a connection, and then use the ReadLines function to get the content of the URL. For example: B<-url ("http://www.baidu.com")
Html<-readlines (b) These need to be explained that, in addition to the HTTP protocol, you can also use the FTP protocol file://shared folders. You can also set up agents that access the network.Read DatabaseIf you need to connect to the database in R, mostly using ODBC to connect, you need to install package rodbc. If it is a Linux or Mac platform, you can install the Rmysql package for MySQL database. Because I'm a Mac now, for example, I've created a table employee in the MySQL test database, and now I need to read the table. Library (Rmysql)
Conn<-dbconnect (MySQL (), dbname="Test", host="127.0.0.1"Next we want to see what tables we have, which can: Dblisttables (conn) if I want to query all the data in the employee table, then: Emp<-dbgetquery (Conn,"SELECT * from Employee ")Check out the database remember to close the connection, it is a good habit: Dbdisconnect (conn) if we are not connected to MySQL, then we need to install the corresponding database connection package. Like what:
    • Roracle
    • Rpostgresql
    • Rsqlite
Alternatively, you can use JDBC to access the database, which is RJDBC

Input of R language data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.