# R Learning Note "three" R Language Beginner's Guide

Source: Internet
Author: User

1 Using C,cbind,rbind binding variables

Get four columns of data before WINGCRD,TARSUS,HEAD,WT

Each column contains 8 data, and you can continue to connect the variable via c

`Birddata <-C (WINGCRD,TARSUS,HEAD,WT) birddata[1] 59.0 55.0 53.5 55.0 52.5 57.5 53.0 55.0 22.3 19.7 20.8 20.3 20.8 21.5[ 20.6 21.5 31.2 30.4 30.6 30.3 30.3 30.8 32.5   NA  9.5 13.8 14.8 15.2[29] 15.5 15.6 15.6 15.7`

Birddata is a single vector with a length of 32, and the symbol [1],[15],[29] does not need to consider different realities of computers. This is just a single vector, and R does not differentiate which of these values belong to which variable. Pass:

`ID <-C (1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4) ID <-Rep (c (1,2,3,4), each = 8) ID <- Rep (1:4,each = 8)`

These three expressions have the same effect.

`ID <-Rep (1:4,each = 8) ID [1] 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4`
`A <-seq (from =1, to = 4,by = 1) Rep (A,each = 8)`

Effect:

`A <-seq (from =1, to = 4,by = 1) Rep (A,each = 8) [1] 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 Var Names <-C ("Wingcrd", "Tarsus", "Head", "Wt") Id2 <-rep (Varnames,each = 8) varnames <-C ("Wingcrd", "Tarsus", "head "," Wt ") Id2 <-rep (Varnames,each = 8) id2[1]" WINGCRD "" WINGCRD "" WINGCRD "" WINGCRD "" WINGCRD "" WINGCRD "" WINGCRD "[8]" Wi NGCRD "Tarsus" "  Tarsus"  "Tarsus"  "Tarsus"  "Tarsus"  "Tarsus" [All] "tarsus"  "Tarsus"  " Head "Head" "Head" "Head" "head" [+] "head" "Head" "    Head"    "wt"      "WT" "WT"      "WT"     [+] "wt"      "WT"      "WT"      "WT"     `

Req (varnames,8):

`Rep (varnames,8) [1] "WINGCRD" "Tarsus"  "Head"    "WT"      "WINGCRD" "Tarsus"  "Head"   [8] "WT"      " WINGCRD "Tarsus" "  Head"    "WT"      "WINGCRD" "Tarsus" [[] "Head"    "WT" "      WINGCRD" "Tarsus"  " Head "    wt"      "WINGCRD" [+] "tarsus"  "Head"    "WT" "WINGCRD" "      Tarsus" "  Head"    "WT"     [] "WINGCRD" "Tarsus" "  Head"    "Wt"`

The Cbind function outputs the combined variable as a column

`Z <-cbind (wingcrd,tarsus,head,wt) z     wingcrd tarsus Head   wt[1,]    59.0   22.3 31.2  9.5[2,]    55.0   19.7 30.4 13.8[3,]    53.5 20.8 30.6   14.8[4,]    55.0   20.3 30.3 15.2[5,]    52.5   20.8 30.3 15.5[6,]    57.5   21.5 30.8 15.6[7,]    53.0   20.6 32.5 15.6[8,]    55.0   21.5   NA 15.7`

Access the first column of Z

`Z[,1][1] 59.0 55.0 53.5 55.0 52.5 57.5 53.0 55.0z[1:8,1][1] 59.0 55.0 53.5 55.0 52.5 57.5 53.0 55.0`

Access the first line of Z

`Z[1,]WINGCRD  Tarsus    head      Wt    59.0    22.3    31.2     9.5 z[1,1:4]wingcrd  tarsus    Head      Wt    59.0    22.3    31.2     `

The same access method can be used:

`Z[1,]z[1,1:4]z[1,1]z[,2:3]x <-z[4,4]y <-z[,4]w <-z[,3] #得到第三列的数据D <-z[,c (1,3,4)] #得到1, 3, 4 columns of all data e <-Z[, C ( -1,-3)] #负号表示排除第一第三列`

Display the dimensions of Z

`Dim (z) Dim (z) [1] 8 4`

View only the number of z rows

`Nrows <-Dim (Z) [1]nrows[1] 8`

Rbind is similar to the Cbind function, except that the former represents data in the form of rows, which represent data in columns

`Z2 <-rbind (WINGCRD,TARSUS,HEAD,WT) Z2        [, 1] [, 2] [, 3] [, 4] [, 5] [, 6] [, 7] [, 8]WINGCRD 59.0 55.0 53.5 55.0 52.5 57.5 53.0 55.0Tarsus  22.3 19.7 20.8 20.3 20.8 21.5 20.6 21.5Head    31.2 30.4 30.6 30.3 30.3 30.8 32.5   nawt       9.5 13 .8 14.8 15.2 15.5 15.6 15.6 15.7`

