当前位置:网站首页>14 r basic exercises

14 r basic exercises

2022-06-11 20:09:00 THE ORDER

> x=list(aa=1:3,abab=matrix(NA,2,2),cc=c(T,F))
> x
$aa
[1] 1 2 3

$abab
     [,1] [,2]
[1,]   NA   NA
[2,]   NA   NA

$cc
[1]  TRUE FALSE

> x[c(1,3)]
$aa
[1] 1 2 3

$cc
[1]  TRUE FALSE

> x[[1]]
[1] 1 2 3
> x[1]
$aa
[1] 1 2 3

> x[[c(T,F,F)]]# Double brackets cannot be used logic Indexes 
Error in x[[c(T, F, F)]] : recursive indexing failed at level 2
> x$aa
[1] 1 2 3
> x=data.frame(id=1:3,math=89:91)
> x
  id math
1  1   89
2  2   90
3  3   91
> x$id  #dollar Symbol takes a column directly 
[1] 1 2 3
> x[id]# Report errors 
Error in `[.data.frame`(x, id) : object 'id' not found
> x[[c(1,3)]]
[1] 3
> x[[1]][3]
[1] 3
> x=1:3
> x[4]#  return NA
[1] NA
> x[2]=10
> x
[1]  1 10  3
> x[4]=2
> x
[1]  1 10  3  2
> x[7]=20# skip 5,6
> x# Auto fill 5,6 by NA
[1]  1 10  3  2 NA NA 20
> x=data.frame(id=1:5,sex=c(" male "," Woman "," male "," Woman "," male "),math=c(79,99,60,12,59),chn=c(80,69,61,99,57),english=c(82,89,70,90,50))
> x
  id sex math chn english
1  1   male    79  80      82
2  2   Woman    99  69      89
3  3   male    60  61      70
4  4   Woman    12  99      90
5  5   male    59  57      50
> x[x$math>=60,]# adopt math Column filter data frame ,logic Indexes 
  id sex math chn english
1  1   male    79  80      82
2  2   Woman    99  69      89
3  3   male    60  61      70
> x[x$math>=60 & x$chn>=60 & x$english>=60]# Add a comma at the end , Otherwise, it will be judged that the result is 3 That's ok , Show only before 3 Columns such as x[TRUE  TRUE  TRUE FALSE FALSE]
  id sex math
1  1   male    79
2  2   Woman    99
3  3   male    60
4  4   Woman    12
5  5   male    59
> x[x$math>=60 & x$chn>=60 & x$english>=60,]
  id sex math chn english
1  1   male    79  80      82
2  2   Woman    99  69      89
3  3   male    60  61      70
> x$math>=60 & x$chn>=60 & x$english>=60
[1]  TRUE  TRUE  TRUE FALSE FALSE
> x[5,3:4]=c(60)
> x
  id sex math chn english
1  1   male    79  80      82
2  2   Woman    99  69      89
3  3   male    60  61      70
4  4   Woman    12  99      90
5  5   male    60  60      50
> x$sum=x$math+x$chn+x$english# New column , Total score 
> x
  id sex math chn english sum
1  1   male    79  80      82 241
2  2   Woman    99  69      89 257
3  3   male    60  61      70 191
4  4   Woman    12  99      90 201
5  5   male    60  60      50 170
> mean(c(1,2,3))#mean middle , Average 
[1] 2
> x$sum=x$sum-mean(x$sum)
> x
  id sex math chn english sum
