当前位置:网站首页>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 first bullet of comparative learning
- C#深拷贝
- Première formation sur les largeurs modernes
- 计算926的9260次方里的字符串里有多少个926
- Detailed explanation on persistence of 2022redis7.0x
- Global Market Consulting - Research Report on the overall scale and application segmentation of the global HVDC converter station market in 2022
- In 2021, the global revenue of Indian fragrant rice was about $12530 million, and it is expected to reach $21670 million in 2028
- 29. location对象
- 接口隔离原则
- Big work and requirements of economics in autumn 21 of Dagong [standard answer]
猜你喜欢

电源防反接和防倒灌 - 使用MOS 管和运放实现理想二极管

In 2021, the global barite product revenue was about $571.3 million, and it is expected to reach $710.2 million in 2028

使用flask框架写挡板

Calculate the number of 926 in the string to the power of 9260 of 926

ICML 2022 | 基于结构化数据的异常检测再思考:我们究竟需要怎样的图神经网络?...

sql优化之DATE_FORMAT()函数

Are there any techniques for 3D modeling?

The latest test questions and answers for the eight major members (standard members) of Ningxia architecture in 2022

接口隔离原则

Global Market Consulting - Research Report on the overall scale and application segmentation of the global HVDC converter station market in 2022
随机推荐
导师转我800块,让我仿真一个电路(电源设计)
Linux环境安装mysql数据库详细教程(含卸载和密码重置过程)
The tutor transferred me 800 yuan and asked me to simulate a circuit (power supply design)
黑圆圈显示实现
2022-2028 global and Chinese thermal power generation (TEG) module market status and future development trend
These postgraduate entrance examination majors are easy to be confused. If you make a mistake, you will take the exam in vain!
Major work title and requirements of engineering earthquake resistance in autumn 21 [standard answer]
MySQL——事务
这些考研专业容易混淆,搞错就白考啦!
C#深拷贝
Richter's substitution principle
2022-2028 global and Chinese thermopile infrared sensor market status and future development trend
2022-2028 current situation and future development trend of global and Chinese thermocouple belt Market
Apache APISIX 在Amazon Graviton3 上的安装和性能测试
[advanced MySQL] differences among 10 data types and how to optimize the table structure (3)
WR | 西湖大学鞠峰组微纳塑料污染对人工湿地菌群与脱氮功能的影响
Hanging memory recursive dynamic programming (with example explanation POJ 1163)
WR | effect of micro nano plastic pollution in Jufeng formation of West Lake University on microbial flora and nitrogen removal function of Constructed Wetland
【Bug解决】UnpicklingError: A load persistent id instruction was encountered, but no persistent_load.
Are there any techniques for 3D modeling?