加入收藏 | 设为首页 | 会员中心 | 我要投稿 核心网 (https://www.hxwgxz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

『Data Science』R语言学习笔记,基础语法

发布时间:2021-02-28 05:03:27 所属栏目:大数据 来源:网络整理
导读:Data Types Data Object Vector x - c(0.5,0.6) ## numericx - c(TRUE,FALSE) ## logicalx - c(T,F) ## logicalx - c("a","b","c") ## characterx - 9:29 ## integerx - c(1+0i,2+4i) ## complexx - vector("numeric",length = 10) ## create a numeric vect
副标题[/!--empirenews.page--]

Data Types

Data Object & Vector

x <- c(0.5,0.6)        ## numeric
x <- c(TRUE,FALSE)     ## logical
x <- c(T,F)            ## logical
x <- c("a","b","c")     ## character
x <- 9:29               ## integer
x <- c(1+0i,2+4i)      ## complex

x <- vector("numeric",length = 10) ## create a numeric vector,which length is 10.

x <- 0.6    ## get the class type of the variable
class(x)    ## print the class type of "x".

x <- 1:10   ## set the class type to the variable forcibly.
as.character(x)

List

x <- list("...","...",...)

Matrices

Matrices are vectors with a dimension attribute. The dimension attribute is itself an integer vector of lenght 2 (nrow,ncol).

m <- matrix(nrow = 2,ncol = 3)
n <- matrix(1:6,nrow = 2,ncol = 3)

dim(m)          ## get the value of "norw,ncol" of the matrix.

attributes(m)   ## get the a of  

m <- 1:10           ## create a new numeric vector,from 1 to 10
dim(m) <- c(2,5)    ## put the vector "m" into a matrix,and assign the value (nrow = 2,ncol = 3) to it.
m                   ## print the value of "m".

x <- 1:3
y <- 10:12
cbind(x,y)     ## create a matrix by "cbind",binding the value of columns with variables,which has 3 rows and 2 columns.
rbind(x,y)     ## create a matrix by "rbind",binding the value of rows with variables,which has 2 rows and 3 columns.

Factors

Factors are used to represent categorical data. One can think of a factor is an integer vector where each integer has a label.

x <- factor(c("yes","yes","no","no"))  ## create a factor with a character vector.
x                                                       ## print the factor.
table(x)                                                ## list the label (with its quantity) of the factor in a table.
unclass(x)                                              ## list the value and the label of the factor.

x <- factor(c("yes",level("yes","no")))  ## create a factor with a character vector which had set the "levels" in it.

Missing Values

Missing values are denoted by NA of NaN for undefined mathematical operations.

is.na()     
is.nan()    

x <- c(1,2,NaN,NA,4)    ## Create a vector for test the functions,```is.na()``` and ```is.nan()```.
is.na(x)                    ## NA values have a class also,so there are integer NA,character NA,etc.
is.nan(x)                   ## A NaN value is also NA but the converse is not true.

Whole codes below:

> x <- c(1,10,3)
> is.na(x)
[1] FALSE FALSE  TRUE FALSE FALSE
> is.nan(x)
[1] FALSE FALSE FALSE FALSE FALSE
> x <- c(1,4)
> is.na(x)
[1] FALSE FALSE  TRUE  TRUE FALSE
> is.nan(x)
[1] FALSE FALSE  TRUE FALSE FALSE

Data Frames

Data frames are used to store tabular data.

  • They are represented as a special type of list where every element of the list has to have the same length.
  • Each element of the list can be thought of as a column and the length of each element of the list is the number of rows.
  • Unlike matrices,data frames can store different classes of objects in each column (just like lists);matrices must have every element be the same class.
  • Data frames also have a special attribute called row.names.
  • Data frames are usually created by calling read.table() or read.csv().
  • Can be converted to a matrix by calling data.matrix().
> x <- data.frame(foo = 1:4,bar = c(T,T,F,F))  ## create a Data Frame Object which has two columns and four rows.
> x
  foo   bar
1   1  TRUE
2   2  TRUE
3   3 FALSE
4   4 FALSE

Names

R objects can also have names,which is very useful for writing readable code and self-describing objects.

> x <- 4:6                              ## Create a integer vector 'x' which has three elements.
> names(x) <- c("foo","bar","norf")   ## Assign names to vector 'x'.
> x                                     ## Print the value of 'x'.
 foo  bar norf 
   4    5    6

Data Reading

Reading Data

  • read.table,read.csv,for reading tabular data,which return a data.frame object.
  • readLines,for reading lines of a text file.
  • source,for reading in R code files(inverse of dump).
  • dget,for reading in R code files(inverse of dput).
  • load,for reading in saved workspaces.
  • unserialize,for reading single R objects in binary form.

read.table

Description: Reads a file in table format and creates a data frame from it,with cases corresponding to lines and variables to fields in the file.

(编辑:核心网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读