当前位置:网站首页>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
> ```
边栏推荐
- 负数取余问题
- Microsoft Word 教程,如何在 Word 中插入页码、目录?
- Are you confused about choosing a low code platform? Follow these three steps
- 7:00 tonight | application of PhD debate self supervised learning in Recommendation System
- Test prerequisites: recommend a special cross platform app performance test tool!
- asp. Net using JSON to interact with API data
- Module 8 fonctionnement
- system()
- Demand and business model innovation - demand 3- demand engineering process
- EASYCODE one click plug-in custom template
猜你喜欢

系统 日志

测试必备:推荐一款跨平台App性能专项测试工具!

Deep feature synthesis and genetic feature generation, comparison of two automatic feature generation strategies

Shell arrays and functions

基于微信电子书阅读小程序毕业设计毕设作品(2)小程序功能

Implementation of exec function and shell

Process creation fork (), demise wait()

硬件测试之—纹波测试为什么不要使用接地夹子

“即服务”,未来已来,始于现在 | IT消费新模式,FOD按需计费

什么是数据驱动
随机推荐
编程工具下载地址
Leetcode topic [string]-344- reverse string
asp. Net using JSON to interact with API data
EASYCODE one click plug-in custom template
今晚7:00 | PhD Debate 自监督学习在推荐系统中的应用
新来的同事问我 where 1=1 是什么意思???
Pyinstaller packaging tutorial packaging resource files
VC Hacon Joint Programming genimage3extern writeimage
typescript的装饰器(Decorotor)基本使用
Experience Technology Department of ant group launched the 2023rd school recruitment
Wangxuegang room+paging3
进程会计、进程时间、守护进程
Reading small programs based on wechat e-book graduation design works (7) Interim inspection report
Wechat e-book reading applet graduation design work (6) opening defense ppt
【GAMES101】课堂笔记8–着色(着色频率、图形管线、纹理映射)
Microsoft Word 教程,如何在 Word 中插入页码、目录?
Axure RP 9 for Mac(交互式产品原型设计工具)中文版
Are you confused about choosing a low code platform? Follow these three steps
Unsupported class file major version 60
Unsupported class file major version 60