R Language Learning (4) - "R language portal"

  1. Object Naming
    must begin with a letter, case sensitive, can be a little behind the numbers underscore _.;
  2. Assignment
    use <-or ->; may be assigned are: a result value, a formula, a function
  3. LS function () is used to display all the objects in memory; RM function () delete an object,
    delete an object x: rm(x)
    Delete all objects: rm(list = ls())
    to find the object containing the letter m, it is necessary to set options pattern, such as: ls(pattern = "m")either short ls(pat = "m")
    ls.str()will exhibit For more information about all objects in memory, then it will return a lot of information (too detailed), if not so detailed, you can set max.level options (specifies the level of object information), specifically:
    ls.str(pat = "M", max.level = -1)

Object:

Properties of the object: the type and length. among them:

  • Type: numeric, character, and complex-type logic type, using mode()the function can be seen.
  • Length: the number of objects, using the element length()function can be seen.

InfAnd -Infrepresents a positive and negative infinity;
NaNrepresent non-numeric.

  1. Factor, is a categorical variable;
  2. Array is a K-dimensional data table;
  3. Matrix, all the elements of a two-dimensional data table, an array, and the matrix must be of the same type;
  4. Data block, there is a configuration factor or several vectors and must be of equal length, but may be different data types;
  5. "Ts" represents the time series data, comprising the additional attributes, such as frequency and time;
  6. List can contain any type of object, including a list.

Read data files:

Reading a text file (ASCII) data in use read.table () 或 scan 或 read.fwf, these basic data files.
Difference:
Function read.table(): Create a data box, read data in tabular form.

> mydata <- read.table("test.dat")
> mydata
     V1
1 a,b,c
2 3,2,3
> mydata <- read.table("test.dat", sep = ",")
> mydata
  V1 V2 V3
1  a  b  c
2  3  2  3
> 

Note: Each variable data box will be named are, if not specified name, default value will be referred to as: V1, V2, ...;
In addition, you can also access each variable separately, such as: mydata $ V1, mydata $ V2, ...
or mydata [ "V1"], mydata [ "V2"], ...
or mydata [, 1], mydata [ , 2]
the difference: results of a second data frame is removed, while the first and The third is a vector.
scan()Function to read more flexible, can specify the variable type, can also create different types of objects, if whatthe default, it will create a numerical vector.

> mydata <- scan("test.dat" , what = list(""))
Read 2 records
> mydata
[[1]]
[1] "a,b,c" "3,2,3"
#如果用 mydata <- scan("test.dat" , what = list("", 0 , 0))
#表示读取三个变量,第一个是字符型变量,第二个和第三个是数值型变量

read.fwfFunction can be used to read the file number of kinds of fixed-width data formats, such as:

> mydata <- read.fwf("test.dat", widths = c(1, 4, 3))
> mydata
  V1   V2  V3
1  A 1.50 1.2
2  B 1.55 1.3
3  C 1.60 1.4

Storing data:

  1. The object x written to the file:
> x = matrix(1:9 , 3 , 3)
> x
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9
> write(x , file = "test.dat" , append = TRUE)
  1. save.image() orsave(list = ls(all = TRUE) , file = ".RData")

Generate data:

#生成规则序列
> x <- 1:9
> x
[1] 1 2 3 4 5 6 7 8 9
> x <- 1:10-1
> x
 [1] 0 1 2 3 4 5 6 7 8 9
> seq(1 , 5 ,1)
[1] 1 2 3 4 5
> seq(length = 1 , from = 1, to = 5)
[1] 1
> seq(length = 5 , from = 1, to = 5)
[1] 1 2 3 4 5
> seq(length = 10 , from = 1, to = 5)
 [1] 1.000000 1.444444 1.888889 2.333333 2.777778
 [6] 3.222222 3.666667 4.111111 4.555556 5.000000
> c(1,23,3,4)
[1]  1 23  3  4
> #通过键盘输入
> x <- scan()
1: 1 2 3 4
5: 
Read 4 items
> x
[1] 1 2 3 4
> #使用rep函数创建所有元素相同的向量
> rep(2,5)
[1] 2 2 2 2 2
> #sequence()函数创建一系列连续的整数序列,每个序列都以给定的参数的数值结尾
> sequence(4:6)
 [1] 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6
> sequence(c(4,6))
 [1] 1 2 3 4 1 2 3 4 5 6
 #gl(k,n,length = , labels = c())--共k个水平,每个水平重复n遍,直到凑够length个为之,其中labels是每个水平的名称
>  gl(3,5, labels = c("a", "b" , "c"))
 [1] a a a a a b b b b b c c c c c
Levels: a b c
>  gl(3,5, labels = c("a", "b" , "c"), length = 20)
 [1] a a a a a b b b b b c c c c c a a a a a
Levels: a b c

Here Insert Picture Description

#使用expand.grid()创建数据框,完全搭配出各参数的各水平
> expand.grid(a = c(1,2) , b = c(10, 20) , sex = c("E", "F"))
  a  b sex
1 1 10   E
2 2 10   E
3 1 20   E
4 2 20   E
5 1 10   F
6 2 10   F
7 1 20   F
8 2 20   F
  • Generate random data:

Here Insert Picture Description
Basic forms: rfunc (n, p1, p2 , ...) represents a distribution func random number r, n need to generate a parameter that determines the value of the distribution function of p1, P2, ...
with the same manner, with a density function ( Dfunc), the cumulative probability density function, i.e. distribution function (pFunc), quantile function (qfunc (p, ...) where 0 <p <1)
the last two functions sequences can be used to find statistical test value of P or The critical value.
Here Insert Picture Description

user target audience:

  • Vector has two parameters: the type, length.
    Different types have their own default values, for example: Logical default FALSE, then the default character is "", the value 0 is the default type.
> x = vector(mode = "logical" , length = 3)
> x
[1] FALSE FALSE FALSE
> x = vector(mode = "numeric" , length = 2)
> x
[1] 0 0
> x = vector(mode = "character" , length = 3)
> x
[1] "" "" ""
  • Factors, including not only a categorical variable itself, but also different variables may level.
    Format:
    factor (X, = Sort levels (UNIQUE (X), na.last = TRUE), Labels = levels, the exclude = NA, orderd = is.ordered (X))
    in which the factors may be used to specify the levels of the level, the lack of value is the province of mutually different values in the vector x;
    Labels used to specify the name of the horizontal;
    the exclude excluded from the level value represents the vector x;
    ordered is a logical option to specify whether the level of factor orderly.

factor(1:3)
[1] 1 2 3
Levels: 1 2 3
factor(1:3 , levels = 1:5)
[1] 1 2 3
Levels: 1 2 3 4 5
factor(1:3 , labels = c(“a”,“D”,“C”))
[1] a D C
Levels: a D C
factor(1:5 , exclude = 4)
[1] 1 2 3 5
Levels: 1 2 3 5

![在这里插入图片描述](https://img-blog.csdnimg.cn/20200120180254274.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzNDQ4NDkx,size_16,color_FFFFFF,t_70)


Published 109 original articles · won praise 30 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_43448491/article/details/104047346