library(tidyverse)
## Warning: package 'tidyverse' was built under R version 3.3.2
## Loading tidyverse: ggplot2
## Loading tidyverse: tibble
## Loading tidyverse: tidyr
## Loading tidyverse: readr
## Loading tidyverse: purrr
## Loading tidyverse: dplyr
## Warning: package 'ggplot2' was built under R version 3.3.2
## Warning: package 'readr' was built under R version 3.3.2
## Warning: package 'purrr' was built under R version 3.3.2
## Conflicts with tidy packages ----------------------------------------------
## filter(): dplyr, stats
## lag(): dplyr, stats
df=read_csv("http://vincentarelbundock.github.io/Rdatasets/csv/survival/pbc.csv")
## Warning: Missing column names filled in: 'X1' [1]
## Parsed with column specification:
## cols(
## .default = col_integer(),
## age = col_double(),
## sex = col_character(),
## edema = col_double(),
## bili = col_double(),
## albumin = col_double(),
## alk.phos = col_double(),
## ast = col_double(),
## protime = col_double()
## )
## See spec(...) for full column specifications.
df=df[,-1]
str(df)
## Classes 'tbl_df', 'tbl' and 'data.frame': 418 obs. of 20 variables:
## $ id : int 1 2 3 4 5 6 7 8 9 10 ...
## $ time : int 400 4500 1012 1925 1504 2503 1832 2466 2400 51 ...
## $ status : int 2 0 2 2 1 2 0 2 2 2 ...
## $ trt : int 1 1 1 1 2 2 2 2 1 2 ...
## $ age : num 58.8 56.4 70.1 54.7 38.1 ...
## $ sex : chr "f" "f" "m" "f" ...
## $ ascites : int 1 0 0 0 0 0 0 0 0 1 ...
## $ hepato : int 1 1 0 1 1 1 1 0 0 0 ...
## $ spiders : int 1 1 0 1 1 0 0 0 1 1 ...
## $ edema : num 1 0 0.5 0.5 0 0 0 0 0 1 ...
## $ bili : num 14.5 1.1 1.4 1.8 3.4 0.8 1 0.3 3.2 12.6 ...
## $ chol : int 261 302 176 244 279 248 322 280 562 200 ...
## $ albumin : num 2.6 4.14 3.48 2.54 3.53 3.98 4.09 4 3.08 2.74 ...
## $ copper : int 156 54 210 64 143 50 52 52 79 140 ...
## $ alk.phos: num 1718 7395 516 6122 671 ...
## $ ast : num 137.9 113.5 96.1 60.6 113.2 ...
## $ trig : int 172 88 55 92 72 63 213 189 88 143 ...
## $ platelet: int 190 221 151 183 136 NA 204 373 251 302 ...
## $ protime : num 12.2 10.6 12 10.3 10.9 11 9.7 11 11 11.5 ...
## $ stage : int 4 3 4 4 3 3 3 3 2 4 ...
mean(df$age)
## [1] 50.74155
dfnum<-select(df,5,11:19)
#For loop
output <- vector("double", ncol(dfnum)) # Object nháºn kết quả
for (i in seq_along(dfnum)) { # chuá»—i
output[[i]] <- mean(dfnum[[i]],na.rm=T) #nội dung kết quả
}
output
## [1] 50.741551 3.220813 369.510563 3.497440 97.648387
## [6] 1982.655769 122.556346 124.702128 257.024570 10.731731
df%>%select(.,5,11:19)%>%map_dbl(mean,na.rm=T)
## age bili chol albumin copper alk.phos
## 50.741551 3.220813 369.510563 3.497440 97.648387 1982.655769
## ast trig platelet protime
## 122.556346 124.702128 257.024570 10.731731
df%>%select(.,5,11:19)%>%map_dbl(sd,na.rm=T)
## age bili chol albumin copper
## 10.4472144 4.4075064 231.9445450 0.4249716 85.6139199
## alk.phos ast trig platelet protime
## 2140.3888245 56.6995249 65.1486387 98.3255845 1.0220003
df%>%select(.,5,11:19)%>%map(~quantile(.,c(0.025,0.5,0.975),na.rm=T))
## $age
## 2.5% 50% 97.5%
## 32.55031 51.00068 70.04346
##
## $bili
## 2.5% 50% 97.5%
## 0.400 1.400 17.315
##
## $chol
## 2.5% 50% 97.5%
## 174.075 309.500 1086.225
##
## $albumin
## 2.5% 50% 97.5%
## 2.54 3.53 4.22
##
## $copper
## 2.5% 50% 97.5%
## 13.00 73.00 294.95
##
## $alk.phos
## 2.5% 50% 97.5%
## 504.75 1259.00 9261.74
##
## $ast
## 2.5% 50% 97.5%
## 49.6000 114.7000 245.2487
##
## $trig
## 2.5% 50% 97.5%
## 52.025 108.000 279.800
##
## $platelet
## 2.5% 50% 97.5%
## 95.0 251.0 470.4
##
## $protime
## 2.5% 50% 97.5%
## 9.5000 10.6000 13.1625
list("mean","sd","median","min","max")%>%invoke_map(,df$bili)%>%str()
## List of 5
## $ : num 3.22
## $ : num 4.41
## $ : num 1.4
## $ : num 0.3
## $ : num 28
df%>%select(.,5,6,11:20)%>%split(.$sex)%>%map(~lm(bili~ast,data=.))%>%map(summary)
## $f
##
## Call:
## lm(formula = bili ~ ast, data = .)
##
## Residuals:
## Min 1Q Median 3Q Max
## -9.7927 -1.9258 -0.7962 0.2759 21.9344
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1.159930 0.601710 -1.928 0.0549 .
## ast 0.036419 0.004438 8.206 9.02e-15 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.263 on 274 degrees of freedom
## (98 observations deleted due to missingness)
## Multiple R-squared: 0.1973, Adjusted R-squared: 0.1943
## F-statistic: 67.33 on 1 and 274 DF, p-value: 9.017e-15
##
##
## $m
##
## Call:
## lm(formula = bili ~ ast, data = .)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.9472 -1.0597 -0.5661 0.6612 5.6187
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.215974 0.945540 0.228 0.82069
## ast 0.021774 0.007245 3.005 0.00496 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.015 on 34 degrees of freedom
## (8 observations deleted due to missingness)
## Multiple R-squared: 0.2099, Adjusted R-squared: 0.1866
## F-statistic: 9.031 on 1 and 34 DF, p-value: 0.004958
df%>%select(.,5,6,11:20)%>%split(.$sex)%>%map(~lm(bili~ast,data=.))%>%map(summary) %>%map_dbl(~.$r.squared)
## f m
## 0.1972687 0.2098785
df%>%select(.,5,6,11:20)%>%split(.$sex)%>%map(~lm(bili~ast,data=.))%>%map(summary)%>%map(~.$coefficients)
## $f
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1.15992966 0.60171046 -1.927721 5.492338e-02
## ast 0.03641918 0.00443824 8.205770 9.017164e-15
##
## $m
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.21597383 0.94554034 0.2284131 0.820693319
## ast 0.02177421 0.00724546 3.0052217 0.004957891
df%>%select(.,5,6,11:20)%>%split(.$sex)%>%map(~lm(ast~as.factor(stage),data=.))%>%map(summary.aov)
## $f
## Df Sum Sq Mean Sq F value Pr(>F)
## as.factor(stage) 3 42305 14102 4.358 0.00511 **
## Residuals 272 880138 3236
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 98 observations deleted due to missingness
##
## $m
## Df Sum Sq Mean Sq F value Pr(>F)
## as.factor(stage) 3 6069 2023 0.908 0.448
## Residuals 32 71289 2228
## 8 observations deleted due to missingness
df%>%select(.,5,6,11:20)%>%split(.$sex)%>%map(~lm(bili~ast,data=.))%>%map(plot)








## $f
## NULL
##
## $m
## NULL