๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Programming/R

๊ธฐ๋ณธ ํ•จ์ˆ˜ ์‚ฌ์šฉ๋ฒ•

R์˜ ๋‚ด์žฅ๋œ ๊ธฐ๋ณธ ํ•จ์ˆ˜์˜ ์‚ฌ์šฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž.

 

โ–ก if ( ) { } else if ( ) { } else { }

 

In:

char = 'A'

if (char == 'B') {
  print('if')
} else if (char == 'C') {
  print('else if')
} else {
  print('else')
}

 

Out:

[1] "else"

 

โ–ก for ( ) { }

 

In:

for (i in 1:5) {
  print(i)
}

 

Out:

[1] 1
[1] 2
[1] 3
[1] 4
[1] 5

 

โ–ก ifelse

 

In:

char = 'A'

ifelse(char == 'A', 'char is A', 'char is not A')

 

Out:

[1] "char is A"

 

โ–ท ifelse ํ•จ์ˆ˜์˜ ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋Š” ์กฐ๊ฑด์„ ์จ์ง€ํ•˜๊ณ , ๋‘ ๋ฒˆ์งธ ์ธ์ž๋Š” ์กฐ๊ฑด์ด ์ฐธ์ผ ๊ฒฝ์šฐ์˜ ์ถœ๋ ฅ๊ฐ’์ด๊ณ , ์„ธ ๋ฒˆ์งธ ์ธ์ž๋Š” ์กฐ๊ฑด์ด ๊ฑฐ์ง“์ผ ๊ฒฝ์šฐ์˜ ์ถœ๋ ฅ๊ฐ’์„ ์˜๋ฏธํ•œ๋‹ค.

 

โ–ก which, which.max, which.min

 

In:

x = 1:10

which(x == 5)

which.max(x)

which.min(x)

 

Out:

[1] 5

[1] 10

[1] 1

 

โ–ท which ํ•จ์ˆ˜์˜ ์ธ์ž์— ์กฐ๊ฑด์„ ์ฃผ๊ณ , ์ด ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๊ฐ’์˜ ์œ„์น˜๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

 

โ–ท which.max ํ•จ์ˆ˜๋Š” ํ•ด๋‹น ๋ฒกํ„ฐ์˜ ์ตœ๋Œ€๊ฐ’์ด ํฌํ•จ๋œ ์œ„์น˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. which.min ํ•จ์ˆ˜๋Š” ์ตœ์†Œ๊ฐ’์ด ํฌํ•จ๋œ ์œ„์น˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

โ–ก order, sort

 

In:

y = sample(10:20)

print(y)

order(x)

sort(x)

 

Out:

 [1] 14 11 18 10 16 17 15 13 12 19 20

 [1]  4  2  9  8  1  7  5  6  3 10 11

 [1] 10 11 12 13 14 15 16 17 18 19 20

 

โ–ท order ํ•จ์ˆ˜๋Š” ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ๋‚˜์—ดํ–ˆ์„ ๋•Œ์˜ ๋ฒกํ„ฐ์˜ ์œ„์น˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. sort ํ•จ์ˆ˜๋Š” ์ •๋ ฌ๋œ ํ›„์˜ ๋ฒกํ„ฐ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

โ–ก subset

 

In:

df = data.frame(a = c('A', 'B', 'C', 'A'), 
                b = c('C', 'B', 'A', 'C'))

print(df)

subset(df, a == 'B' & b == 'B')

 

Out:

  a b
1 A C
2 B B
3 C A
4 A C

  a b
2 B B

 

โ–ท subset ํ•จ์ˆ˜๋Š” ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„์˜ ์ผ๋ถ€๋ฅผ ์ถ”์ถœํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋กœ ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„์— ๋Œ€ํ•ด ๋‘ ๋ฒˆ์งธ ์ธ์ž๋กœ ์ฃผ์–ด์ง„ ์กฐ๊ฑด๋งŒ ๋งŒ์กฑํ•˜๋Š” ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„์„ ์ถ”์ถœํ•œ๋‹ค.

 

โ–ก rbind

 

In:

df_1 = data.frame(a = c('A', 'B', 'C', 'A'), 
                  b = c('C', 'B', 'A', 'C'))

df_2 = data.frame(a = c('C', 'F', 'K', 'B'), 
                  b = c('D', 'E', 'M', 'D'))

df_1 %>% 
  rbind(df_2)

 

Out:

  a b
1 A C
2 B B
3 C A
4 A C
5 C D
6 F E
7 K M
8 B D

 

โ–ท rbind ํ•จ์ˆ˜๋Š” ๋‘ ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„์„ ํ–‰์„ ๊ธฐ์ค€์œผ๋กœ ์ด์–ด์ฃผ๋Š” ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

 

โ–ก cbind

 

In:

df_3 = data.frame(a = c('A', 'B', 'C', 'A'), 
                  b = c('C', 'B', 'A', 'C'))

df_4 = data.frame(c = c('C', 'F', 'K', 'B'), 
                  d = c('D', 'E', 'M', 'D'))

df_3 %>% 
  cbind(df_4)

 

Out:

  a b c d
1 A C C D
2 B B F E
3 C A K M
4 A C B D

 

โ–ท cbind ํ•จ์ˆ˜๋Š” ๋‘ ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„์„ ์—ด์„ ๊ธฐ์ค€์œผ๋กœ ์ด์–ด์ฃผ๋Š” ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

 

โ–ก rep

 

In:

rep(1, 10)

 

Out:

[1] 1 1 1 1 1 1 1 1 1 1

 

โ–ท rep ํ•จ์ˆ˜๋Š” ์ฒซ ๋ฒˆ์งธ ์ธ์ž์˜ ๊ฐ’์„ ๋‘ ๋ฒˆ์งธ ์ธ์ž๋งŒํผ์„ ๋ฐ˜๋ณตํ•˜์—ฌ ๋ฒกํ„ฐ๋ฅผ ๋งŒ๋“ ๋‹ค.

 

โ–ก seq

 

In:

seq(1, 100)

 

Out:

  [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16
 [17]  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32
 [33]  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48
 [49]  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64
 [65]  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80
 [81]  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96
 [97]  97  98  99 100

 

โ–ท seq ํ•จ์ˆ˜๋Š” ์ฒซ ๋ฒˆ์งธ ์ธ์ž์˜ ๊ฐ’๋ถ€ํ„ฐ ๋‘ ๋ฒˆ์งธ ์ธ์ž์˜ ๊ฐ’๊นŒ์ง€ ๊ธฐ๋ณธ ๋‹จ์œ„๋กœ ๋ฒกํ„ฐ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. length.out, by ์ธ์ž๋ฅผ ์ด์šฉํ•˜์—ฌ ๋‹ค์–‘ํ•œ ํ˜•ํƒœ์˜ ๋ฒกํ„ฐ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

 

โ–ก floor, ceiling, round

 

In:

floor(10.6)

ceiling(10.6)

round(10.6)

 

Out:

[1] 10

[1] 11

[1] 11

 

โ–ท floor, ceiling, round ํ•จ์ˆ˜๋Š” ๋ง ๊ทธ๋Œ€๋กœ ์˜ฌ๋ฆผ, ๋‚ด๋ฆผ, ๋ฐ˜์˜ฌ๋ฆผ ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.