1  1   male    79  80      82  29
2  2   Woman    99  69      89  45
3  3   male    60  61      70 -21
4  4   Woman    12  99      90 -11
5  5   male    60  60      50 -42
> letters
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z"
> letters[26]
[1] "z"
> LETTERS
 [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"
> letters[-26]
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y"
> letters[c(1:13)*2]
 [1] "b" "d" "f" "h" "j" "l" "n" "p" "r" "t" "v" "x" "z"
> x=as.data.frame(matrix(1:25,5,5,dimnames = list(letters[1:5],letters[22:26])))
> x
  v  w  x  y  z
a 1  6 11 16 21
b 2  7 12 17 22
c 3  8 13 18 23
d 4  9 14 19 24
e 5 10 15 20 25
> x[2:3,2:4]
  w  x  y
b 7 12 17
c 8 13 18
> x[c(2,4),c(1,3,5)]
  v  x  z
b 2 12 22
d 4 14 24
> x[c("b","d"),c("v","x","z")]
  v  x  z
b 2 12 22
d 4 14 24
> x[x$v>2,]
  v  w  x  y  z
c 3  8 13 18 23
d 4  9 14 19 24
e 5 10 15 20 25
> x[rownames(x)>"b",colnames(x)>"w"]
   x  y  z
c 13 18 23
d 14 19 24
e 15 20 25
> x[3:5,3:5]
   x  y  z
c 13 18 23
d 14 19 24
e 15 20 25
> x$o=T
> x
  v  w  x  y  z    o
a 1  6 11 16 21 TRUE
b 2  7 12 17 22 TRUE
c 3  8 13 18 23 TRUE
d 4  9 14 19 24 TRUE
e 5 10 15 20 25 TRUE
> x=c(1,2,NA,4,NA,5)
> y=c("a","b",NA,"d","e",NA)
> x1=data.frame(x,y)
> x1
   x    y
1  1    a
2  2    b
3 NA <NA>
4  4    d
5 NA    e
6  5 <NA>
> x1$z=c(1:4,NA,6)
> x1
   x    y  z
1  1    a  1
2  2    b  2
3 NA <NA>  3
4  4    d  4
5 NA    e NA
6  5 <NA>  6
> x1[!is.na(x1$x),]# Filter the information according to the first column, which is not NA The column of 
  x    y z
1 1    a 1
2 2    b 2
4 4    d 4
6 5 <NA> 6
> complete.cases(x,y)# There is one NA for false
[1]  TRUE  TRUE FALSE  TRUE FALSE FALSE
> x1[complete.cases(x1$x,x1$y),]
  x y z
1 1 a 1
2 2 b 2
4 4 d 4
> x1[complete.cases(x1[1:2]),]
  x y z
1 1 a 1
2 2 b 2
4 4 d 4
> x1[complete.cases(x1),]
  x y z
1 1 a 1
2 2 b 2
4 4 d 4
> x1
   x    y  z
1  1    a  1
2  2    b  2
3 NA <NA>  3
4  4    d  4
5 NA    e NA
6  5 <NA>  6
> na.omit(x1)# Delete directly na Row of values 
  x y z
1 1 a 1
2 2 b 2
4 4 d 4
> 
> View(airquality)
> data()
> ?airquality
> airquality[airquality$Month==5,]
   Ozone Solar.R Wind Temp Month Day
1     41     190  7.4   67     5   1
2     36     118  8.0   72     5   2
3     12     149 12.6   74     5   3
4     18     313 11.5   62     5   4
5     NA      NA 14.3   56     5   5
6     28      NA 14.9   66     5   6
7     23     299  8.6   65     5   7
8     19      99 13.8   59     5   8
9      8      19 20.1   61     5   9
10    NA     194  8.6   69     5  10
11     7      NA  6.9   74     5  11
12    16     256  9.7   69     5  12
13    11     290  9.2   66     5  13
14    14     274 10.9   68     5  14
15    18      65 13.2   58     5  15
16    14     334 11.5   64     5  16
17    34     307 12.0   66     5  17
18     6      78 18.4   57     5  18
19    30     322 11.5   68     5  19
20    11      44  9.7   62     5  20
21     1       8  9.7   59     5  21
22    11     320 16.6   73     5  22
23     4      25  9.7   61     5  23
24    32      92 12.0   61     5  24
25    NA      66 16.6   57     5  25
26    NA     266 14.9   58     5  26
27    NA      NA  8.0   57     5  27
28    23      13 12.0   67     5  28
29    45     252 14.9   81     5  29
30   115     223  5.7   79     5  30
31    37     279  7.4   76     5  31
> airquality[airquality$Month==5 & airquality$Temp>70,]
   Ozone Solar.R Wind Temp Month Day
2     36     118  8.0   72     5   2
3     12     149 12.6   74     5   3
11     7      NA  6.9   74     5  11
22    11     320 16.6   73     5  22
29    45     252 14.9   81     5  29
30   115     223  5.7   79     5  30
31    37     279  7.4   76     5  31
> attach(airquality)#attach Fix it first airquality You can select columns directly 
> airquality[Month==5 & Temp>70,]# Be careful not to have the same name as a function in the global environment 
   Ozone Solar.R Wind Temp Month Day
2     36     118  8.0   72     5   2
3     12     149 12.6   74     5   3
11     7      NA  6.9   74     5  11
22    11     320 16.6   73     5  22
29    45     252 14.9   81     5  29
30   115     223  5.7   79     5  30
31    37     279  7.4   76     5  31
> search()#R Search path , When looking for a function , From here, search the corresponding function to realize its function , The search logic is to find... In the sub environment , Then find the parent environment 
 [1] ".GlobalEnv"        "airquality"        "tools:rstudio"     "package:stats"     "package:graphics"  "package:grDevices"
 [7] "package:utils"     "package:datasets"  "package:methods"   "Autoloads"         "package:base"     
> ?c
> detach(airquality)# Put down from the environment airquality
> library()# Package function , The newly installed function will be placed in the second position 
> detach("package:woe")# Unload the package 
原网站

版权声明
本文为[THE ORDER]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203011757348428.html