R Programming Languare AUTHOR, Slides of Programming Languages

longer object length is not a multiple of shorter object length ... Error in which(x == 3) <- 5 : could not find function ”which<-”. > ...

Typology: Slides

2022/2023

Uploaded on 02/28/2023

blueeyes_11
blueeyes_11 🇺🇸

4.7

(18)

261 documents

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
R Programming Languare
AUTHOR
Dexin Ren,School of Earth and Space science of USTC
April 19, 2016
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download R Programming Languare AUTHOR and more Slides Programming Languages in PDF only on Docsity!

R Programming Languare

AUTHOR::: 任 德 馨

Dexin Ren,School of Earth and Space science of USTC

April 19, 2016

Contents

  • 1 基基基本本本操操操作作作
  • 2 基基基本本本数数数据据据结结结构构构的的的建建建立立立
    • 2.1 向量的建立
      • 2.1.1 向量具有较简单的规律 seq() or :
      • 2.1.2 向量具有较为复杂的规律 rep()
      • 2.1.3 向量没有什么规律 c()
      • 2.1.4 用户键盘输入元素 scan()
      • 2.1.5 不等长向量运算
      • 2.1.6 向量提取
      • 2.1.7 向量函数
    • 2.2 矩阵,matrix()
    • 2.3 数组,array()
    • 2.4 字符型向量的建立
    • 2.5 factor() 因子型向量
    • 2.6 列表,list()
    • 2.7 数据框(数据集),data.frame()
  • 3 从从从纯纯纯文文文本本本文文文件件件中中中读读读取取取数数数据据据,,,read.table() or read.csv()
  • 4 函函函数数数

x <− 1 : 1 0 x [ 1 ] 1 2 3 4 5 6 7 8 9 10

x <− 1:10 − 3 x [ 1 ] − 2 − 1 0 1 2 3 4 5 6 7

x = seq ( [ from = ] 1 , [ to = ] 5 , by = 0. 5 ) x [ 1 ] 1. 0 1. 5 2. 0 2. 5 3. 0 3. 5 4. 0 4. 5 5. 0

x = seq ( 1 , 1 0 , length = 11) x [ 1 ] 1. 0 1. 9 2. 8 3. 7 4. 6 5. 5 6. 4 7. 3 8. 2 9. 1 1 0. 0 2.1.2 向向向量量量具具具有有有较较较为为为复复复杂杂杂的的的规规规律律律 rep()

x = rep ( 2 : 5 , [ time = ] 2 ) x [ 1 ] 2 3 4 5 2 3 4 5

x = rep ( 2 : 5 , time = 3 , each = 2 ) x [ 1 ] 2 2 3 3 4 4 5 5 2 2 3 3 4 4 5 5 2 2 3 3 4 4 5 5

x = rep ( 3 : 5 , rep ( 2 : 4 ) ) x [ 1 ] 3 3 4 4 4 5 5 5 5 2.1.3 向向向量量量没没没有有有什什什么么么规规规律律律 c()

x = c ( 2 , 5 , 6 : 1 0 ) x [ 1 ] 2 5 6 7 8 9 10

x = rep ( c ( 3 , 5 ) , c ( 2 , 4 ) ) x [ 1 ] 3 3 5 5 5 5 2.1.4 用用用户户户键键键盘盘盘输输输入入入元元元素素素 scan()

当输入结束后,键入结束输入

a = scan ( ) 1 : 1 2 : 2 3 : 3 4 : <ENTER, u s e r input>

Read 3 i t e m s

a [ 1 ] 1 2 3

[ 1 ] 2 4 6 8 6 8 10 12 10 12

Warning message : In 1 : 1 0 + 1 : 4 : l o n g e r o b j e c t length i s not a m u l t i p l e o f s h o r t e r o b j e c t length

[ 1 ] 2 4 6 8 10 7 9 11 13 15

( x <− 1 : 1 0 ) [ 1 ] 1 2 3 4 5 6 7 8 9 10

( y <− x [ − ( 1 : 3 ) ] ) [ 1 ] 4 5 6 7 8 9 10

