๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Programming/R

(10)
๊ธฐ๋ณธ ํ•จ์ˆ˜ ์‚ฌ์šฉ๋ฒ• R์˜ ๋‚ด์žฅ๋œ ๊ธฐ๋ณธ ํ•จ์ˆ˜์˜ ์‚ฌ์šฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž. โ–ก if ( ) { } else if ( ) { } else { } In: char = 'A' if (char == 'B') { print('if') } else if (char == 'C') { print('else if') } else { print('else') } Out: [1] "else" โ–ก for ( ) { } In: for (i in 1:5) { print(i) } Out: [1] 1 [1] 2 [1] 3 [1] 4 [1] 5 โ–ก ifelse In: char = 'A' ifelse(char == 'A', 'char is A', 'char is not A') Out: [1] "char is A" โ–ท ifelse ํ•จ์ˆ˜์˜ ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋Š” ์กฐ๊ฑด์„ ..
caret ์‚ฌ์šฉ๋ฒ• caret ํŒจํ‚ค์ง€๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์–‘ํ•œ ๋จธ์‹ ๋Ÿฌ๋‹ ๊ธฐ๋ฒ•์˜ ์ ์šฉ ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด์ž. In: df_iris = iris str(df_iris) Out: 'data.frame':150 obs. of 5 variables: $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ... $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ... $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ... $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ... $ Species : Factor w/ 3 levels ..
dplyr ์‚ฌ์šฉ๋ฒ• dplyr์˜ ๋Œ€ํ‘œ์ ์ธ ํ•จ์ˆ˜ select, filter, mutate, summarise, group_by, sample_n, sample_frac์˜ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์„ ์ดํ•ดํ•˜๊ณ  ์ ์šฉํ•˜์—ฌ ๋ณด์ž. In: library(dplyr) df_iris = iris str(df_iris) Out: Sepal.Length Sepal.Width Petal.Length Petal.Width Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100 1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300 Median :5.800 Median :3.000 Median :4.350 Median :1.300 Mean :5.843 Mean :3.057 Mean :3...
๊ฒฐ์ธก์น˜ ๋‹ค๋ฃจ๋Š” ๋ฐฉ๋ฒ• ๊ฒฐ์ธก์น˜ ๋‹ค๋ฃจ๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•˜์—ฌ ์•Œ์•„๋ณด์ž. โ–ก ๊ฒฐ์ธก์น˜ ํ™•์ธ In: library(MASS) df_car = Cars93 df_car %>% sapply(function(x) sum(is.na(x))) Out: Manufacturer Model Type 0 0 0 Min.Price Price Max.Price 0 0 0 MPG.city MPG.highway AirBags 0 0 0 DriveTrain Cylinders EngineSize 0 0 0 Horsepower RPM Rev.per.mile 0 0 0 Man.trans.avail Fuel.tank.capacity Passengers 0 0 0 Length Wheelbase Width 0 0 0 Turn.circle Rear.seat.room Luggag..
CSV ํŒŒ์ผ ์ฝ๊ธฐ/์“ฐ๊ธฐ CSV ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ ๋’ค, ์ด๋ฅผ ํŠน์ • ๊ฒฝ๋กœ์— ์“ฐ๊ณ  ์ฝ๊ธฐ๋ฅผ ์ˆ˜ํ–‰ํ•˜์—ฌ ๋ณด์ž. In: df_car = mtcars df_car %>% head() Out: mpg cyl disp hp drat wt qsec vs am gear carb Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 Va..
๋ฌธ์ž์—ด ๋ฐ์ดํ„ฐ ๋‹ค๋ฃจ๋Š” ๋ฐฉ๋ฒ• R์˜ ๋‚ด์žฅ๋˜์–ด ์žˆ๋Š” ํ•จ์ˆ˜์™€ stringr ํŒจํ‚ค์ง€๋ฅผ ํ™œํ•˜์—ฌ ๋ฌธ์ž์—ด ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃจ๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณผ ๊ฒƒ์ด๋‹ค. โ–ก ๋ฌธ์ž ์ด์–ด ๋ถ™์ด๊ธฐ In: paste('Rooney', 'Song', sep = '_') paste0('Rooney', 'Song') str_c('Rooney', 'Song', sep = '_') Out: [1] "Rooney_Song" [1] "RooneySong" [1] "Rooney_Song" โ–ท paste, paste0 ํ•จ์ˆ˜๋Š” ๋‘ ๋ฌธ์ž์—ด์„ ๋ถ™์—ฌ์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. ๋‘ ํ•จ์ˆ˜์˜ ์ฐจ์ด์ ์€ paste0 ํ•จ์ˆ˜๋Š” ๋ถ™์ผ ๋•Œ, ์‚ฌ์ด์— ๋ฌธ์ž๋ฅผ ์‚ฝ์ž…ํ•˜์ง€ ์•Š๊ณ , ๋ฐ”๋กœ ๋ถ™์ธ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. paste ํ•จ์ˆ˜์˜ sep ์ธ์ž๋ฅผ ํ†ตํ•ด ๋‘ ๋ฌธ์ž์—ด์„ ๋ถ™์ผ ๋•Œ, ์‚ฌ์ด์— ๋“ค์–ด๊ฐˆ ๋ฌธ์ž๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. โ–ท str_c ํ•จ์ˆ˜๋Š” st..
์‹œ๊ณ„์—ด ๋ฐ์ดํ„ฐ ๋‹ค๋ฃจ๋Š” ๋ฐฉ๋ฒ• R์˜ ๋‚ด์žฅ๋˜์–ด ์žˆ๋Š” ํ•จ์ˆ˜์™€ lubridate ํŒจํ‚ค์ง€๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์‹œ๊ณ„์—ด ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃจ๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณผ ๊ฒƒ์ด๋‹ค. โ–ก ๊ธฐ๋ณธ ํ•จ์ˆ˜๋ฅผ ํ™œ์šฉํ•œ ์‹œ๊ณ„์—ด ๋ฐ์ดํ„ฐ ๋‹ค๋ฃจ๊ธฐ In: date_form = c('%Y%m%d', '%Y.%m.%d', '%Y~%m~%d', '%Y-%m-%d') date_1 = as.Date('20201019', tryFormats = date_form) date_2 = as.Date('2020.10.19', tryFormats = date_form) date_3 = as.Date('2020~10~19', tryFormats = date_form) print(date_1) print(date_2) print(date_3) print(class(date_1)) print(class(date_2)..
tidyr ์‚ฌ์šฉ๋ฒ• tidyr ํŒจ์บ์ง€์˜ ๋Œ€ํ‘œ์ ์ธ ํ•จ์ˆ˜ gather, spread, seperate, unite์˜ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์„ ์ดํ•ดํ•˜๊ณ  ์ ์šฉํ•˜์—ฌ ๋ณด์ž. In: library(dplyr) library(tidyr) df_iris = iris df_iris$id = 1:nrow(df_iris) df_iris = df_iris[, c(6, 1:5)] str(df_iris) Out: 'data.frame':150 obs. of 6 variables: $ id : int 1 2 3 4 5 6 7 8 9 10 ... $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ... $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ... $ ..
apply, sapply, lapply ์‚ฌ์šฉ๋ฒ• apply, sapply, lapply ํ•จ์ˆ˜์˜ ์‚ฌ์šฉ๋ฒ•์„ ์ดํ•ดํ•˜๊ณ  ์ ์šฉํ•˜์—ฌ ๋ณด์ž. In: library(dplyr) df_iris = iris df_iris_num = iris %>% select(-Species) str(df_iris_num) Out: 'data.frame':150 obs. of 4 variables: $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ... $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ... $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ... $ Petal.Width : num 0.2 0.2 0.2 0..
Leaflet์„ ์ด์šฉํ•œ ์ง€๋„ ์‹œ๊ฐํ™” R์˜ Leaflet์„ ์ด์šฉํ•˜์—ฌ ์ง€๋„๋ฅผ ์‹œ๊ฐํ™” ํ•ด๋ณด์ž. ์šฐ๋ฆฌ๋‚˜๋ผ์˜ ํ–‰์ •๊ตฌ์—ญ์„ ์‹œ๊ฐํ™” ํ•  ๊ฒƒ์ด๋‹ค. ์—ฌ๊ธฐ(www.gisdeveloper.co.kr/?p=2332)์—์„œ ์–ป์€ 2020๋…„ 5์›”์˜ ์‹œ๊ตฐ๊ตฌ ํ–‰์ •๊ตฌ์—ญ ๋ฐ์ดํ„ฐ๋ฅผ ์ด์šฉํ•  ๊ฒƒ์ด๋‹ค. In: library(leaflet) library(raster) library(htmltools) data_map = shapefile('../input/SIG.shp') โ–ท leaflet์€ ์ง€๋„์˜ ์‹œ๊ฐํ™”๋ฅผ ์œ„ํ•ด, raster์€ ํ™•์žฅ์ž๊ฐ€ shp์ธ ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ์œ„ํ•ด, htmltools๋Š” ์‹œ๊ฐํ™”๋œ ์ง€๋„ ์œ„์— ๋‚˜ํƒ€๋‚˜๋Š” ํ…์ŠคํŠธ๋ฅผ ์ˆ˜์ •ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋‹ค. In: head(data_map@data) head(data_map@plotOrder) # head(data_map@polyg..