paste(letters[1:5], as.character(1:5))
[1] "a 1" "b 2" "c 3" "d 4" "e 5"
> paste(letters[1:5], as.character(1:3))
[1] "a 1" "b 2" "c 3" "d 1" "e 2"
> expand.grid(pants = c("blue", "black"), shirt = c("white", "grey", "plaid")) # 모든 조합 생성?
  pants shirt
1  blue white
2 black white
3  blue  grey
4 black  grey
5  blue plaid
6 black plaid



> install.packages("gtools")
> library(gtools)
> permutations(5,2)
      [,1] [,2]
 [1,]    1    2
 [2,]    1    3
 [3,]    1    4
 [4,]    1    5
 [5,]    2    1
 [6,]    2    3
 [7,]    2    4
 [8,]    2    5
 [9,]    3    1
[10,]    3    2
[11,]    3    4
[12,]    3    5
[13,]    4    1
[14,]    4    2
[15,]    4    3
[16,]    4    5
[17,]    5    1
[18,]    5    2
[19,]    5    3
[20,]    5    4
> combinations(5,2)
      [,1] [,2]
 [1,]    1    2
 [2,]    1    3
 [3,]    1    4
 [4,]    1    5
 [5,]    2    3
 [6,]    2    4
 [7,]    2    5
 [8,]    3    4
 [9,]    3    5
[10,]    4    5

> all_phone_numbers <- permutations(10, 7, v = 0:9)
> n <- nrow(all_phone_numbers)
> n
[1] 604800
> index <- sample(n, 5)
> index
[1] 124574 106782 415511 232305 465598
> all_phone_numbers[index,]
     [,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,]    2    0    6    4    1    8    5
[2,]    1    7    9    0    8    2    4
[3,]    6    8    7    4    3    2    5
[4,]    3    8    5    4    9    1    0
[5,]    7    6    2    1    9    8    3

> suits <- c("d", "c", "h", "s")
> numbers <- c("a", "j", "q", "k", as.character(1:10))
> deck <- expand.grid(number=numbers, suit=suits)
> deck<-paste(deck$number, deck$suit)
> deck
 [1] "a d"  "j d"  "q d"  "k d"  "1 d"  "2 d" 
 [7] "3 d"  "4 d"  "5 d"  "6 d"  "7 d"  "8 d" 
[13] "9 d"  "10 d" "a c"  "j c"  "q c"  "k c" 
[19] "1 c"  "2 c"  "3 c"  "4 c"  "5 c"  "6 c" 
[25] "7 c"  "8 c"  "9 c"  "10 c" "a h"  "j h" 
[31] "q h"  "k h"  "1 h"  "2 h"  "3 h"  "4 h" 
[37] "5 h"  "6 h"  "7 h"  "8 h"  "9 h"  "10 h"
[43] "a s"  "j s"  "q s"  "k s"  "1 s"  "2 s" 
[49] "3 s"  "4 s"  "5 s"  "6 s"  "7 s"  "8 s" 
[55] "9 s"  "10 s"
> kings<-paste("k", suits)
> kings
[1] "k d" "k c" "k h" "k s"
> mean(deck %in% kings)
[1] 0.07142857

> hands <- permutations(52, 2, v=deck) #deck 중에서 2개씩 뽑기
> nrows(hands)
[1] 2652
> first_card <- hands[,1] #first column
> second_card <- hands[,2]
> sum(first_card %in% kings)
[1] 204
> sum(first_card %in% kings & second_card%in% kings)/sum(first_card %in% kings)
[1] 0.05882353

> combinations(3,2)
     [,1] [,2]
[1,]    1    2
[2,]    1    3
[3,]    2    3





'programing > R studio' 카테고리의 다른 글

[Rstudio] rep, replicate, sample, table, prop.table  (0) 2021.06.28
R 패키지 설치 에러  (3) 2019.06.09
R 단축키  (0) 2019.03.23
시계열 ts()  (0) 2019.01.30
approx VS approxfun  (0) 2019.01.30

> beads<-rep(c("red","blue"), 3)
> beads
[1] "red"  "blue" "red"  "blue" "red"  "blue"


> beads<-rep(c("red","blue"), times=c(2,3))
> beads
[1] "red"  "red"  "blue" "blue" "blue"

> sample(beads, 1)
[1] "blue"

> A<-10000
> events<-replicate(A, sample(beads, 1))
> table(events)
events
blue  red 
5967 4033 
> prop.table(table(events))
events
  blue    red 
0.5967 0.4033 

> events<-replicate(100000, sample(beads, 1))
> prop.table(table(events))
events
   blue     red 
0.59903 0.40097 

> events<-replicate(100, sample(beads, 1))
> prop.table(table(events))
events
blue  red 
0.55 0.45 

> events<-sample(beads, A, replace = TRUE)
> prop.table(table(events))
events
  blue    red 
0.5999 0.4001 

'programing > R studio' 카테고리의 다른 글

