您的位置:首页 > 其它

R语言-要点琐细及自定义函数的创建

2018-02-27 11:40 218 查看
# Time of establishment: 2018.2.27
# Restart of the R learning
# ------------------------------------------------------------------------------
# 1.the symble of connecting sentence when changing lines is +
# If a line is still not completed, but the key enter has been pushed, there
# will be a symbol "+" that means you can continue

# 2."#" is commenting symbol
# 3.ctrl + c is used to stop a function which is still working

# 4.object is to be manipulated
a <- 1      # establish an object
a <- a +2   # change the value of object a

# 5.uppercase and lowecase letters are different
# 6.the command "ls()" can show us the objects that exist
# 7.R langusge will use vector loop method to achieve the purpose of length matching
# for the mulplication of matrix,
# %*% --->>> in multiply
# %o% --->>> out multiply
die <- c(1:6)
inn <- die %*% die    # get a number
out <- die %o% die    # get a matrix
out
t(die)    # transpose. Vector can be seen as a column Vector
die
det(out)

# ------------------------------------------------------------------------------
# function
round(3.15426)    # as the name, rounding
factorial(5)      #
mean(die)         # to get the average value
sample(x = 1:4, size = 2)   # x means the vector to be sampled
# size means the number of members
sample(die, 1)              # simulate the craps process
args(sample)  # look up for the arguments of the function
round(3.14, digits = 1)   # remain a decimal
# note: sample function is without replacement originally
# in order to get the result with replacement
sample(die, size = 2, replace = TRUE)

# ------------------------------------------------------------------------------
# establish a function of mine
roll <- function(){
die <- 1:6
dice <- sample(die, size = 2, replace = TRUE)
sum(dice)
}

sim = roll()
# note: 8L means a long integer which is 8

# function with input arguements
roll2 <- function(bones){
dice <- sample(bones, size = 2, replace = TRUE)
sum(dice)
}

roll2(bones = 1:4)

# set defauts for the input arguements
roll3 <- function(bones = 1:6){
dice <- sample(bones, size = 2, replace = TRUE)
sum(dice)
}

roll3()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: