R language-Input and output

Source: Internet
Author: User
Tags random seed

Basic input and output

Input:

ReadLine, edit, Fix

Output:

Print, Cat

Output redirection

Sink

#basic input and outputx=ReadLine('Please enter:')#read input, one behavior of a stringX#[1] "1 2 3"MyData=data.frame (Name=character (0), Age=numeric (0), Height=numeric (0))#defining domain types by character (0)Mydata=edit (MyData)#modify directly with editMydatamydata2=data.frame ()#Create an empty data.frameMydata2=edit (MYDATA2) Mydata2fix (MYDATA2)#modify directly by fixMydata2#Output Print CatX=rnorm (Ten, -,2)  Print(x)#The print output has a newline at the end and an element position hint at the beginning of the lineCat (x)#cat output is not wrapped, orderedPrint(X,digits =4)#valid bitsPrint('Hello World 1') ;Print('Welcome to R') Cat ('Hello World 1'); Cat ('Welcome to R') Cat (format(x,digits=3),'\ n')#specify a valid bit by formatCat ('Hello World', file='D:/app.log')#REDIRECT sinkSink'output.txt', append = T)#Append mode redirectionPrint('Hello World') sink ()#Sink () required after useSink'D:/output.txt') Cat ('Hello world \ n') Cat (x,'\ n') sink () x=rnorm (Ten) xPrint(x,digits=2) getOption ('digits')#Get digits option information defaults to 7Options (digits =7)#modifying option informationX

Analog Data and datasets

#analog Data and Datasets # arbitrary distribution # y=a*x+b+e# X~n (0,2) # e~n (0,1) # b=0.5, a=2Set. Seed (Ten)#random seeds start with the same random seed, and the result of subsequent random numbers is the same.X=rnorm ( -,0,2) e=rnorm ( -) y=2*x +0.5+Eplot (x,y)#Generate random numbers(X=rbinom (5, -,0.7)) (x=rbinom (5, -,0.7)) Set. Seed (Ten) (x=rbinom (5, -,0.7)) (x=rbinom (5, -,0.7)) Data ( Package='Datasets')#Data ()##查看系统所有包中的数据集Data Package=.packages (all.available =T)) Library (Arules)#Load PackageData (groceries)#Querying data sets

File Data source

Text file:

Read.table, Read.csv, Read.delim

Excel file:

Multiple ways

SPSS file:

FOREIGN::READ.SPSS, Hmisc::spss.get

#File Data SourceGETWD ()#get current work pathSETWD ('e:/')#Modify the current work pathx=rnorm ( +,Ten,2) y=rnorm ( +,Ten,2) Z=rnorm ( +,Ten,2) Save (x, y,z,file='XYZ. Rdata')#Save saves the file while saving the variable informationL=load ('XYZ. Rdata')#Load load file, load variable informationx=Read. Table ('Scan0.txt', header=t,sep=' ', Comment.char ='@')#reading text Filesxstr (x) x=Read. CSV ('Scan0.txt', Header=f,comment.char ='@', sep=' ')#Read CSV fileSTR (x) x=Read. Delim ('Scan.txt', header=f,sep=' ') x=Read. Delim ('Clipboard', header=f)#reading clipboard informationx=scan ('Scan.txt', What=list (date="", pv=0, uv=0), sep=",")#What specifies the data type specify list name and data type via listx=as.data.frame (x)Write. Table (x,'Scan2.txt', sep=',', quote=f,col.names = t,row.names=f)#Write files #excel file # 1, CSV file # 2, clipboard +read.delim# 3, xlsx expansion Pack # 4, RODBC data sourceLibrary (foreign) cars=Read. SPSS ('Car_sales.sav') Library (HMISC) Cust=spss.get ('Car_sales.sav', Use. Value.labels =T) Cust

relational database MySQL

ODBC, Rmysql

#RmysqlInstall.packages ('Rmysql') Library (RMYSQL) Conn= Dbconnect (MySQL (), dbname='rtest', username='rtest', password='rtest', host="192.168.1.100", port=3306) dblisttables (conn) dblistfields (Conn,'T_user') Summary (MySQL (), verbose=T) Users=dbreadtable (Conn,'T_user') str (users) Userstmpuser=data.frame (Name=paste ('User',1: -, sep="'), Age=rnorm ( -, -,5)) tmpuserdbwritetable (Conn,'T_user', tmpuser,append=t,row.names=FALSE) dbwritetable (Conn,'T_stu', tmpuser,append=T) dbreadtable (Conn,'T_stu') Res=dbgetquery (Conn,'SELECT * from T_user where age>10') Resres=dbsendquery (Conn,'Show Databases')#Records and TypesDl=Fetch (RES) DLDBDISCONNECT (conn)#RODBCInstall.packages ('RODBC') Library (RODBC) Conn=odbcconnect ("Mysqlodbc") Conn=odbcconnect ("Mysqlodbc", uid="rtest", pwd='rtest') sqlTables (conn) Users=sqlfetch (Conn,'T_user') usersstr (users) Users=sqlquery (Conn,'SELECT * from T_user where age>15') Usersodbcclose (conn)

R language-Input and output

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.