( z <− x[−c ( 2 , 5 , 8 ) ] ) [ 1 ] 1 3 4 6 7 9 10

x [ x > 3 ] [ 1 ] 4 5 6 7 8 9 10

x > 3 [ 1 ] FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE TRUE TRUE

x [ x > 5 ] <− 10 x [ 1 ] 1 2 3 4 5 10 10 10 10 10

which ( x == 3) [ 1 ] 3

which ( x == 3) <− 5 E r r o r i n which ( x == 3) <− 5 : c o u l d not find function ” which<−”

x [ which ( x == 3) ] <− 5 x [ 1 ] 1 2 5 4 5 10 10 10 10 10

matrix ( data = 1 : 9 , nrow = 3 , byrow = TRUE) [ , 1 ] [ , 2 ] [ , 3 ] [ 1 , ] 1 2 3 [ 2 , ] 4 5 6 [ 3 , ] 7 8 9

matrix ( data = 1 : 9 , nrow = 3 , byrow = F a l s e ) E r r o r i n matrix ( data = 1 : 9 , nrow = 3 , byrow = F a l s e ) : o b j e c t ’ F a l s e ’ not found

rowName <− paste ( ’ row ’ , 1 : 3 , sep = ’ ’ ) colName <− paste ( ’ c o l ’ , 1 : 3 , sep = ’ ’ )

(m1 <− matrix ( data = 1 : 9 , nrow = 3 , byrow = TRUE, dimnames = l i s t ( rowName , colName ) ) ) c o l 1 c o l 2 c o l 3 row1 1 2 3 row2 4 5 6 row3 7 8 9 m1 <− matrix ( data = 1 : 9 , nrow = 3 , byrow = TRUE, dimnames = l i s t ( rowName , colName ) ) m c o l 1 c o l 2 c o l 3 row1 1 2 3

row2 4 5 6 row3 7 8 9

m1 [ 1 , ] c o l 1 c o l 2 c o l 3 1 2 3

m1 [ 1 , 2 ] [ 1 ] 2

m1 [ 1 , 2 : 3 ] c o l 2 c o l 3 2 3

m1 [ 7 ] [ 1 ] 3

diag ( 3 )

[ , 1 ] [ , 2 ] [ , 3 ]

[ 1 , ] 1 0 0

[ 2 , ] 0 1 0

[ 3 , ] 0 0 1

diag ( c ( 1 , 2 , 3 ) ) [ , 1 ] [ , 2 ] [ , 3 ] [ 1 , ] 1 0 0 [ 2 , ] 0 2 0 [ 3 , ] 0 0 3

diag ( 2 , nc = 3 , nr = 5) [ , 1 ] [ , 2 ] [ , 3 ] [ 1 , ] 2 0 0 [ 2 , ] 0 2 0 [ 3 , ] 0 0 2 [ 4 , ] 0 0 0 [ 5 , ] 0 0 0

matrix ( 1 , nr = 2 , nc = 3) [ , 1 ] [ , 2 ] [ , 3 ] [ 1 , ] 1 1 1 [ 2 , ] 1 1 1

x = matrix ( 1 : 9 , 3 )

x [ 2 , ] [ 1 ] 2 5 8

x [ − 2 , ] [ , 1 ] [ , 2 ] [ , 3 ] [ 1 , ] 1 4 7 [ 2 , ] 3 6 9

x [ , 2 , drop = TRUE] #:降低维度drop [ 1 ] 4 5 6

x [ , 2 , drop = FALSE ] [ , 1 ] [ 1 , ] 4 [ 2 , ] 5 [ 3 , ] 6

x [ 2 , , drop = FALSE ] [ , 1 ] [ , 2 ] [ , 3 ]

paste()函函函数数数

z = c ( ’ x ’ , ’ y ’ , ’ z ’ ) z [ 1 ] ”x” ”y” ” z ”

paste ( z , 1 : 5 ) [ 1 ] ”x 1” ”y 2” ” z 3” ”x 4” ”y 5”

