iT邦幫忙

0

R語言個人小筆記

快期末考了在這邊做個筆記方便我觀看哈哈

  • Data Component
  • Data Frame & Logic Function
  • Advanced Math Operation
  • Logic Package
  • Advanced Logic Function

Data Component

用來分類資料
語法為:

factor()

x<-c("yes","no","yes","no","no","yes")
factor(x)
#Levels: no yes
y<-factor(x,levels = c("yes","no","middle"))
y
#Levels: yes no middle
z<-factor(x,levels = c("yes","no","middle"),labels = c("y","n","m"))
z
#Levels: y n m
//labels用來幫物件取名

Factor Function

factor()
levels()
nlevels()
length()
ordered()

x<-c("yes","no","yes","no","no","yes")
x<-factor(x,levels=c("yes","no","middle"),labels = c("y","n","m"))
factor(x)
levels(x)
nlevels(x)
length(x)
ordered(x)

> factor(x)
[1] y n y n n y
Levels: y n
> levels(x)
[1] "y" "n" "m"
> nlevels(x)
[1] 3
> length(x)
[1] 6
> ordered(x) //按照levels裡的排序
[1] y n y n n y
Levels: y < n

第二種語法:

Table() 分類並且計算數量

x<-c("d","a","e","c","d","b","c","d","e","f")
table(x)
Output:
a b c d e f 
1 1 2 3 2 1 

Data Frame & Logic Function

Vector、Matrix、Array 物件資料型態相同
Data Frame 可以有彈性的資料型態

x<-1:12
y<-c("a","b","c","d","e","f","g","h","i","j","k","l")
k<-rbind(x,y)
k
is.character(k)
is.numeric(k)
data.frame(x,y)

