This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
df<-read.csv("disease.csv")
df %>% glimpse
## Rows: 4
## Columns: 194
## $ year <int> 1999, 2000, 2001, 2002
## $ Afghanistan <int> 0, 0, 0, 0
## $ Albania <dbl> 89.0, 132.0, 54.0, 4.9
## $ Algeria <dbl> 25.0, 0.0, 14.0, 0.7
## $ Andorra <dbl> 245.0, 138.0, 312.0, 12.4
## $ Angola <dbl> 217.0, 57.0, 45.0, 5.9
## $ Antigua...Barbuda <dbl> 102.0, 128.0, 45.0, 4.9
## $ Argentina <dbl> 193.0, 25.0, 221.0, 8.3
## $ Armenia <dbl> 21.0, 179.0, 11.0, 3.8
## $ Australia <dbl> 261.0, 72.0, 212.0, 10.4
## $ Austria <dbl> 279.0, 75.0, 191.0, 9.7
## $ Azerbaijan <dbl> 21.0, 46.0, 5.0, 1.3
## $ Bahamas <dbl> 122.0, 176.0, 51.0, 6.3
## $ Bahrain <int> 42, 63, 7, 2
## $ Bangladesh <int> 0, 0, 0, 0
## $ Barbados <dbl> 143.0, 173.0, 36.0, 6.3
## $ Belarus <dbl> 142.0, 373.0, 42.0, 14.4
## $ Belgium <dbl> 295.0, 84.0, 212.0, 10.5
## $ Belize <dbl> 263.0, 114.0, 8.0, 6.8
## $ Benin <dbl> 34.0, 4.0, 13.0, 1.1
## $ Bhutan <dbl> 23.0, 0.0, 0.0, 0.4
## $ Bolivia <dbl> 167.0, 41.0, 8.0, 3.8
## $ Bosnia.Herzegovina <dbl> 76.0, 173.0, 8.0, 4.6
## $ Botswana <dbl> 173.0, 35.0, 35.0, 5.4
## $ Brazil <dbl> 245.0, 145.0, 16.0, 7.2
## $ Brunei <dbl> 31.0, 2.0, 1.0, 0.6
## $ Bulgaria <dbl> 231.0, 252.0, 94.0, 10.3
## $ Burkina.Faso <dbl> 25.0, 7.0, 7.0, 4.3
## $ Burundi <dbl> 88.0, 0.0, 0.0, 6.3
## $ Cote.d.Ivoire <int> 37, 1, 7, 4
## $ Cabo.Verde <int> 144, 56, 16, 4
## $ Cambodia <dbl> 57.0, 65.0, 1.0, 2.2
## $ Cameroon <dbl> 147.0, 1.0, 4.0, 5.8
## $ Canada <dbl> 240.0, 122.0, 100.0, 8.2
## $ Central.African.Republic <dbl> 17.0, 2.0, 1.0, 1.8
## $ Chad <dbl> 15.0, 1.0, 1.0, 0.4
## $ Chile <dbl> 130.0, 124.0, 172.0, 7.6
## $ China <int> 79, 192, 8, 5
## $ Colombia <dbl> 159.0, 76.0, 3.0, 4.2
## $ Comoros <dbl> 1.0, 3.0, 1.0, 0.1
## $ Congo <dbl> 76.0, 1.0, 9.0, 1.7
## $ Cook.Islands <dbl> 0.0, 254.0, 74.0, 5.9
## $ Costa.Rica <dbl> 149.0, 87.0, 11.0, 4.4
## $ Croatia <dbl> 230.0, 87.0, 254.0, 10.2
## $ Cuba <dbl> 93.0, 137.0, 5.0, 4.2
## $ Cyprus <dbl> 192.0, 154.0, 113.0, 8.2
## $ Czech.Republic <dbl> 361.0, 170.0, 134.0, 11.8
## $ North.Korea <int> 0, 0, 0, 0
## $ DR.Congo <dbl> 32.0, 3.0, 1.0, 2.3
## $ Denmark <dbl> 224.0, 81.0, 278.0, 10.4
## $ Djibouti <dbl> 15.0, 44.0, 3.0, 1.1
## $ Dominica <dbl> 52.0, 286.0, 26.0, 6.6
## $ Dominican.Republic <dbl> 193.0, 147.0, 9.0, 6.2
## $ Ecuador <dbl> 162.0, 74.0, 3.0, 4.2
## $ Egypt <dbl> 6.0, 4.0, 1.0, 0.2
## $ El.Salvador <dbl> 52.0, 69.0, 2.0, 2.2
## $ Equatorial.Guinea <dbl> 92.0, 0.0, 233.0, 5.8
## $ Eritrea <dbl> 18.0, 0.0, 0.0, 0.5
## $ Estonia <dbl> 224.0, 194.0, 59.0, 9.5
## $ Ethiopia <dbl> 20.0, 3.0, 0.0, 0.7
## $ Fiji <int> 77, 35, 1, 2
## $ Finland <int> 263, 133, 97, 10
## $ France <dbl> 127.0, 151.0, 370.0, 11.8
## $ Gabon <dbl> 347.0, 98.0, 59.0, 8.9
## $ Gambia <dbl> 8.0, 0.0, 1.0, 2.4
## $ Georgia <dbl> 52.0, 100.0, 149.0, 5.4
## $ Germany <dbl> 346.0, 117.0, 175.0, 11.3
## $ Ghana <dbl> 31.0, 3.0, 10.0, 1.8
## $ Greece <dbl> 133.0, 112.0, 218.0, 8.3
## $ Grenada <dbl> 199.0, 438.0, 28.0, 11.9
## $ Guatemala <dbl> 53.0, 69.0, 2.0, 2.2
## $ Guinea <dbl> 9.0, 0.0, 2.0, 0.2
## $ Guinea.Bissau <dbl> 28.0, 31.0, 21.0, 2.5
## $ Guyana <dbl> 93.0, 302.0, 1.0, 7.1
## $ Haiti <dbl> 1.0, 326.0, 1.0, 5.9
## $ Honduras <int> 69, 98, 2, 3
## $ Hungary <dbl> 234.0, 215.0, 185.0, 11.3
## $ Iceland <dbl> 233.0, 61.0, 78.0, 6.6
## $ India <dbl> 9.0, 114.0, 0.0, 2.2
## $ Indonesia <dbl> 5.0, 1.0, 0.0, 0.1
## $ Iran <int> 0, 0, 0, 0
## $ Iraq <dbl> 9.0, 3.0, 0.0, 0.2
## $ Ireland <dbl> 313.0, 118.0, 165.0, 11.4
## $ Israel <dbl> 63.0, 69.0, 9.0, 2.5
## $ Italy <dbl> 85.0, 42.0, 237.0, 6.5
## $ Jamaica <dbl> 82.0, 88.0, 9.0, 3.4
## $ Japan <int> 77, 202, 16, 7
## $ Jordan <dbl> 6.0, 21.0, 1.0, 0.5
## $ Kazakhstan <dbl> 124.0, 246.0, 12.0, 6.8
## $ Kenya <dbl> 58.0, 22.0, 2.0, 1.8
## $ Kiribati <int> 21, 34, 1, 1
## $ Kuwait <int> 0, 0, 0, 0
## $ Kyrgyzstan <dbl> 31.0, 88.0, 6.0, 2.4
## $ Laos <dbl> 62.0, 0.0, 123.0, 6.2
## $ Latvia <dbl> 281.0, 216.0, 62.0, 10.5
## $ Lebanon <dbl> 20.0, 55.0, 31.0, 1.9
## $ Lesotho <dbl> 82.0, 50.0, 0.0, 2.8
## $ Liberia <dbl> 19.0, 152.0, 2.0, 3.1
## $ Libya <int> 0, 0, 0, 0
## $ Lithuania <dbl> 343.0, 244.0, 56.0, 12.9
## $ Luxembourg <dbl> 236.0, 133.0, 271.0, 11.4
## $ Madagascar <dbl> 26.0, 15.0, 4.0, 0.8
## $ Malawi <dbl> 8.0, 11.0, 1.0, 1.5
## $ Malaysia <dbl> 13.0, 4.0, 0.0, 0.3
## $ Maldives <int> 0, 0, 0, 0
## $ Mali <dbl> 5.0, 1.0, 1.0, 0.6
## $ Malta <dbl> 149.0, 100.0, 120.0, 6.6
## $ Marshall.Islands <int> 0, 0, 0, 0
## $ Mauritania <int> 0, 0, 0, 0
## $ Mauritius <dbl> 98.0, 31.0, 18.0, 2.6
## $ Mexico <dbl> 238.0, 68.0, 5.0, 5.5
## $ Micronesia <dbl> 62.0, 50.0, 18.0, 2.3
## $ Monaco <int> 0, 0, 0, 0
## $ Mongolia <dbl> 77.0, 189.0, 8.0, 4.9
## $ Montenegro <dbl> 31.0, 114.0, 128.0, 4.9
## $ Morocco <dbl> 12.0, 6.0, 10.0, 0.5
## $ Mozambique <dbl> 47.0, 18.0, 5.0, 1.3
## $ Myanmar <dbl> 5.0, 1.0, 0.0, 0.1
## $ Namibia <dbl> 376.0, 3.0, 1.0, 6.8
## $ Nauru <int> 49, 0, 8, 1
## $ Nepal <dbl> 5.0, 6.0, 0.0, 0.2
## $ Netherlands <dbl> 251.0, 88.0, 190.0, 9.4
## $ New.Zealand <dbl> 203.0, 79.0, 175.0, 9.3
## $ Nicaragua <dbl> 78.0, 118.0, 1.0, 3.5
## $ Niger <dbl> 3.0, 2.0, 1.0, 0.1
## $ Nigeria <dbl> 42.0, 5.0, 2.0, 9.1
## $ Niue <int> 188, 200, 7, 7
## $ Norway <dbl> 169.0, 71.0, 129.0, 6.7
## $ Oman <dbl> 22.0, 16.0, 1.0, 0.7
## $ Pakistan <int> 0, 0, 0, 0
## $ Palau <dbl> 306.0, 63.0, 23.0, 6.9
## $ Panama <dbl> 285.0, 104.0, 18.0, 7.2
## $ Papua.New.Guinea <dbl> 44.0, 39.0, 1.0, 1.5
## $ Paraguay <dbl> 213.0, 117.0, 74.0, 7.3
## $ Peru <dbl> 163.0, 160.0, 21.0, 6.1
## $ Philippines <dbl> 71.0, 186.0, 1.0, 4.6
## $ Poland <dbl> 343.0, 215.0, 56.0, 10.9
## $ Portugal <int> 194, 67, 339, 11
## $ Qatar <dbl> 1.0, 42.0, 7.0, 0.9
## $ South.Korea <dbl> 140.0, 16.0, 9.0, 9.8
## $ Moldova <dbl> 109.0, 226.0, 18.0, 6.3
## $ Romania <dbl> 297.0, 122.0, 167.0, 10.4
## $ Russian.Federation <dbl> 247.0, 326.0, 73.0, 11.5
## $ Rwanda <dbl> 43.0, 2.0, 0.0, 6.8
## $ St..Kitts...Nevis <dbl> 194.0, 205.0, 32.0, 7.7
## $ St..Lucia <dbl> 171.0, 315.0, 71.0, 10.1
## $ St..Vincent...the.Grenadines <dbl> 120.0, 221.0, 11.0, 6.3
## $ Samoa <dbl> 105.0, 18.0, 24.0, 2.6
## $ San.Marino <int> 0, 0, 0, 0
## $ Sao.Tome...Principe <dbl> 56.0, 38.0, 140.0, 4.2
## $ Saudi.Arabia <dbl> 0.0, 5.0, 0.0, 0.1
## $ Senegal <dbl> 9.0, 1.0, 7.0, 0.3
## $ Serbia <dbl> 283.0, 131.0, 127.0, 9.6
## $ Seychelles <dbl> 157.0, 25.0, 51.0, 4.1
## $ Sierra.Leone <dbl> 25.0, 3.0, 2.0, 6.7
## $ Singapore <dbl> 60.0, 12.0, 11.0, 1.5
## $ Slovakia <dbl> 196.0, 293.0, 116.0, 11.4
## $ Slovenia <dbl> 270.0, 51.0, 276.0, 10.6
## $ Solomon.Islands <dbl> 56.0, 11.0, 1.0, 1.2
## $ Somalia <int> 0, 0, 0, 0
## $ South.Africa <dbl> 225.0, 76.0, 81.0, 8.2
## $ Spain <int> 284, 157, 112, 10
## $ Sri.Lanka <dbl> 16.0, 104.0, 0.0, 2.2
## $ Sudan <dbl> 8.0, 13.0, 0.0, 1.7
## $ Suriname <dbl> 128.0, 178.0, 7.0, 5.6
## $ Swaziland <dbl> 90.0, 2.0, 2.0, 4.7
## $ Sweden <dbl> 152.0, 60.0, 186.0, 7.2
## $ Switzerland <dbl> 185.0, 100.0, 280.0, 10.2
## $ Syria <int> 5, 35, 16, 1
## $ Tajikistan <dbl> 2.0, 15.0, 0.0, 0.3
## $ Thailand <dbl> 99.0, 258.0, 1.0, 6.4
## $ Macedonia <dbl> 106.0, 27.0, 86.0, 3.9
## $ Timor.Leste <dbl> 1.0, 1.0, 4.0, 0.1
## $ Togo <dbl> 36.0, 2.0, 19.0, 1.3
## $ Tonga <dbl> 36.0, 21.0, 5.0, 1.1
## $ Trinidad...Tobago <dbl> 197.0, 156.0, 7.0, 6.4
## $ Tunisia <dbl> 51.0, 3.0, 20.0, 1.3
## $ Turkey <dbl> 51.0, 22.0, 7.0, 1.4
## $ Turkmenistan <dbl> 19.0, 71.0, 32.0, 2.2
## $ Tuvalu <int> 6, 41, 9, 1
## $ Uganda <dbl> 45.0, 9.0, 0.0, 8.3
## $ Ukraine <dbl> 206.0, 237.0, 45.0, 8.9
## $ United.Arab.Emirates <dbl> 16.0, 135.0, 5.0, 2.8
## $ United.Kingdom <dbl> 219.0, 126.0, 195.0, 10.4
## $ Tanzania <dbl> 36.0, 6.0, 1.0, 5.7
## $ USA <dbl> 249.0, 158.0, 84.0, 8.7
## $ Uruguay <dbl> 115.0, 35.0, 220.0, 6.6
## $ Uzbekistan <dbl> 25.0, 101.0, 8.0, 2.4
## $ Vanuatu <dbl> 21.0, 18.0, 11.0, 0.9
## $ Venezuela <dbl> 333.0, 100.0, 3.0, 7.7
## $ Vietnam <int> 111, 2, 1, 2
## $ Yemen <dbl> 6.0, 0.0, 0.0, 0.1
## $ Zambia <dbl> 32.0, 19.0, 4.0, 2.5
## $ Zimbabwe <dbl> 64.0, 18.0, 4.0, 4.7
library(reshape)
##
## Attaching package: 'reshape'
## The following object is masked from 'package:dplyr':
##
## rename
df1<- melt(df,id="year")
df1 %>% glimpse
## Rows: 772
## Columns: 3
## $ year <int> 1999, 2000, 2001, 2002, 1999, 2000, 2001, 2002, 1999, 2000, 2…
## $ variable <fct> Afghanistan, Afghanistan, Afghanistan, Afghanistan, Albania, …
## $ value <dbl> 0.0, 0.0, 0.0, 0.0, 89.0, 132.0, 54.0, 4.9, 25.0, 0.0, 14.0, …
names(df1)[2:3] <- c("country","disease")
names(df1)
## [1] "year" "country" "disease"
df1 %>% filter(year==2000) %>% summarize(m=mean(disease))
## m
## 1 81.01036
df1 %>% filter(year==2000) %>% filter(disease>81.01036) %>%
NROW -> result
print(result)
## [1] 76
#11번문제 P169
df<-read.csv("facebook.csv")
df %>% glimpse
## Rows: 6,997
## Columns: 16
## $ status_id <chr> "246675545449582_1649696485147474", "24667554544958…
## $ status_type <chr> "video", "photo", "video", "photo", "photo", "photo…
## $ status_published <chr> "4/22/2018 6:00", "4/21/2018 22:45", "4/21/2018 6:1…
## $ num_reactions <int> 529, 150, 227, 111, 213, 217, 503, 295, 203, 170, 2…
## $ num_comments <int> 512, 0, 236, 0, 0, 6, 614, 453, 1, 9, 2, 4, 4, 4, 1…
## $ num_shares <int> 262, 0, 57, 0, 0, 0, 72, 53, 0, 1, 3, 0, 2, 0, 0, 3…
## $ num_likes <int> 432, 150, 204, 111, 204, 211, 418, 260, 198, 167, 2…
## $ num_loves <int> 92, 0, 21, 0, 9, 5, 70, 32, 5, 3, 7, 5, 6, 8, 10, 2…
## $ num_wows <int> 3, 0, 1, 0, 0, 1, 10, 1, 0, 0, 1, 4, 2, 1, 1, 1, 0,…
## $ num_hahas <int> 1, 0, 1, 0, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 5, 0, …
## $ num_sads <int> 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ num_angrys <int> 0, 0, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ react_comment_r <dbl> 1.0332031, 0.0000000, 0.9618644, 0.0000000, 0.00000…
## $ react_share_r <dbl> 2.019084, 0.000000, 3.982456, 0.000000, 0.000000, 0…
## $ postive_reactions <int> 527, 150, 226, 111, 213, 217, 498, 293, 203, 170, 2…
## $ negative_reactions <int> 2, 0, 1, 0, 0, 0, 5, 2, 0, 0, 0, 0, 0, 0, 0, 5, 0, …
colSums(is.na(df))
## status_id status_type status_published num_reactions
## 0 0 0 0
## num_comments num_shares num_likes num_loves
## 0 0 0 0
## num_wows num_hahas num_sads num_angrys
## 0 0 0 0
## react_comment_r react_share_r postive_reactions negative_reactions
## 120 117 0 0
df %>% mutate(ratio=(num_loves+num_wows)/(num_reactions)) %>%
filter(ratio<0.5&ratio>0.4) %>% filter(status_type == "video" ) %>%
NROW
## [1] 90
library(lubridate)
##
## Attaching package: 'lubridate'
## The following object is masked from 'package:reshape':
##
## stamp
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
df<-read.csv("netflix.csv")
df %>% glimpse
## Rows: 8,807
## Columns: 11
## $ show_id <chr> "s1", "s2", "s3", "s4", "s5", "s6", "s7", "s8", "s9", "s1…
## $ type <chr> "Movie", "TV Show", "TV Show", "TV Show", "TV Show", "TV …
## $ title <chr> "Dick Johnson Is Dead", "Blood & Water", "Ganglands", "Ja…
## $ director <chr> "Kirsten Johnson", "", "Julien Leclercq", "", "", "Mike F…
## $ cast <chr> "", "Ama Qamata, Khosi Ngema, Gail Mabalane, Thabang Mola…
## $ country <chr> "United States", "South Africa", "", "", "India", "", "",…
## $ date_added <chr> "25-Sep-21", "24-Sep-21", "24-Sep-21", "24-Sep-21", "24-S…
## $ release_year <int> 2020, 2021, 2021, 2021, 2021, 2021, 2021, 1993, 2021, 202…
## $ rating <chr> "PG-13", "TV-MA", "TV-MA", "TV-MA", "TV-MA", "TV-MA", "PG…
## $ duration <chr> "90 min", "2 Seasons", "1 Season", "1 Season", "2 Seasons…
## $ listed_in <chr> "Documentaries", "International TV Shows, TV Dramas, TV M…
df %>% filter(country=="United Kingdom") %>%
mutate(ymd=dmy(date_added)) %>% select(ymd)%>%
filter(ymd>='2018-01-01'&ymd<='2018-01-30')
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `ymd = dmy(date_added)`.
## Caused by warning:
## ! 15 failed to parse.
## ymd
## 1 2018-01-30
## 2 2018-01-18
## 3 2018-01-01
## 4 2018-01-15
## 5 2018-01-01
library(dplyr)
df<-read.csv("firestation.csv")
glimpse(df)
## Rows: 21,542
## Columns: 3
## $ 신고일시 <chr> "2018-01-02 11:45", "2018-01-04 01:56", "2018-01-05 16:54",…
## $ 출동일시 <chr> "2018-01-02 11:50", "2018-01-04 02:00", "2018-01-05 16:54",…
## $ 출동소방서 <chr> "양평소방서", "수원남부소방서", "김포소방서", "포천소방서",…
summary(df)
## 신고일시 출동일시 출동소방서
## Length:21542 Length:21542 Length:21542
## Class :character Class :character Class :character
## Mode :character Mode :character Mode :character
library(lubridate)
df$신고일시_1<-ymd_hm(df$신고일시)
df$출동일시_1<-ymd_hm(df$출동일시)
df$시간차<-difftime(df$출동일시_1,df$신고일시_1,units='secs')
df %>% group_by(출동소방서,year(신고일시_1),month(신고일시_1)) %>%
summarize(mean_time=mean(시간차)) %>% arrange(desc(mean_time))
## `summarise()` has grouped output by '출동소방서', 'year(신고일시_1)'. You can
## override using the `.groups` argument.
## # A tibble: 623 × 4
## # Groups: 출동소방서, year(신고일시_1) [54]
## 출동소방서 `year(신고일시_1)` `month(신고일시_1)` mean_time
## <chr> <dbl> <dbl> <drtn>
## 1 대화119안전센터 2018 9 3840.0000 secs
## 2 초월119안전센터 2018 7 2925.0000 secs
## 3 초월119안전센터 2018 6 2865.0000 secs
## 4 경기도소방재난본부 2018 8 2220.0000 secs
## 5 경기도소방재난본부 2018 10 1620.0000 secs
## 6 수원남부소방서 2018 1 1056.3830 secs
## 7 공흥119안전센터 2018 11 900.0000 secs
## 8 경기도소방재난본부 2018 9 840.0000 secs
## 9 양평소방서 2018 6 663.5294 secs
## 10 경기도소방재난본부 2018 5 660.0000 secs
## # ℹ 613 more rows
df1<- df %>% group_by(출동소방서,year(신고일시_1),month(신고일시_1)) %>%
summarize(mean_time=mean(시간차)) %>% arrange(desc(mean_time))
## `summarise()` has grouped output by '출동소방서', 'year(신고일시_1)'. You can
## override using the `.groups` argument.
df1$mean_time[[1]]/60
## [1] 64
df2<-df1$mean_time[[1]]/60
print(df2)
## [1] 64