当前位置:网站首页>4 R basic exercises
4 R basic exercises
2022-06-12 19:49:00 【THE ORDER】
> print('hello') # Initial programming
[1] "hello"
> getwd()# Get work path
[1] "C:/RStudio/project"
> a=c(1,99,Inf,-3.58,-Inf)
> a#a vector
[1] 1.00 99.00 Inf -3.58 -Inf
> b=c(-1L,-9L,1L,25L) #b vector
> b
[1] -1 -9 1 25
> class(b)#b type
[1] "integer"
> c=(-100):0#c vector
> c
[1] -100 -99 -98 -97 -96 -95 -94 -93 -92 -91 -90 -89 -88 -87 -86 -85 -84 -83 -82 -81 -80 -79 -78 -77 -76 -75
[27] -74 -73 -72 -71 -70 -69 -68 -67 -66 -65 -64 -63 -62 -61 -60 -59 -58 -57 -56 -55 -54 -53 -52 -51 -50 -49
[53] -48 -47 -46 -45 -44 -43 -42 -41 -40 -39 -38 -37 -36 -35 -34 -33 -32 -31 -30 -29 -28 -27 -26 -25 -24 -23
[79] -22 -21 -20 -19 -18 -17 -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0
> complex(15)# The plural
[1] 0+0i 0+0i 0+0i 0+0i 0+0i 0+0i 0+0i 0+0i 0+0i 0+0i 0+0i 0+0i 0+0i 0+0i 0+0i
> d=as.character(complex(15))# Coercive transformation
> d
[1] "0+0i" "0+0i" "0+0i" "0+0i" "0+0i" "0+0i" "0+0i" "0+0i" "0+0i" "0+0i" "0+0i" "0+0i" "0+0i" "0+0i" "0+0i"
> class(d)
[1] "character"
> list(a=1:3,b=(T:F),c("a","b"))# list
$a
[1] 1 2 3
$b
[1] 1 0
[[3]]
[1] "a" "b"
> list(a=1:3,b=list(b1=2:4,b2="a"))# List nesting
$a
[1] 1 2 3
$b
$b$b1
[1] 2 3 4
$b$b2
[1] "a"
> args(matrix)# Describe functions
function (data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL)
NULL
> matrix(1:6,2,3)# Matrix default column arrangement
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
> matrix(1:6,2,3,byrow = T,dimnames=list(c("x1","x2"),c("a","b","c")))# Name the matrix
a b c
x1 1 2 3
x2 4 5 6
> matrix(1,2,3)
[,1] [,2] [,3]
[1,] 1 1 1
[2,] 1 1 1
> matrix(NA,2,3)# Fill the matrix
[,1] [,2] [,3]
[1,] NA NA NA
[2,] NA NA NA
> x=matrix(NA,2,3)# Matrix assignment
> dim(x)
[1] 2 3
> z=1:6
> as.matrix(z)
[,1]
[1,] 1
[2,] 2
[3,] 3
[4,] 4
[5,] 5
[6,] 6
> as.numeric(z)
[1] 1 2 3 4 5 6
> as.matrix(z)# Vector to matrix
[,1]
[1,] 1
[2,] 2
[3,] 3
[4,] 4
[5,] 5
[6,] 6
> as.numeric(z)# Matrix to value
[1] 1 2 3 4 5 6
> y=7:12
> rbind(z,y)# Row merge vector
[,1] [,2] [,3] [,4] [,5] [,6]
z 1 2 3 4 5 6
y 7 8 9 10 11 12
> cbind(z,y)# The column merge vector is a matrix
z y
[1,] 1 7
[2,] 2 8
[3,] 3 9
[4,] 4 10
[5,] 5 11
[6,] 6 12
> args(array)
function (data = NA, dim = length(data), dimnames = NULL)
NULL
> array(1:12,dim=c(2,3,2))# Create a list of
, , 1
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
, , 2
[,1] [,2] [,3]
[1,] 7 9 11
[2,] 8 10 12
> array(1:12,dim=c(2,3,2),dimnames=list(c("x1","x2"),c("x3","x4","x5"),c("x6","x7")))
, , x6
x3 x4 x5
x1 1 3 5
x2 2 4 6
, , x7
x3 x4 x5
x1 7 9 11
x2 8 10 12
> x=data.frame(id=1:3,age=c(12,13,12),name=c("li","han","lao"))# Data frame
> x
id age name
1 1 12 li
2 2 13 han
3 3 12 lao
> colnames(x) # Data frame column name
[1] "id" "age" "name"
> nrow(x)# Row name
[1] 3
> nrow(x)# Row number
[1] 3
> ncol(x)# Number of columns
[1] 3
> l=list(a=c(1,3,2),b=c(F,T,F))# list
> l
$a
[1] 1 3 2
$b
[1] FALSE TRUE FALSE
> a=matrix(1:8,2,4)# matrix
> rbind(1:4,5:8)# Matrix merging
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
> b=array(1:8,dim = c(2,2,2),dimnames = list(1:2,3:4,5:6))# Create array
> b
, , 5
3 4
1 1 3
2 2 4
, , 6
3 4
1 5 7
2 6 8
> c=data.frame(ID=1:3,math=c(80,78,90))# Create list box
> list(list=l,Matrix=a,Array=b,Datafram=c)# Compound list
$list
$list$a
[1] 1 3 2
$list$b
[1] FALSE TRUE FALSE
$Matrix
[,1] [,2] [,3] [,4]
[1,] 1 3 5 7
[2,] 2 4 6 8
$Array
, , 5
3 4
1 1 3
2 2 4
, , 6
3 4
1 5 7
2 6 8
$Datafram
ID math
1 1 80
2 2 78
3 3 90
> factor(c("y","y","n","n"))# Categorical data factor
[1] y y n n
Levels: n y
> factor(c(" Primary school "," Junior high school "," high school "," Primary school "),order=T,levels=c(" Primary school "," Junior high school "," high school "))# Factor sorting
[1] Primary school Junior high school high school Primary school
Levels: Primary school < Junior high school < high school
> args(gl)
function (n, k, length = n * k, labels = seq_len(n), ordered = FALSE)
NULL
> gl(3,4)
[1] 1 1 1 1 2 2 2 2 3 3 3 3
Levels: 1 2 3
> gl(3,4)#1-3, loop 4 Time
[1] 1 1 1 1 2 2 2 2 3 3 3 3
Levels: 1 2 3
> gl(2,3,12,labels=c(" male "," Woman "),order=T)# label
[1] male male male Woman Woman Woman male male male Woman Woman Woman
Levels: male < Woman
> x=c(1,2,NA,NaN,3)
> is.numeric(x)
[1] TRUE
> is.na(x)
[1] FALSE FALSE TRUE TRUE FALSE
> is.nan(x)#NA Include NAN,NAN yes NA True subset
[1] FALSE FALSE FALSE TRUE FALSE
> x=1:3
> names(x)
NULL
> names(x)=c("a","b","c")# Vector naming
> x
a b c
1 2 3
> x=list(1:3,c(T,F))
> x
[[1]]
[1] 1 2 3
[[2]]
[1] TRUE FALSE
> names(x)=c("aa","bb")
> x
$aa
[1] 1 2 3
$bb
[1] TRUE FALSE
> names(x)=c("cc","dd")# List naming
> x
$cc
[1] 1 2 3
$dd
[1] TRUE FALSE
> x=matrix(NA,2,3)
> x
[,1] [,2] [,3]
[1,] NA NA NA
[2,] NA NA NA
> dimnames(x)=list(c("a","b"),c("x","y","z"))# Matrix naming
> x
x y z
a NA NA NA
b NA NA NA
> x=data.frame(id=1:3,math=89:91)
> x
id math
1 1 89
2 2 90
3 3 91
> rownames(x)=c("x1","x2","x3")# Data frame row name
> x
id math
x1 1 89
x2 2 90
x3 3 91
> colnames(x)=c("ID","MATH")# Data frame column name
> x
ID MATH
x1 1 89
x2 2 90
x3 3 91
> names(x)
[1] "ID" "MATH"
> names(x)# Name
[1] "ID" "MATH"
> x=factor(c(" Primary school "," Junior high school "," high school "," Primary school "," high school "),order=T,levels=c(" Primary school "," Junior high school "," high school "))
> x
[1] Primary school Junior high school high school Primary school high school
Levels: Primary school < Junior high school < high school
> gl(3,3,90,labels = c(" Primary school "," Junior high school "," high school "),order=T)
[1] Primary school Primary school Primary school Junior high school Junior high school Junior high school high school high school high school Primary school Primary school Primary school Junior high school Junior high school Junior high school high school high school high school Primary school Primary school Primary school Junior high school Junior high school Junior high school high school high school
[27] high school Primary school Primary school Primary school Junior high school Junior high school Junior high school high school high school high school Primary school Primary school Primary school Junior high school Junior high school Junior high school high school high school high school Primary school Primary school Primary school Junior high school Junior high school Junior high school high school
[53] high school high school Primary school Primary school Primary school Junior high school Junior high school Junior high school high school high school high school Primary school Primary school Primary school Junior high school Junior high school Junior high school high school high school high school Primary school Primary school Primary school Junior high school Junior high school Junior high school
[79] high school high school high school Primary school Primary school Primary school Junior high school Junior high school Junior high school high school high school high school
Levels: Primary school < Junior high school < high school
> z=c(0:3)
> z
[1] 0 1 2 3
> names(z)=c("a","b","c","d")
> z
a b c d
0 1 2 3
> z=matrix(NA,2,2)
> z
[,1] [,2]
[1,] NA NA
[2,] NA NA
> dimnames(z)=list(c("a","b"),c("c","d"))
> z
c d
a NA NA
b NA NA
> x=data.frame(x=1:3,y=2:4)
> x
x y
1 1 2
2 2 3
3 3 4
> colnames(x)=c("y","z")
> x
y z
1 1 2
2 2 3
3 3 4
> rownames(x)=c("a","b","c")
> x
y z
a 1 2
b 2 3
c 3 4
> x=data.frame(id=1:3,math=89:91)
> x
id math
1 1 89
2 2 90
3 3 91
> fix(x)# It's not used in general fix Modify the data frame , Poor reusability
> ```
边栏推荐
- Torch network model is converted to onnx format and visualized
- How mysterious is "PIP not an internal or external command, nor a runnable program or batch file"
- What is data driven
- Overview of object detection
- 运算器的基本结构
- 5g R17 standard is frozen. What does it say?
- How to make a computer installation and startup USB flash disk
- 硬件测试之—纹波测试为什么不要使用接地夹子
- Storage system overview
- 选电子工程被劝退,真的没前景了?
猜你喜欢

"As a service", the future has come, starting from the present | new mode of it consumption, FOD billing on demand

系统 日志

Pyinstaller packaging tutorial packaging resource files

Demand and business model analysis-1-business model canvas

基于微信电子书阅读小程序毕业设计毕设作品(7)中期检查报告

运算器的基本结构

The component style set by uniapp takes effect in H5 and app, but does not take effect in wechat applet. The problem is solved

在 Traefik Proxy 2.5 中使用/开发私有插件(Traefik 官方博客)

Reading small program based on wechat e-book graduation design (4) opening report

Download and configuration of nuitka packaging tutorial
随机推荐
Mode of most elements (map, sort, random, Boyer Moore voting method)
Are you confused about choosing a low code platform? Follow these three steps
华尔街备忘单(Wall Street Cheat Sheet)
Interpreter Files
Process creation fork (), demise wait()
负数取余问题
从16页PPT里看懂Jack Dorsey的Web5
Torch network model is converted to onnx format and visualized
Demand and business model analysis -6- five topics
基于微信电子书阅读小程序毕业设计毕设作品(3)后台功能
How mysterious is "PIP not an internal or external command, nor a runnable program or batch file"
用户权限和组权限
How to close icloud when Apple ID of Apple mobile phone forgets password and frequently jumps out to log in
进程会计、进程时间、守护进程
Negative remainder problem
[observation] Huawei's next generation data center "adds momentum" to Guangxi's low-carbon and high-quality development
基於分布式數據庫本身的定時備份方法
模块八作业
Jenkins中pipeline对接CMDB接口获取主机列表的发布实践原创
【观察】华为下一代数据中心,为广西低碳高质量发展“添动能”