paste ( z , 1 : 5 , sep = ’ ’ ) [ 1 ] ” x1 ” ” y2 ” ” z3 ” ” x4 ” ” y5 ”

#s e p = s t r i n g 设置字符串连接中介

paste ( z , 1 : 5 , sep = ’− ’ ) [ 1 ] ”x−1” ”y−2” ”z−3” ”x−4” ”y−5”

2.5 factor() 因因因子子子型型型向向向量量量

x = c ( ’ a ’ , ’ b ’ , ’ a ’ , ’ c ’ , ’ d ’ , ’ b ’ ) x [ 1 ] ”a” ”b” ”a” ” c ” ”d” ”b”

f x = factor ( x ) f x [ 1 ] a b a c d b L e v e l s : a b c d

c l a s s ( x ) [ 1 ] ” c h a r a c t e r ”

c l a s s ( f x ) [ 1 ] ” f a c t o r ”

storage .mode( f x ) #存储模式 [ 1 ] ” i n t e g e r ”

as. numeric ( f x ) [ 1 ] 1 2 1 3 4 2

l e v e l s ( f x ) [ 1 ] ”a” ”b” ” c ” ”d”

2.6 列列列表表表,,,list()

list格格格式式式

list(x1, x2, x3, ...)

list是一个强大的容器,可以在其中放入各种类型的变量,比如数字、字符、向量、 矩阵、数组及数据框,甚至的列表本身。

list元素访问同样使用方括号[ ]。

2.7 数数数据据据框框框(((数数数据据据集集集))),,,data.frame()

name <− c ( ’ Jane ’ , ’ J u s t i n ’ , ’Tom ’ , ’ J e l l y ’ )

age <− c ( 1 0 , 1 1 , 1 3 , 1 1 )

gender <− c ( ’F ’ , ’M’ , ’M’ , ’M’ )

s t u d e n t <− data. frame ( name= name , age= age , gender= gender )

s t u d e n t name age gender 1 Jane 10 F 2 J u s t i n 11 M 3 Tom 13 M 4 J e l l y 11 M

s t u d e n t [ 1 ] name 1 Jane 2 J u s t i n 3 Tom 4 J e l l y

s t u d e n t [ 1 , ] name age gender 1 Jane 10 F

s t u d e n t [ 1 , 3 ] [ 1 ] F L e v e l s : F M

s t u d e n t $name

switch (EXPR,... )

for ( var i n seq ) expr

while ( cond ) expr

repeat expr break ;

#一个例子

c e n t r e <− function ( x , type ) { switch ( type , mean = mean( x ) , median = median( x ) , trimmed = mean( x , trim =. 1 ) ) }

x <− rnorm ( 1 0 )

c e n t r e ( x , ’ mean ’ ) [ 1 ] −0.

c e n t r e ( x , ’ median ’ )

[ 1 ] 0. 0 1 7 1 2 9 5 6

c e n t r e ( x , ’ trimmed ’ ) [ 1 ] −0.

#myfun ( ) 参数具有初始值,则函数调用时候可以不用赋值 ,

myfun = function ( x = 1 , y = 2 , z = 3 ) {

print ( ”HELLO” )

p = x + y + z

cat ( ”Sum i s ” , ’ \n ’ , p , ’ \n ’ )

#调用函数Console

source ( ’C: / U se rs /Rdx/Desktop/myfun .R ’ )

myfun ( y = 10) [ 1 ] ”HELLO” Sum i s 14

myfun ( 1 0 ) [ 1 ] ”HELLO” Sum i s 15

myfun ( 1 0 , 1 0 , 1 0 ) [ 1 ] ”HELLO” Sum i s 30

因因因R语语语言言言函函函数数数只只只能能能返返返回回回一一一个个个值值值,,,对对对于于于有有有多多多个个个值值值需需需要要要返返返回回回可可可以以以采采采用用用:::

output <− l i s t ( x = x , y = y )

return output

#调用Console

out <− myfun ( x = 1 , y = 2)

x1 <− out$x

y1 <− out$y