0. ํ์ํ ํจํค์ง ๋ถ๋ฌ์ค๊ธฐ
library1 = c("plyr", "dplyr", "ggplot2", "stringr", "tidyr", "readxl", "xlsx", "readxl") unlist(lapply(library1, require, character.only=TRUE))
1. dataset ๋ถ๋ฌ์ค๊ธฐ
setwd('C:/ITWILL/2_Rwork/00/SemiProject') hospital = read.csv("hospital.csv") population = read.csv("population.csv", check.names=F, header = T) head(hospital, n=10) #๋ณ์ 1๊ฐ๋ง๋ค์ ์ ๋ณด ๊ธฐ์ฌ. head(population, n=10) #์ง์ญ ๋ณ ์ธ๊ตฌ ์ dataset str(population)
2. dataset ์ ์
๊ฒฐ์ธก์น ์ฒ๋ฆฌ
sum(is.na(hospital)) #0 sum(is.na(population)) #0
๋น ๋
ธ์ธ์ธ๊ตฌ ํ ์ถ๊ฐํ๊ธฐ (์ ์ฒด์ธ๊ตฌ ์-๋
ธ์ธ์ธ๊ตฌ ์-์์ ์์ธ๊ตฌ ์)
pop = (population$์ด์ธ๊ตฌ์-(population$๋
ธ์ธ์ธ๊ตฌ+population$์์ ์์ธ๊ตฌ)) population = cbind(population, pop) head(population)
๋
ธ์ธ ์ธ๊ตฌ, ์์ ์ ์ธ๊ตฌ, ๋น ๋
ธ์ธ์ธ๊ตฌ์ ๋น์จ ํ ์ถ๊ฐํ๊ธฐ
pop_be = (population$pop/population$์ด์ธ๊ตฌ์) * 100 population = cbind(population, pop_be) pop_old_be = (population$๋
ธ์ธ์ธ๊ตฌ/population$์ด์ธ๊ตฌ์) * 100 population = cbind(population, pop_old_be) pop_child_be = (population$์์ ์์ธ๊ตฌ/population$์ด์ธ๊ตฌ์) * 100 population = cbind(population, pop_child_be) head(population)
๋ณ์๋ช
์์ด๋ก ๋ณํ
colnames(hospital) = c('division', 'area1', 'area2') colnames(population) = c('area', 'total_pop', 'pop_child', 'pop_old', '65~69', '70~74', '75~79', '80~84', '85~89', '90~94', '95~99', '100~', 'pop', 'pop_be', 'pop_old_be', 'pop_child_be') head(hospital) head(population)
๋ชจ๋ ๋ฌธ์ ๊ณต๋ฐฑ -> ๋จ์ผ ์คํ์ด์ค ๊ณต๋ฐฑ์ผ๋ก ์นํ
str_trim(hospital$area1) str_trim(hospital$area2)
hospital์์ ์ํ๋ ์ ๋ณด(์ง์ญ๋ณ ๋ณ์ ๊ฐ์) ์ถ์ถ
hospital = hospital %>% group_by(area1, area2) %>% dplyr :: summarize(count=n()) str(hospital) #grouped_df
[๊ฒฝ๊ณ ] `summarise()` has grouped output by 'area1'. You can override using the `.groups` argument.
[ํด๊ฒฐ] ๋ง์ง๋ง ๋ณ์ ์ญ์ ์๋ฆฌ๋ ๋จ์ ๊ฒฝ๊ณ ๋ฉ์์ง์ด๋ฏ๋ก ๋ฌด์ํ๊ณ ์งํ
hospital์์ area1๊ณผ area2๋ฅผ ๋ํด ใ
ใ
์ ใ
ใ
๊ตฐ ํํ๋ก ๋ง๋ค๊ธฐ
hospital = unite(hospital, "area", c(area1, area2), sep=" ") head (hospital)
๋ ๋ฐ์ดํฐ ํ๋ ์์ ๊ณตํต๊ฐ(area:์ง์ญ)์ผ๋ก ๋ฌถ๊ธฐ
total = merge(hospital, population, by="area", all=T) head(total)
[๋ฌธ์ ์ ] ๊ฒฐ์ธก์น ๋ฐ์
sum(is.na(total)) #87๊ฐ
1. -์ถ์ฅ์ ์ง์ญ์ ์ด ๋ณ์ ์ NA, ์ธ๊ตฌ ์ 0
2. -์, -๋ ๋จ์์ ์ด ๋ณ์ ์๊ฐ ์ถ๋ ฅ๋์ง ์์
[ํด๊ฒฐ1] ์ถ์ฅ์์๋ ๋ณ์๊ณผ ์ธ๊ตฌ๊ฐ ๋ชจ๋ ์์ผ๋ฏ๋ก ์๋ฏธ์๋ ๋ฐ์ดํฐ๋ผ๊ณ ํ๋จ, ํด๋น ํ ๋ชจ๋ ์ญ์
total[total == 0] = NA #์ถ์ฅ์ ์ง์ญ์ ๋ชจ๋ 0 ๊ฐ์ NA๋ก ๋ณํ total[is.na(total$pop),] total <- total[!is.na(total$pop),] #์ด ์ธ๊ตฌ ์(pop) NA๊ฐ ์ญ์ sum(is.na(total)) #27๊ฐ total[is.na(total$count),]
[ํด๊ฒฐ2] sql์ด์ฉํด์ ๊ฐ ์ง์ญ๋ณ ๋ณ์ ์ด ๊ฐ์ ํ์ธํ ๋ค ์์
ํ์ผ๋ก ์ง์ ์์น ์
๋ ฅ.
write.xlsx(total, "C:/ITWILL/2_Rwork/00/SemiProject/total.xlsx", col.names=T, row.names=T, append=F) total = read_excel("total2.xlsx")
๋ ๋จ์ ์ด ๋ณ์ ์ ๋ํ ์๋ฏธ์๋ ๋ฐ์ดํฐ๋ผ๊ณ ํ๋จ, ํด๋น ํ ๋ชจ๋ ์ญ์
total <- total[!is.na(total$count),] sum(is.na(total$count)) #0๊ฐ
'๊ฐ์ธ๊ณต๋ถ > R' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์ธ๋ฏธํ๋ก์ ํธ03 ์ถ๊ฐ๋ถ์ (0) | 2021.10.30 |
---|---|
์ธ๋ฏธํ๋ก์ ํธ02 ์๊ด๋ถ์ (0) | 2021.10.29 |
39. R ์์๋ธ๋ชจ๋ธ ์ฐ์ต๋ฌธ์ (0) | 2021.10.25 |
36. R ์ฐ๊ด๋ถ์ ์ฐ์ต๋ฌธ์ (0) | 2021.10.22 |
35. R ๊ตฐ์ง๋ถ์ ์ฐ์ต๋ฌธ์ (0) | 2021.10.21 |