r introduction & auto make table1
TRANSCRIPT
![Page 1: R Introduction & auto make table1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5560e5e4d8b42aa65e8b5007/html5/thumbnails/1.jpg)
R & Rstudio데이터셋 다루기
Table 1 만들기
R을 이용한 보건학 통계분석1주차 : R & Rstudio 설치 및 Table1 작성
김진섭
서울대학교 보건대학원 예방의학교실 전임의, 유전체역학교실 박사과정
March 11, 2014
김진섭 R을 이용한 보건학 통계분석
![Page 2: R Introduction & auto make table1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5560e5e4d8b42aa65e8b5007/html5/thumbnails/2.jpg)
R & Rstudio데이터셋 다루기
Table 1 만들기
목차
1 R & Rstudio
2 데이터셋 다루기
3 Table 1 만들기
김진섭 R을 이용한 보건학 통계분석
![Page 3: R Introduction & auto make table1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5560e5e4d8b42aa65e8b5007/html5/thumbnails/3.jpg)
R & Rstudio데이터셋 다루기
Table 1 만들기
1.1 R의 특징
1 오픈 소스 소프트웨어(무료)
2 수많은 패키지 : 최신 분석방법이 가장 빠르게 적용된다.
3 완성된 프로그래밍 언어: 뭐든 가능.
4 숫자 하나하나를 추출할 수 있다.
5 그림의 퀄리티가 뛰어나다.
6 미국에서는 이미 대세(빅데이터)
김진섭 R을 이용한 보건학 통계분석
![Page 4: R Introduction & auto make table1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5560e5e4d8b42aa65e8b5007/html5/thumbnails/4.jpg)
R & Rstudio데이터셋 다루기
Table 1 만들기
1.2 R의 설치
1 구글에서 R 검색 : 가장 위에 나온다.
2 http://r-project.kr/wiki/R-basic/installation :설치 도움말
김진섭 R을 이용한 보건학 통계분석
![Page 5: R Introduction & auto make table1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5560e5e4d8b42aa65e8b5007/html5/thumbnails/5.jpg)
R & Rstudio데이터셋 다루기
Table 1 만들기
1.3 Rstudio의 설치
1 Rstudio.org
2 http://r-project.kr/wiki/R-basic/editors : 설치도움말
김진섭 R을 이용한 보건학 통계분석
![Page 6: R Introduction & auto make table1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5560e5e4d8b42aa65e8b5007/html5/thumbnails/6.jpg)
R & Rstudio데이터셋 다루기
Table 1 만들기
1.4 R 갖고 놀기: Data set 아님.
1 도움말 보기
2 기초 계산하기
3 여러가지 숫자열 지정
김진섭 R을 이용한 보건학 통계분석
![Page 7: R Introduction & auto make table1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5560e5e4d8b42aa65e8b5007/html5/thumbnails/7.jpg)
R & Rstudio데이터셋 다루기
Table 1 만들기
1.5 for, if, else, ifelse
for (i in 1:6) print(i)
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
1 if, else if 주의!! : 반드시 줄이 이어지게 해야 함.
2 ifelse(조건, 참일때, 거짓일때)
김진섭 R을 이용한 보건학 통계분석
![Page 8: R Introduction & auto make table1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5560e5e4d8b42aa65e8b5007/html5/thumbnails/8.jpg)
R & Rstudio데이터셋 다루기
Table 1 만들기
목표
1 데이터 정리는 Excel, 통계만 R로 돌린다.(Rcommander)
2 Data 정리 및 통계를 R로 돌린다.
3 자주쓰는 것들을 함수로 만들어 쓴다.
4 논문 테이블 및 그림을 R로 만든다.
5 논문을 R로 쓴다.
6 R 패키지 개발
김진섭 R을 이용한 보건학 통계분석
![Page 9: R Introduction & auto make table1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5560e5e4d8b42aa65e8b5007/html5/thumbnails/9.jpg)
R & Rstudio데이터셋 다루기
Table 1 만들기
2.1 디렉토리 설정
getwd()
setwd("/home/secondmath/Dropbox/GSPH/myteaching/orientation/") #set directory
김진섭 R을 이용한 보건학 통계분석
![Page 10: R Introduction & auto make table1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5560e5e4d8b42aa65e8b5007/html5/thumbnails/10.jpg)
R & Rstudio데이터셋 다루기
Table 1 만들기
2.2 데이터 읽어오기
핵심!!!! : 가급적 CSV(comma separate)파일로 읽자!!! 표준.
getwd()
## [1] "/home/secondmath/Dropbox/GSPH/myteaching/orientation"
setwd("/home/secondmath/Dropbox/GSPH/myteaching/orientation/") #set directory
getwd()
## [1] "/home/secondmath/Dropbox/GSPH/myteaching/orientation"
a = read.csv("week2.csv")
a = read.table("week2.csv", sep = ",", header = T)
기타 : 엑셀, SAS, SPSS, STATA 파일도 읽는것 가능
김진섭 R을 이용한 보건학 통계분석
![Page 11: R Introduction & auto make table1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5560e5e4d8b42aa65e8b5007/html5/thumbnails/11.jpg)
R & Rstudio데이터셋 다루기
Table 1 만들기
2.3 데이터 저장하기
핵심!!!! : 가급적 CSV(comma separate)파일로 쓰자!!! 표준.
write.csv(a, "write.csv", row.names = FALSE, quote = F)
write.table(a, "write.txt", sep = "\t", row.names = FALSE)
기타 : 엑셀, SAS, SPSS, STATA 파일도 쓰는것 가능. 절대 그럴필요 없다.
김진섭 R을 이용한 보건학 통계분석
![Page 12: R Introduction & auto make table1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5560e5e4d8b42aa65e8b5007/html5/thumbnails/12.jpg)
R & Rstudio데이터셋 다루기
Table 1 만들기
2.4 데이터 살펴보기 및 예제 데이터 소개
R 코드 참고.
1 혈압, BMI, TG, LDL 등..
2 가족 ID
3 특정 gene locus information: allele count 0,1,2
김진섭 R을 이용한 보건학 통계분석
![Page 13: R Introduction & auto make table1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5560e5e4d8b42aa65e8b5007/html5/thumbnails/13.jpg)
R & Rstudio데이터셋 다루기
Table 1 만들기
2.5 새로운 변수 만들기
1 직접 만들기
2 ifelse 문 이용하기
3 행렬 직접 지목하여 집어넣기
김진섭 R을 이용한 보건학 통계분석
![Page 14: R Introduction & auto make table1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5560e5e4d8b42aa65e8b5007/html5/thumbnails/14.jpg)
R & Rstudio데이터셋 다루기
Table 1 만들기
2.6 정렬, Subset, Merge
head(order(a$TG))
e = a[order(a$TG), ] ## low value first
head(e)
e = a[order(-a$TG), ] ## Large value first
head(e)
g = subset(a, sex == 0) ## only men
g = a[which(a$sex == 0), ] ## same
head(g)
aa = a[, c("ID", "TG")] ##example
head(aa)
c = merge(a, aa, by = "ID", all = F) ##all=F VS all=T
김진섭 R을 이용한 보건학 통계분석
![Page 15: R Introduction & auto make table1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5560e5e4d8b42aa65e8b5007/html5/thumbnails/15.jpg)
R & Rstudio데이터셋 다루기
Table 1 만들기
2.7 간단한 함수 만들기
twomean = function(x1, x2) {a = (x1 + x2)/2
a
}twomean(4, 6)
## [1] 5
김진섭 R을 이용한 보건학 통계분석
![Page 16: R Introduction & auto make table1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5560e5e4d8b42aa65e8b5007/html5/thumbnails/16.jpg)
R & Rstudio데이터셋 다루기
Table 1 만들기
3.1 그룹별 정렬하기 : 연속변수
tapply(Y값, 그룹변수, mean)
tapply(a$TG, a$sex, mean)
## 0 1
## 152.94 90.06
tapply(a$TG, a$sex, sd)
## 0 1
## 90.72 51.75
tb_mean = round(tapply(a$TG, a$sex, mean), 2)
tb_sd = round(tapply(a$TG, a$sex, sd), 2)
김진섭 R을 이용한 보건학 통계분석
![Page 17: R Introduction & auto make table1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5560e5e4d8b42aa65e8b5007/html5/thumbnails/17.jpg)
R & Rstudio데이터셋 다루기
Table 1 만들기
3.1 그룹별 정렬하기 : 연속변수(2)
aggregate(a[, c(6, 7, 8, 9, 10)], by = list(a$sex), mean)
## Group.1 FBS tCholesterol HDL LDL TG
## 1 0 95.88 193.1 47.87 113.57 152.94
## 2 1 92.48 178.0 57.70 98.12 90.06
aggregate(a[, 6:10], by = list(a$sex, a$genecount), mean)
## Group.1 Group.2 FBS tCholesterol HDL LDL TG
## 1 0 0 94.38 152.3 46.62 81.01 121.64
## 2 1 0 85.26 150.1 55.11 75.67 76.96
## 3 0 1 96.68 197.7 48.09 116.35 163.49
## 4 1 1 96.71 194.2 58.82 111.89 95.18
## 5 0 2 95.41 249.0 49.28 161.40 167.60
## 6 1 2 109.50 246.5 71.50 139.50 169.50
평균과 표준편차 정보 합쳐서 볼 수도 있다. paste 이용
김진섭 R을 이용한 보건학 통계분석
![Page 18: R Introduction & auto make table1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5560e5e4d8b42aa65e8b5007/html5/thumbnails/18.jpg)
R & Rstudio데이터셋 다루기
Table 1 만들기
3.2 그룹별 정렬하기: 범주형 변수
table(변수, 그룹변수), prop.table(table, 2)
options(digits = 4) # round
hyperTG_table = table(a$hyperTG, a$sex)
hyperTG_table
##
## 0 1
## 0 297 63
## 1 97 4
prop.table(hyperTG_table, 2) # 1: row, 2: column
##
## 0 1
## 0 0.7538 0.9403
## 1 0.2462 0.0597
김진섭 R을 이용한 보건학 통계분석
![Page 19: R Introduction & auto make table1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5560e5e4d8b42aa65e8b5007/html5/thumbnails/19.jpg)
R & Rstudio데이터셋 다루기
Table 1 만들기
3.3 apply 문
apply(행렬, 1, ,함수) # 1: 행별로, 2: 열별로
hyperTG_table
##
## 0 1
## 0 297 63
## 1 97 4
apply(hyperTG_table, 2, sum)
## 0 1
## 394 67
기타. sapply, lapply 등... 생략 .
김진섭 R을 이용한 보건학 통계분석
![Page 20: R Introduction & auto make table1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5560e5e4d8b42aa65e8b5007/html5/thumbnails/20.jpg)
R & Rstudio데이터셋 다루기
Table 1 만들기
3.4 테이블 1 만들기
전략!!! 그룹별정렬하기 이용: tb.1 함수 미리 준비함.
1 Missing 정리!!!! R은 NA가 missing.
2 쓸 변수만 남김 & 숫자형/범주형 확인
3 숫자형일 경우 3.1, 범주형일 경우 3.2
4 연속변수 - 그룹별 : t-test, ANOVA, Wilcox..
5 범주형변수 - 그룹별 : chi-square, fisher-exact
김진섭 R을 이용한 보건학 통계분석
![Page 21: R Introduction & auto make table1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5560e5e4d8b42aa65e8b5007/html5/thumbnails/21.jpg)
R & Rstudio데이터셋 다루기
Table 1 만들기
Table1 생성
Variable: Mean (SD) or N (%) 0 1 P-value P-value:NP
sex < 0.001 < 0.0010 394 (100) 0 (0)1 0 (0) 67 (100)
age 42.32 (11.4) 40.73 (10.86) 0.29 0.208DXA total tscore -0.28 (1.12) -0.51 (1.41) 0.134 0.309
FBS 95.88 (17.88) 92.48 (26.57) 0.184 < 0.001tCholesterol 193.13 (34.36) 178.01 (28.8) < 0.001 < 0.001
HDL 47.87 (11.01) 57.7 (13.58) < 0.001 < 0.001LDL 113.57 (30.8) 98.12 (25.81) < 0.001 < 0.001TG 152.94 (90.72) 90.06 (51.75) < 0.001 < 0.001
genecount 0.007 0.0070 105 (26.65) 27 (40.3)1 231 (58.63) 38 (56.72)2 58 (14.72) 2 (2.99)
김진섭 R을 이용한 보건학 통계분석
![Page 22: R Introduction & auto make table1](https://reader031.vdocuments.net/reader031/viewer/2022013121/5560e5e4d8b42aa65e8b5007/html5/thumbnails/22.jpg)
R & Rstudio데이터셋 다루기
Table 1 만들기
END
Email : [email protected]: (02)880-2473H.P: 010-9192-5385
김진섭 R을 이용한 보건학 통계분석