现在的位置: 首页 > 综合 > 正文

R 基础

2018年02月19日 ⁄ 综合 ⁄ 共 3342字 ⁄ 字号 评论关闭

 

#======================R-demonstration script=============================

#--------------------------start and quit---------------------------------
R
quit()
save.image() #save data, although I would just copy code into a text file. Same as quit("yes")

#---------------------------comments--------------------------------------
#Text after pound sign is ignored (considered to be comments)

#----------------------------help-----------------------------------------
help("matrix")
#or
?matrix
#or search in help with
help.search("matrix")

#-----------------------Simple calculation--------------------------------
1+1
2^3 #following notation is also excepted: 2**3

#----------------------More complex functions-----------------------------
#function are of form functionname() like the help function we saw earlier
sum(3,6)
sin(3)
#you may(for clarity) include argument labels
help(topic="matrix")

#-------------------------variables---------------------------------------
#assignment sign is -> or <- . Variables maybe created in assignment
k<-2
#variables maybe listed with ls
ls()
#variable maybe deleted with rm()
rm(k)
ls()

#-------------------------vector/array------------------------------------
1:9
c("jouke","eric","raul")
array(0,5)
example.vector<-c(3,5,6,7)
example.vector

#---------------------------matrix----------------------------------------
example.matrix<-matrix(data=c(3,5,8,6),nrow=3,ncol=4)
example.matrix

#----------------------vector & matrix calculations-----------------------
#vector
3*1:3
#watch out for the following:
example.matrix*c(2,2,2)
#instead what you probably want
crossprod(example.matrix,c(2,2,2))

#--------------------------graphics---------------------------------------
#a simple plot
x<-1:10
y<-1/x
plot(x,y)
#demo of graphic capability of R:
demo(graphics)
#more advance settings can be set with par() or in the plot comand
par() #for a list of current settings
#plot works with many objects as demonstrate below


#-----------------------load data from file-------------------------------
#working directory
getwd()
setwd("/home/jap441/Desktop/R-demonstration")
#list files
list.files() #or dir()
#read data into tabel
dataTabel<-read.table("crop-simulation-sample-data.txt", header=TRUE, na.strings = c("NAN","NA","na","nan"))
#show data
dataTabel
summary(dataTabel)
plot(dataTabel)demo
# Read manual for import from EpiInfo, Minitab, S-PLUS, SAS, SPSS, Stata, Systat


#---------------------tables/matrix indexes & subsets---------------------
#colum time
dataTabel["TIME"]
dataTabel$TIME #which is equal to dataTabel[,"TIME"] watch the comma here!!!!!!!!!!
dataTabel[2]
#row 3
dataTabel[3,]
#number 4 of colum 3 (Note that since this colum contains texts, it is considered nominal and therefor levels are shown.
dataTabel[4,3]
#selection of a dataset with == >= <= != (note: this works with integers. Use for floating point comparison all.equal()
1:5>=3
(1:5)[1:5>=3]
(1:5)*(1:5>=3)
dataTabel["LAI"][dataTabel["LAI"]>=0]
#plot a selection
plot(dataTabel$TIME,dataTabel$LAI) #plot LAI over time
#create a temporal reference to a colum by attaching it (until detach)
LAI #gives error
attach(dataTabel)
LAI #is present
detach(dataTabel)
LAI #gives error

#-----------------------data types---------------------------------
#R uses several data types of which the following are of importance
#logical, numeric, integer, complex, character
#vector, factor, matrix, table, list
factor(c(1,5,1,7,5,7))
array(c(1,5,1,7,5,7))
#Since R is Object oriented, anything is really a datatype like a plot, or an anova
#Using the wrong data type can give you warning/error or inexpected results
8*array(1:5)
8*factor(1:5)
plot(array(1:5),c(5,3,7,8,4))
plot(factor(1:5),c(5,3,7,8,4))

抱歉!评论已关闭.