-
Notifications
You must be signed in to change notification settings - Fork 30
/
tidyverse.R
92 lines (55 loc) · 1.64 KB
/
tidyverse.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
a <- read.csv("https://raw.githubusercontent.com/jinseob2kim/R-skku-biohrs/main/data/smc_example1.csv")
library(magrittr)
a %>% head
head(a)
a %>% head(10)
10 %>% head(a, .)
subset(a, Sex == "M")
a %>% subset(Sex == "M")
a$Sex
a[["Sex"]]
a[, "Sex"]
a %>% .$Sex
a %>% .[["Sex"]]
a %>% .[, "Sex"]
head(subset(a, Sex == "M"))
b <- subset(a, Sex == "M")
head(b)
a %>%
subset(Sex == "M") %>%
head
b <- subset(a, Sex == "M")
model <- glm(DM ~ Age + Weight + BMI, data = b, family = binomial)
summ.model <- summary(model)
summ.model$coefficients
a %>%
subset(Sex == "M") %>%
glm(DM ~ Age + Weight + BMI, data = ., family = binomial) %>%
summary %>%
.$coefficients
b <- subset(a, Age >= 50)
aggregate(. ~ Sex + Smoking, data = b,
FUN = function(x){c(mean = mean(x), sd = sd(x))})
a %>%
subset(Age >= 50) %>%
aggregate(. ~ Sex + Smoking, data = .,
FUN = function(x){c(mean = mean(x), sd = sd(x))})
library(dplyr) ## 따로 magrittr 불러올 필요 없음.
a %>%
filter(Age >= 50) %>%
select(-STRESS_EXIST) %>% ## 범주형 변수 제외
group_by(Sex, Smoking) %>%
summarize_all(list(mean = mean, sd = sd))
a[order(a$Age), ]
arrange(a, Age, Sex)
arrange(a, "Age", "Sex")
a[, c("Age", "Sex", "Height")]
select(a, Age, Sex, Height)
select(a, Sex:Height)
a %>% filter(Sex == "M") %>% select(Sex:HTN) %>% arrange(Age)
a$old <- as.integer(a$Age >= 65)
a$overweight <- as.integer(a$BMI >= 27)
a %>% transmute(Old = as.integer(Age >= 65), Overweight = as.integer(BMI >= 27))
zz <- a %>%
group_by(Sex, Smoking) %>%
summarize_all(funs(mean = mean, sd = sd))