您的位置:首页 > 其它

R语言-关于对象学习的一些注释和实例

2018-01-16 14:26 453 查看
# ------------------------------------------------------------------------------
# object -->> the entity that R is dealing with
# object :
#     atomic--
#     numeric--complex--logical--character--raw
# object -->> mode-length
z <- 0:9                    # here, z is an int vector
digits <- as.character(z)   # here, digits is a chr vector
d <- as.integer(digits)     # then, d is back to int mode
e <- numeric()              # empty numeric
e[3] <- 17                  # change the length and the mode
alpha <- c(1:10)
alpha <- alpha[2 * 1:5]     # obtain the values which address are even
length(alpha) <- 3          # save three elements which are at the beginning
length(alpha) <- 5          # expand the length of the vector
alpha_nia=attributes(alpha) # give the non-intrinsic attributes of the object
z <- matrix(1:9,3,3,T)      # obtain a matrix
# attr(z, "dim") <- c(10,10)  # allow R to treat z as a vector that is 10 * 10
# last command is still a stranger for me

# in R, every object has a specificated class
# which can be numeric, logical, character, list, matrix, array, factor, data.frame
winter <- c(1,2,2,3)
unclass(winter)   # unless() can remove the class mode

# --about factors     ---->>>> ordered | unordered
state <- c( "tas", "sa",  "qld", "nsw", "nsw", "nt",  "wa", "wa",
"qld", "vic", "nsw", "vic", "qld", "qld", "sa", "tas",
"sa",  "nt",  "wa",  "vic", "qld", "nsw", "nsw","wa",
"sa",  "act", "nsw", "vic", "vic", "act")
statef <- factor(state)
levers(statef)    # levels() gives the elements that are diffrent

incomes <- c( 60, 49, 40, 61, 64, 60, 59, 54, 62, 69, 70, 42, 56,
61, 61, 61, 58, 51, 48, 65, 49, 49, 41, 48, 52, 56,
59, 46, 58, 43)
incmeans <- tapply(incomes, statef, mean) # calculate the average value of each state

# below, gives a new function to get the standard error
# this is similar to the function handle in matlab
# of cause, there is a function, sd(), for the standard reeor calculation
stderr <- function(x) sqrt(var(x)/length(x))
incster <- tapply(incomes, statef, stderr)  # calculate the standard reeor of each state

# about the ordered factors, the levers of the factors are ordered reccording to the 1st letter
# ------------------------------------------------------------------------------
# end

----------------------------------------------------------------------------------------------------------------

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