>k //使用rbind資料型態全變為字串
  [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
x "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10"  "11"  "12" 
y "a"  "b"  "c"  "d"  "e"  "f"  "g"  "h"  "i"  "j"   "k"   "l"  
> is.character(k)
[1] TRUE
> is.numeric(k)
[1] FALSE
> data.frame(x,y)
    x y
1   1 a
2   2 b
3   3 c
4   4 d
5   5 e
6   6 f
7   7 g
8   8 h
9   9 i
10 10 j
11 11 k
12 12 l

which為邏輯函數
例子:

//如有一段程式碼
x.info<-data.frame(Name=c("Tony","Mary","Joe","Jessica"),Gender=c("Male","Female","Male","Female"),Score=c(81,92,76,53),stringsAsFactors=FALSE)
//如何使用which輸出成以下
 Name Gender Score Result
1    Tony   Male    81   pass
2    Mary Female    92   pass
3     Joe   Male    76   pass
4 Jessica Female    53   fail

Answer:
Result<-c(91,92,76,53)
Result[which(Result>=60)]<-"pass"
Result[which(Result<60)]<-"fail"
x.info<-cbind(x.info,Result)
x.info

Advanced Math Operation

List是一種特別的物件集合,它的元素也由序號(下標)區分,但是各元素的型別可 以是任意物件,不同元素不必是同一型別。

y<-list(Name=c("Tony","Mary","Joe"),Gender=c("Male","Female","Male","Female"),Score=c(81,92,76,53))

//如何呈現所有Name
y[1] or y[[1]]
//如何呈現所有Score
y[3] or y[[3]]
//如何呈現Mary's Score
y[[3]][2]
//如何呈現Mary's Data
y[[1]][2] y[[2]][2] y[[3]][2]

結合兩個Data Frame

x<-data.frame(Name=c("Tony","Mary","Joe","Jessic"),Gender=c("Male","Female","Male","Female"),Score=c(81,92,76,53),stringsAsFactors=FALSE)
y<-data.frame(Name=c("Pete","Lucy","Dora","Kevin"),Gender=c("Male","Female","Female","Male"),Score=c(86,72,96,83),stringsAsFactors=FALSE)

x1<-list(x,y)
x1

output:
[[1]]
    Name Gender Score
1   Tony   Male    81
2   Mary Female    92
3    Joe   Male    76
4 Jessic Female    53

[[2]]
   Name Gender Score
1  Pete   Male    86
2  Lucy Female    72
3  Dora Female    96
4 Kevin   Male    83

可用 $ 符號呼叫物件

y<-list(Name=c("Tony","Mary","Joe"),Gender=c("Male","Female","Male","Female"),Score=c(81,92,76,53))

> y$Name
[1] "Tony" "Mary" "Joe" 

呼叫多個物件

> y[2:3]
$Gender
[1] "Male"   "Female" "Male"   "Female"

$Score
[1] 81 92 76 53

取代物件

y[2:3]<-list(c("Male","Female","Female","Male"),c(41,22,56,63))
y[2:3]

> y[2:3]
$Gender
[1] "Male"   "Female" "Female" "Male"  

$Score
[1] 41 22 56 63

新增新物件

y[["Height"]]<-c(170,167,172,183)

Logic Package

將一個字串分割成多個字串,可以使用 strsplit 函數,第一個參數是輸入的字串,而第二個參數則是分隔字串:
strsplit()

x<-c("Hello R World","Hi")
strsplit(x,"")

output:
[[1]]
 [1] "H" "e" "l" "l" "o" " " "R" " " "W" "o" "r" "l" "d"

[[2]]
[1] "H" "i"

轉大寫
toupper()

轉小寫
tolower()

除去重複物件
unique()

z<-c(3,5,7,9,2,4,6,13,5,7,9)
unique(z)

output:
[1]  3  5  7  9  2  4  6 13

將多個字串連接:
paste()

a<-c("Distance","are","normally","used","to","measure","the","similarity")
paste(a)
paste(a,collapse = "")
paste(a,collapse = " ")

output:
> paste(a)
[1] "Distance"   "are"        "normally"   "used"      
[5] "to"         "measure"    "the"        "similarity"
> paste(a,collapse = "")
[1] "Distancearenormallyusedtomeasurethesimilarity"
> paste(a,collapse = " ")
[1] "Distance are normally used to measure the similarity"

結合兩向量:

str1<-letters[1:5]
str2<-1:5
paste(str1,str2)

output:
[1] "a 1" "b 2" "c 3" "d 4" "e 5"

個別參數分隔(R 預設用空白相隔):

str<-letters[1:5]
paste("A",str)
paste("A",str,sep="")

output:
> paste("A",str)
[1] "A a" "A b" "A c" "A d" "A e"
> paste("A",str,sep="")
[1] "Aa" "Ab" "Ac" "Ad" "Ae"

擷取字串:
substr()

x<-c("Hello R World")
substr(x,start = 4,stop = 9)

output:
[1] "lo R W"

資料擷取:
grep()

x<-c("Hello","R","World")
grep("l",x)
x[grep("l",x)]

output:
> grep("l",x)
[1] 1 3
> x[grep("l",x)]
[1] "Hello" "World"

Advanced Logic Function

新增函數

x<-function(num){
  y<-num+1
  return(y)
}
x(5)

output:
[1] 6

輸入陣列

x<-function(v){
  x.percent<-round(v*100,digits = 2)
  x.final<-paste(x.percent,sep = "","%")
  return(x.final)
}
y<-c(0.8932,0.2345,0.07641,0.77351)
x(y)

output:
[1] "89.32%" "23.45%" "7.64%"  "77.35%"

區域變數與全域變數

x1<-3
x<-function(n){
  x1<-n+3
  return(x1)
}
x(3) //結果為6
z<-x1+2 //結果為5

搭配switch

x<-function(n){
  if(n>5) index<-1
  if(n<=5) index<-2
  switch(index,a<-"大於5",a<-"小於5")
  return(a)
}
x(6)
x(4)

output:
> x(6)
[1] "大於5"
> x(4)
[1] "小於5"

For迴圈

for(n in 1:10){
  print(n)
}
x<-c("hello","R","world")
for (n in x) {
  print(n)
}

output:
> for(n in 1:10){
 print(n)
 }
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10
> x<-c("hello","R","world")
> for (n in x) {
   print(n)
 }
[1] "hello"
[1] "R"
[1] "world"

//找出0~20的偶數
x<-function(n){
  re<-c()
  for(i in n){
    if(i%%2==0){
      re<-c(re,i)
    }
  }
  return(re)
}
x(0:20)

尚未有邦友留言

立即登入留言