R基础

作者: esskeetit | 来源:发表于2018-01-14 11:16 被阅读0次

http://cran.rstudio.com 下载并安装 R 编程语言。

http://www.rstudio.com 下载并安装 RStudio。

create the vector 'udacious':

udacious <- c("Chris Saden", "Lauren Castellano",
                       "Sarah Spikes","Dean Eckles",
                       "Andy Brown", "Moira Burke",
                       "Kunal Chawla")

udacious

numbers <- c(1:10)

numbers

numbers  <- c(numbers, 11:20)               #add values to a vector

numbers

mystery is a vector that contains the number of characters for each of the names in udacious.

mystery = nchar(udacious)

mystery

Here we get a logical (or boolean) vector that tells us which locations or indices in the vector contain a name that has exactly 11 characters.

mystery == 11

udacious[mystery == 11]

data(mtcars)       #load the mtcars data, 

names(mtcars)   #返回列名

?mtcars              #the details and documentation will appear in the 'Help' tab.

mtcars                # the entire data frame printed out.

str(mtcars)          #gives us the structure of the data frame
                     #'data.frame': (行数) obs. of  (列数)variables:
                      #lists the variable names, #
                      #the type of each variable  and some values for each variable.

dim(mtcars)         #返回行数和列数

row.names(mtcars)                       #the current row names in the data frame.

row.names(mtcars) <- c(1:32)      #change the row names of the cars to numbers.

data(mtcars)                                 #reload the data set 

head(mtcars, 10)                          #print out the first ten rows.

head(mtcars)                                #The head() function prints out the first six rows
                                                     #of a data frame by default.

tail(mtcars, 3)                                #print out the last three rows.

mtcars$mpg                                  #access an individual column from the data
                                                        #frame (仅包含这一列的所有值)

mean(mtcars$mpg)                       #get the average mpg for all the cars.


平均值示例

x <- c(0:10, 50)

x

xm <-mean(x)

xm                                     #output:[1] 8.75

c(xm, mean(x, trim = 0.10))             #output:[1] 8.75 5.50

trim表示截尾平均数,0~0.5之间的数值,如:0.10表示丢弃最大10%和最小的10%的数据后,再计算算术平均数。默认为0.

相关文章

网友评论

      本文标题:R基础

      本文链接:https://www.haomeiwen.com/subject/dzlkoxtx.html