[Rstudio]  (0) 2021.06.28
R 패키지 설치 에러  (3) 2019.06.09
R 단축키  (0) 2019.03.23
시계열 ts()  (0) 2019.01.30
approx VS approxfun  (0) 2019.01.30

cannot create dir 'C:/어쩌구/저쩌구' , reason 'Invalid argument'

 

R studio를 관리자권한으로 다시 열면 됩니다.

'programing > R studio' 카테고리의 다른 글

[Rstudio]  (0) 2021.06.28
[Rstudio] rep, replicate, sample, table, prop.table  (0) 2021.06.28
R 단축키  (0) 2019.03.23
시계열 ts()  (0) 2019.01.30
approx VS approxfun  (0) 2019.01.30

%>% : Ctrl+Shift+M

#(주석) : Ctrl+Shift+C


'programing > R studio' 카테고리의 다른 글

[Rstudio] rep, replicate, sample, table, prop.table  (0) 2021.06.28
R 패키지 설치 에러  (3) 2019.06.09
시계열 ts()  (0) 2019.01.30
approx VS approxfun  (0) 2019.01.30
특정 행 or 열 이름만 바꾸기  (0) 2018.12.03

시계열 ts()

R로 시계열 분석 시, frequency를 나눌 때는 데이터가 frequency대로 정확하게 떨어지게 나눠야 함

ex)

ts(data, start=c(2012,3), frequency = 12) 

2012년 1월~2018년 2월 자료까지 밖에 없는 경우

2012년 3월을 start 점으로 해줘야 함

'programing > R studio' 카테고리의 다른 글

R 패키지 설치 에러  (3) 2019.06.09
R 단축키  (0) 2019.03.23
approx VS approxfun  (0) 2019.01.30
특정 행 or 열 이름만 바꾸기  (0) 2018.12.03
duplicated(), unique(), distinct()  (0) 2018.09.17

approx VS approxfun


1. approx

- 지정된 점 혹은 지전된 수의 근사함수의 값을 리턴한다

- 알고 있는 지점에서 근사값(approximation)이 필요한 경우에 사용


2. approxfun

- 특정 시점에서 평가할 수 있는 함수를 반환한다

- 추후에 주어진 인수에 대해 근사값을 반환하는 함수가 필요한 경우에 사용



'programing > R studio' 카테고리의 다른 글

R 단축키  (0) 2019.03.23
시계열 ts()  (0) 2019.01.30
특정 행 or 열 이름만 바꾸기  (0) 2018.12.03
duplicated(), unique(), distinct()  (0) 2018.09.17
merge 함수의 all.x = TRUE  (0) 2018.09.10

특정 행 or 열 이름만 바꾸기


# temp데이터의 1열 이름을 date로 바꾸기

colnames(temp)[1]<-"date"


# temp데이터의 2행 이름을 seoul로 바꾸기

rownames(temp)[2]<-"seoul"

'programing > R studio' 카테고리의 다른 글

시계열 ts()  (0) 2019.01.30
approx VS approxfun  (0) 2019.01.30
duplicated(), unique(), distinct()  (0) 2018.09.17
merge 함수의 all.x = TRUE  (0) 2018.09.10
R - 기본함수 - paste / paste0  (0) 2018.08.22

> x <- c(1, 1, 4, 5, 4, 6)

 

> duplicated(x)

[1] FALSE  TRUE FALSE FALSE  TRUE FALSE

 

 

> x[duplicated(x)] # 중복된 애들 하나씩만 나열; 1,4=중복됨

[1] 1 4

 

> x[duplicated(x) | duplicated(x, fromLast = T)]

# 모든 중복된 애들; 1, 1, 4, 4

[1] 1 1 4 4

 

 

> # 중복된 애들 제거하고 나열

> x[! duplicated(x)]

[1] 1 4 5 6

> unique(x)

[1] 1 4 5 6

 

 

> # Remove duplicated rows based on col1 and col2

> library(dplyr)

> distinct(data, col1, col2)


 all.x = TRUE


logical; 

if TRUE, then extra rows will be added to the output, one for each row in x that has no matching row in y. 

These rows will have 'NA's in those columns that are usually filled with values from y. 

The default is FALSE, so that only rows with data from both x and y are included in the output.


TRUE이면 y에 일치하는 행이없는 x의 각 행에 하나씩 추가 행이 출력에 추가됩니다.

이 행은 대개 y 값을 채운 해당 열에서 'NA'을 갖습니다.

기본값은 FALSE이므로 x와 y의 데이터가있는 행만 출력에 포함됩니다.





출처: https://www.rdocumentation.org/packages/data.table/versions/1.11.4/topics/merge

R - 기본함수 - paste / paste0

https://m.blog.naver.com/PostView.nhn?blogId=coder1252&logNo=220985161855&proxyReferer=https%3A%2F%2Fwww.google.co.kr%2F


paste() 공백과 이어 붙이기

paste0() 공백없이 이어 붙이기

변수

- sep=" " 
이어 붙일 때 사용할 구분문자

- collapse=" "
결과값 두 개 이상일 때 


+ Recent posts