Using vectors to represent data

`W <-vector (length = 8) w[1] <-59w[2] <-55w[3] <-53.5w[4] <-55w[5] <-52.5w[6] <-57.5w[7] <-5 3W[8] <-55w[1] 59.0 55.0 53.5 55.0 52.5 57.5 53.0 55.0`

Description

After entering W <-vector (length = 8), the direct re-entry of W will get a false response value.

You must enter the value of all elements before you can enter W to view the data

Data can be viewed via w[1],w[1:4],w[2:6],w[-2],w[c, etc.)

Combining data through matrices

`Dmat <-matrix (nrow = 8,ncol = 4) Dmat     [, 1] [, 2] [, 3] [, 4][1,]   na   na   na   na[2,]   na   na   Na   na[3,] na na na   na[4,]   na na na   na[5,]   na   na Na   Na[6,]   Na Na na   na[7,] na na na   na[8,]   na   na Na na`

Initialize matrix

`dmat[,1] <-C (59,55,53.5,55,52.5,57.5,53,55) dmat[,2] <-C (22.3,19.7,20.8,20.3,20.8,21.5,20.6,21.5) Dmat[,3] <-C (31.2,30.4,30.6,30.3,30.3,30.8,32.5,na) dmat[,4] <-C (9.5,13.8,14.8,15.2,15.5,15.6,15.6,15.7) Dmat     [  , 1] [, 2] [, 3] [, 4][1,] 59.0 22.3 31.2  9.5[2,] 55.0 19.7 30.4 13.8[3,] 53.5 20.8 30.6 14.8[4,] 55.0 20.3 30.3 15.2[5,] 52.5 20.8 30.3 15.5[6,] 57.5 21.5 30.8 15.6[7,] 53.0 20.6 32.5 15.6[8,] 55.0 21.5   NA 15.7`

Use the Colnames function to add a name to the column of the Matrix Dmat

`Colnames (Dmat) <-C ("Wingcrd", "Tarsus", "Head", "Wt") Dmat     wingcrd tarsus Head   wt[1,]    59.0   22.3 31.2  9.5[2,]    55.0   19.7 30.4 13.8[3,]    53.5   20.8 30.6 14.8[4,]    55.0   20.3 30.3 15.2[5,]    52.5   20.8 30.3 15.5[6,]    57.5 21.5 30.8   15.6[7,]    53.0   20.6 32.5 15.6[8,]    55.0   21.5   NA 15.7`

When the data is categorized by variable, you can:

`DMAT2 <-As.matrix (Cbind (WINGCRD,TARSUS,HEAD,WT)) Dmat2     wingcrd tarsus Head   wt[1,]    59.0   22.3 31.2  9.5[2,]    55.0   19.7 30.4 13.8[3,]    53.5   20.8 30.6 14.8[4,]    55.0   20.3 30.3 15.2[ 5,]    52.5   20.8 30.3 15.5[6,]    57.5   21.5 30.8 15.6[7,]    53.0   20.6 32.5 15.6[8,]    55.0   21.5   NA 15.7`

Using the Data.frame function to combine data

Use a data frame to combine variables with the same length, and each row of the data frame contains different observations of a uniform sample.

eg

`Dfrm <-data.frame (WC = Wingcrd,ts = tarsus,hd=head,w=wt) dfrm    WC   TS   HD    W1 59.0 22.3 31.2  9.52 55.0 19.7 30.4 13.83 53.5 20.8 30.6 14.84 55.0 20.3 30.3 15.25 52.5 20.8 30.3 15.56 57.5 21.5 30.8 15.67 53.0 20.6 32.5 15 .55.0 21.5   NA 15.7`

Advantages of the data frame: You can change the data based on the original data without changing it. eg

`Dfrm2 <-data.frame (WC = Wingcrd,ts = Tarsus,hd=head,wsq=sqrt (Wt)) Dfrm2    WC   TS   HD      Wsq1 59.0 22.3 31.2 3.0822072 55.0 19.7 30.4 3.7148353 53.5 20.8 30.6 3.8470774 55.0 20.3 30.3 3.8987185 52.5 20.8 30.3 3.9370046 57.5 21.5 30 .8 3.9496847 53.0 20.6 32.5 3.9496848 55.0 21.5   NA 3.962323`

WT and W are different entities that verify:

`RM (WT) WT`

`DFRM\$W[1]  9.5 13.8 14.8 15.2 15.5 15.6 15.6 15.7`

The usual use of data frames:

Make some changes to the data after you enter data into R (remove extreme values, apply changes, add categorical variables, and so on), and then store the data in a data frame for subsequent analysis.

