http://cran.rstudio.com 下載并安裝 R 編程語(yǔ)言竖慧。
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': (行數(shù)) obs. of (列數(shù))variables:
#lists the variable names, #
#the type of each variable and some values for each variable.
dim(mtcars) #返回行數(shù)和列數(shù)
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表示截尾平均數(shù),0~0.5之間的數(shù)值凿将,如:0.10表示丟棄最大10%和最小的10%的數(shù)據(jù)后此叠,再計(jì)算算術(shù)平均數(shù)。默認(rèn)為0.