Using the list to combine data

With list data, each data in a list can be either a vector or a single data. The dimensions of the vectors may or may not be the same.

eg

`X1 <-C (X2) <-C ("A", "B", "C", "D") X3 <-3X4 <-matrix (nrow = 2, Ncol = 2) x4[,1] <-C (+) x4[,2] <- C (3,4) Y <-list (lx1=x1,lx2=x2,lx3=x3,lx4=x4) y\$lx1[1] 1 2 3`

`\$LX 2[1] "a" "B" "C" "D"`

`\$LX 3[1] 3`

`\$LX 4     [, 1] [, 2][1,]    1    3[2,]    2    4`

The importance of list:

The results of linear regression, generalized linear regression, T-test, etc. are usually saved in the list

`M <-lm (WC ~ w,data = dfrm) mCall:`
`LM (formula = WC ~ W, data = dfrm)Coefficients:(Intercept) W65.5315-0.7239`

The results of the slender analysis are stored in:

`Names (M) [1] "coefficients"  "residuals"     "Effects"       "Rank"         [5] "fitted.values" "Assign"        "QR"            "Df.residual"  [9] "xlevels"       "call" "          terms" "         model"        `

You can access specific values in the following ways

`M\$coefficients (Intercept)           W  65.5315140  -0.7238731 m\$residuals         1          2          3          4          5          6          7  0.3452800-0.5420659-1.3181928  0.4713564-1.8114817  3.2609056-1.2390944          8  0.8332929 m\$effects (Intercept)            W                                        -155.7402686    4.0250694   -1.2416235    0.5887546   - 1.6634618                                           3.4191327   -1.0808673    `

Comprehensive:

`AllData <-list (birddata = Birddata,id = Id2,z = Z,varnames = varnames) alldata\$birddata [1] 59.0 55.0 53.5 55.0 52.5 57 .5 53.0 55.0 22.3 19.7 20.8 20.3 20.8 21.5[15] 20.6 21.5 31.2 30.4 30.6 30.3 30.3 30.8 32.5   NA  9.5 13.8 14.8 15.2[ 15.5 15.6 15.6 15.7\$id [1] "WINGCRD" "WINGCRD" "WINGCRD" "WINGCRD" "WINGCRD" "WINGCRD" "WINGCRD" [8] "WINGCRD" "Tarsus "  Tarsus" "  Tarsus"  "Tarsus" "  Tarsus"  "Tarsus" [[] "Tarsus"  "Tarsus"  "Head"    Head "Head" "Head" "Head"    [+   ] "head" "Head"    "head"    "WT" "      wt" "WT" "      wt"     [+] "wt" "WT"      "wt"      "WT"     \$Z     wingcrd tarsus Head   wt[1,]    59.0   22.3 31.2  9.5[2,]    55.0   19.7 30.4 13.8[3,]    53.5   20.8 30.6 14.8[4,]    55.0   20.3 30.3 15.2[5,]    52.5   20.8 30.3 15.5[6,]    57.5   21.5 30.8 15.6[7,]    53.0   20.6 32.5 15.6[8 ,]    55.0   21.5   NA 15.7\$varnames[1] "WINGCRD" "Tarsus"  "Head"    "Wt"`

Take one of these elements separately

`Alldata\$birddata [1] 59.0 55.0 53.5 55.0 52.5 57.5 53.0 55.0 22.3 19.7 20.8 20.3 20.8 21.5[15] 20.6 21.5 31.2 30.4 30.6 30 .3 30.3 30.8 32.5   NA  9.5 13.8 14.8 15.2[29] 15.5 15.6 15.6 15.7alldata\$id [1] "WINGCRD" "WINGCRD" "WINGCRD" "WINGC Rd "" WINGCRD "" WINGCRD "" WINGCRD "[8]" WINGCRD "" Tarsus "" Tarsus "" Tarsus "" Tarsus "" Tarsus  "  " Tarsus "[[]" "Tarsus" "Tarsus" "Head" "Head" "Head" "Head" "Head"  [+   ] "head" "Head" "Head" "wt" "WT" "WT"      "WT"     [] "WT"      "WT" "WT"      "WT"     alldata\$z     WINGCRD Tarsus Head   wt[1,]    59.0   22.3 31.2  9.5[2,]    55.0   19.7 30.4 13.8[3,]    53.5   20.8 30.6 14.8[4,]    55.0   20.3 30.3 15.2[5,]    52.5   20.8 30.3 15.5[6,]    57.5   21.5 30.8 15.6[7,]    53.0   20.6 32.5 15.6[8,]    55.0   21.5   NA 15.7`

Note: only use in list = Cannot use <-

R Learning Note "three" R Language Beginner's Guide

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.