#Assignment 2#

#####################################################################################################################
# Session 3
#####################################################################################################################

# Task 1.1 - Check working directory and import the following files :
              ## "cities.csv"
              ## "movehubcostofliving.csv"
              ## "movehubqualityoflife.csv"

library(tidyr)
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
library(Hmisc)
## Loading required package: lattice
## Loading required package: survival
## Loading required package: Formula
## Loading required package: ggplot2
## 
## Attaching package: 'Hmisc'
## The following objects are masked from 'package:dplyr':
## 
##     src, summarize
## The following objects are masked from 'package:base':
## 
##     format.pval, units
library(tidyverse)
## -- Attaching packages -------------------------------------------------------------------------- tidyverse 1.2.1 --
## v tibble  2.1.1     v purrr   0.3.2
## v readr   1.3.1     v stringr 1.4.0
## v tibble  2.1.1     v forcats 0.4.0
## -- Conflicts ----------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter()    masks stats::filter()
## x dplyr::lag()       masks stats::lag()
## x Hmisc::src()       masks dplyr::src()
## x Hmisc::summarize() masks dplyr::summarize()
library(plyr)
## -------------------------------------------------------------------------
## You have loaded plyr after dplyr - this is likely to cause problems.
## If you need functions from both plyr and dplyr, please load plyr first, then dplyr:
## library(plyr); library(dplyr)
## -------------------------------------------------------------------------
## 
## Attaching package: 'plyr'
## The following object is masked from 'package:purrr':
## 
##     compact
## The following objects are masked from 'package:Hmisc':
## 
##     is.discrete, summarize
## The following objects are masked from 'package:dplyr':
## 
##     arrange, count, desc, failwith, id, mutate, rename, summarise,
##     summarize
library(forecast)

setwd("E:\\Assignment R\\city_rankings_elons_tweets_dataset")
cities <- read.csv("E:\\Assignment R\\city_rankings_elons_tweets_dataset\\cities.csv")
cost_of_living <- read.csv("E:\\Assignment R\\city_rankings_elons_tweets_dataset\\movehubcostofliving.csv")
quality_of_life <- read.csv("E:\\Assignment R\\city_rankings_elons_tweets_dataset\\movehubqualityoflife.csv")

# Task 1.2 - Find the country with the highest mean of Avg Rent

cities_data <- cities %>% inner_join(cost_of_living, by = "City")
## Warning: Column `City` joining factors with different levels, coercing to
## character vector
cities_data2 <- cities_data %>% inner_join(quality_of_life, by = "City")
## Warning: Column `City` joining character vector and factor, coercing into
## character vector
max(cities_data2$Avg.Rent)
## [1] 5052.31
# Task 1.3 - Find the countries where Health.Care >60 and Crime.Rating < 40 

Countries <- count(cities_data2%>%filter(cities_data2$Health.Care>60,cities_data2$Crime.Rating<40))
count(Countries)
## Using freq as weighting variable
##           City              Country Cappuccino Cinema  Wine Gasoline
## 1         Oslo               Norway       3.36  11.20 12.32     1.57
## 2       Ottawa               Canada       2.39   7.65  9.56     0.80
## 3       Oxford       United Kingdom       2.49   7.97  7.97     1.36
## 4       Madrid                Spain       1.70   6.82  3.92     1.24
## 5       Manama              Bahrain       2.60   5.20 19.61     0.17
## 6   Manchester       United Kingdom       2.40   8.00  6.00     1.15
## 7     Montreal               Canada       1.91   7.65  9.56     0.89
## 8       Munich              Germany       2.30   7.67  5.11     1.36
## 9        Seoul          South Korea       2.59   5.25 11.67     1.17
## 10    Shanghai                China       2.96   8.46  8.46     0.85
## 11   Stavanger               Norway       4.48  10.65 13.44     1.68
## 12   Stockholm               Sweden       3.00  11.01  7.91     1.46
## 13   Stuttgart              Germany       2.13   6.82  4.26     1.32
## 14      Sydney            Australia       2.35  12.10 10.75     1.00
## 15   Nashville        United States       3.84  12.00 13.50     0.65
## 16      Newark        United States       2.45   7.19 10.46     0.67
## 17     Nicosia               Cyprus       3.41   6.82  4.69     1.20
## 18      Warsaw               Poland       1.66   5.19  5.19     1.18
## 19  Wellington          New Zealand       2.20   8.82  8.27     1.17
## 20     Calgary               Canada       2.55   8.29  9.56     0.74
## 21   Cambridge        United States       1.99   8.22  7.97     1.39
## 22   Cambridge               Canada       1.99   8.22  7.97     1.39
## 23   Cambridge       United Kingdom       1.99   8.22  7.97     1.39
## 24   Charlotte        United States       2.15   5.23  7.19     0.60
## 25     Chennai                India       0.66   1.81  4.83     0.87
## 26  Chiang Mai             Thailand       1.05   3.53 11.40     0.82
## 27     Cologne              Germany       1.92   7.25  3.62     1.32
## 28  Copenhagen              Denmark       3.66   9.15  5.72     1.40
## 29        Cork              Ireland       2.13   7.08  8.52     1.36
## 30    Vadodara                India       0.72   2.17  6.03     0.84
## 31    Valencia                Spain       1.28   6.82  4.26     1.24
## 32    Valencia          Philippines       1.28   6.82  4.26     1.24
## 33    Valencia            Venezuela       1.28   6.82  4.26     1.24
## 34   Vancouver        United States       2.55   7.97 12.75     0.89
## 35   Vancouver               Canada       2.55   7.97 12.75     0.89
## 36      Venice                Italy       1.28   6.82  3.41     1.40
## 37      Vienna              Austria       2.13   7.67  4.26     1.25
## 38     Vilnius            Lithuania       1.23   4.44  4.44     1.19
## 39       Porto             Portugal       0.68   4.26  3.41     1.37
## 40      Prague       Czech Republic       1.32   5.08  3.29     1.18
## 41       Kochi                India       0.60   1.81  3.62     0.84
## 42      Taipei               Taiwan       1.75   6.01  8.14     0.77
## 43   The Hague          Netherlands       2.26   8.10  4.05     1.52
## 44       Tokyo                Japan       2.30  11.80  8.52     0.98
## 45     Toronto               Canada       2.23   8.29  9.50     0.83
## 46   Trondheim               Norway       3.81  12.32 13.44     1.57
## 47    Florence                Italy       1.02   6.82  4.26     1.53
## 48     Utrecht          Netherlands       2.13   6.82  4.26     1.45
## 49  Gothenburg               Sweden       3.30  10.76  8.01     1.44
## 50   Edinburgh       United Kingdom       2.09   7.97  6.73     1.35
## 51    Edmonton               Canada       2.55   8.29 11.16     0.69
## 52     Bangkok             Thailand       1.37   4.10 13.68     0.91
## 53       Basel          Switzerland       3.50  11.89  7.35     1.25
## 54      Bergen               Norway       3.92  12.32 12.32     1.57
## 55      Berlin              Germany       1.88   6.82  3.84     1.36
## 56       Braga             Portugal       1.02   4.48  2.13     1.39
## 57    Budapest              Hungary       1.00   4.27  2.85     1.20
## 58     Hamburg              Germany       1.83   9.38  3.41     1.36
## 59    Hamilton          New Zealand       1.91   7.01  8.29     0.80
## 60    Hamilton               Canada       1.91   7.01  8.29     0.80
## 61   Hong Kong            Hong Kong       2.78   5.89 10.10     1.52
## 62   Hyderabad                India       0.72   1.81  4.83     0.91
## 63   Hyderabad             Pakistan       0.72   1.81  4.83     0.91
## 64    Lausanne          Switzerland       3.15  12.59  8.40     1.32
## 65       Leeds       United Kingdom       1.99   7.60  6.33     1.38
## 66   Leicester       United Kingdom       2.49   7.22  5.27     1.43
## 67    Limassol               Cyprus       2.98   6.82  4.26     1.15
## 68   Liverpool       United Kingdom       1.99   7.67  5.98     1.35
## 69   Ljubljana             Slovenia       1.28   4.69  4.26     1.27
## 70      Darwin            Australia       3.36  10.08 10.08     1.04
## 71        Doha                Qatar       2.78   6.28 14.32     0.18
## 72     Dresden              Germany       2.13   6.82  3.84     1.33
## 73       Dubai United Arab Emirates       2.62   6.23 12.77     0.31
## 74      Aachen              Germany       2.05   6.88  4.26     1.33
## 75    Aberdeen       United Kingdom       1.99   6.98  5.98     1.37
## 76 Addis Ababa             Ethiopia       0.46   2.29  4.18     0.72
## 77   Ahmedabad                India       0.72   2.11  4.22     0.85
## 78   Amsterdam          Netherlands       2.09   8.52  4.26     1.45
##    Avg.Rent Avg.Disposable.Income Movehub.Rating Purchase.Power
## 1   2016.66               2800.92          82.09          52.51
## 2   1020.02               2900.68          87.69          91.85
## 3   1494.67               1693.96          80.94          50.33
## 4   1193.48               1278.72          85.37          54.07
## 5   1078.72               1176.78          77.56          40.26
## 6   1200.00               1388.55          81.89          62.31
## 7    956.27               1785.03          89.28          66.99
## 8   1278.72               2045.96          86.00          63.28
## 9   1458.20               1458.20          82.43          54.30
## 10  1533.38                592.20          75.69          26.74
## 11  2240.74               2957.77          79.41          46.59
## 12  1501.88               2002.51          82.85          51.03
## 13   980.35               1943.66          82.48          65.82
## 14  2788.71               2755.12          94.53          54.82
## 15  2257.14               3089.75          80.61          80.30
## 16   980.65               2402.60          84.97          84.39
## 17   639.36               1065.60          78.76          39.03
## 18   726.59                664.31          76.76          35.77
## 19  1515.65               1763.67          81.06          49.11
## 20  1115.65               2231.29          85.77          63.90
## 21  1345.20               2730.26          82.15          54.76
## 22  1345.20               2730.26          82.15          54.76
## 23  1345.20               2730.26          82.15          54.76
## 24   915.28               2073.10          84.46          77.18
## 25   301.69                301.69          78.12          32.91
## 26   426.24                227.95          68.64          15.89
## 27   980.35               1704.96          82.18          59.18
## 28  1658.01               2001.04          82.85          49.33
## 29  1022.98               1633.36          83.55          47.98
## 30   120.68                307.73          76.54          36.37
## 31   596.74               1193.48          81.06          49.11
## 32   596.74               1193.48          81.06          49.11
## 33   596.74               1193.48          81.06          49.11
## 34  1848.79               1657.53          82.59          48.06
## 35  1848.79               1657.53          82.59          48.06
## 36  1278.72               1875.46          78.82          45.69
## 37  1248.90               1619.72          81.84          51.21
## 38   493.78                457.29          72.45          24.17
## 39   596.74                664.94          76.17          31.28
## 40   821.94                723.30          76.64          35.32
## 41   181.02                271.52          74.27          28.98
## 42   784.52                942.66          77.42          38.19
## 43  1363.97               2386.95          83.23          68.28
## 44  1967.31               2065.67          80.00          47.57
## 45  1593.78               1912.54          88.42          61.44
## 46  1680.55               2800.92          80.36          51.22
## 47  1278.72               1278.72          76.63          35.31
## 48  1193.48               1611.19          79.70          49.87
## 49  1001.25               2302.88          74.63          26.95
## 50   996.45               1992.89          83.92          58.77
## 51  1147.52               2199.42          85.94          67.19
## 52  1139.76                455.91          74.21          20.82
## 53  1649.29               3847.76          84.20          78.17
## 54  1725.37               3002.59          80.74          51.24
## 55   916.42               1772.57          89.54          68.72
## 56   383.62                596.74          75.07          30.21
## 57   341.97                414.64          74.13          24.59
## 58  1534.47               1747.59          84.66          61.13
## 59   605.64               1593.78          81.34          56.52
## 60   605.64               1593.78          81.34          56.52
## 61  5052.31               2210.39          86.37          50.07
## 62   241.35                301.69          79.35          39.93
## 63   241.35                301.69          79.35          39.93
## 64  1714.00               4266.11          87.21          90.77
## 65   797.16               1992.89          82.57          63.88
## 66   787.19               1793.60          83.36          71.88
## 67   618.05                937.73          78.30          40.13
## 68   896.80               1534.47          81.23          53.78
## 69   767.23                809.86          75.35          32.28
## 70  2015.94               2435.93          79.63          49.51
## 71  2221.74               2775.58          90.73          81.96
## 72  1193.48               2088.58          84.75          82.60
## 73  1981.57               2313.91          98.44          69.64
## 74   767.23               1619.72          81.64          60.55
## 75  1195.74               1743.78          81.89          49.70
## 76   653.77                124.22          59.88           6.38
## 77   193.08                301.69          76.16          33.69
## 78  1513.16               1747.59          84.00          47.18
##    Health.Care Pollution Quality.of.Life Crime.Rating freq freq.1
## 1        88.19     29.39           71.27        35.53    1      1
## 2        66.02     33.55           86.11        22.25    1      1
## 3        66.20     11.48           72.09        24.22    1      1
## 4        73.51     55.77           59.87        39.34    1      1
## 5        72.22     17.06           63.85        19.79    1      1
## 6        61.42      0.00           73.00        24.20    1      1
## 7        66.77      6.27           78.55        31.63    1      1
## 8        88.43     43.08           90.08        15.34    1      1
## 9        75.00     85.59           60.28        21.35    1      1
## 10       78.70     61.74           31.66        16.51    1      1
## 11       62.48     29.13           76.50        20.83    1      1
## 12       82.50     13.93           78.58        25.62    1      1
## 13       80.38     16.89           90.40        23.96    1      1
## 14       71.27     18.48           74.32        32.80    1      1
## 15       60.30      0.00           80.50        25.50    1      1
## 16       79.72     62.14           73.21        30.21    1      1
## 17       72.04      6.27           57.58        38.22    1      1
## 18       63.33     86.16           51.82        32.03    1      1
## 19       73.72     30.55           79.83        27.38    1      1
## 20       75.33     23.53           87.57        29.53    1      1
## 21       81.48     57.18           70.61        24.22    1      1
## 22       81.48     57.18           70.61        24.22    1      1
## 23       81.48     57.18           70.61        24.22    1      1
## 24       72.08     67.05           84.39        30.21    1      1
## 25       67.49     78.07           43.89        33.22    1      1
## 26       85.91     30.55           37.50        37.78    1      1
## 27       67.88     68.41           82.82        27.08    1      1
## 28       85.14     30.54           75.23        32.39    1      1
## 29       64.32     18.48           76.37        35.83    1      1
## 30       77.87     92.42           54.98        32.03    1      1
## 31       72.07     35.24           64.89        26.04    1      1
## 32       72.07     35.24           64.89        26.04    1      1
## 33       72.07     35.24           64.89        26.04    1      1
## 34       83.73     18.48           71.89        30.03    1      1
## 35       83.73     18.48           71.89        30.03    1      1
## 36       72.67     11.48           70.34        21.48    1      1
## 37       79.86     22.39           77.21        27.45    1      1
## 38       77.31     82.08           64.19        27.93    1      1
## 39       61.51     67.64           58.95        37.51    1      1
## 40       64.15     41.53           58.85        34.54    1      1
## 41       70.23     71.78           45.13        16.93    1      1
## 42       88.89     87.62           52.35        15.71    1      1
## 43       66.74     37.21           79.99        29.76    1      1
## 44       71.53     30.54           69.29        13.91    1      1
## 45       66.11      8.95           77.02        27.40    1      1
## 46       81.67     14.53           82.67        19.80    1      1
## 47       67.11     29.44           53.73        39.58    1      1
## 48       70.90     18.27           72.61        29.76    1      1
## 49       68.24     92.42           71.51        25.62    1      1
## 50       60.64     13.94           75.03        30.51    1      1
## 51       78.83     22.79           89.58        31.14    1      1
## 52       95.96     60.39           37.54        36.10    1      1
## 53       79.74     59.18           88.27        28.12    1      1
## 54       84.95     18.40           83.76        16.67    1      1
## 55       65.48     16.05           91.17        24.18    1      1
## 56       69.71     14.54           69.91        18.12    1      1
## 57       60.42     68.38           46.58        34.18    1      1
## 58       82.41     67.78           87.18        25.52    1      1
## 59       86.67     81.68           84.83        35.68    1      1
## 60       86.67     81.68           84.83        35.68    1      1
## 61       67.59     14.88           59.50        16.31    1      1
## 62       63.89     89.72           54.97        36.90    1      1
## 63       63.89     89.72           54.97        36.90    1      1
## 64       65.85     87.62           73.21        35.55    1      1
## 65       81.48     18.48           78.06        28.91    1      1
## 66       60.64     18.04           76.19        24.22    1      1
## 67       77.60     26.74           74.83        23.75    1      1
## 68       82.86     55.35           83.14        28.70    1      1
## 69       71.76     85.59           61.87        32.94    1      1
## 70       75.28      6.78           78.52        32.80    1      1
## 71       68.06     83.45           80.28        27.17    1      1
## 72       78.29     16.89           90.21        15.34    1      1
## 73       67.78     30.81           85.16        19.36    1      1
## 74       73.25     11.69           90.52        15.34    1      1
## 75       82.86     34.31           76.77        24.22    1      1
## 76       63.89     85.59           28.41        26.04    1      1
## 77       61.67     68.21           57.01        18.18    1      1
## 78       68.06     53.42           72.85        29.76    1      1
# Task 1.4 - Rank the Countries by average Quality of Life, where average pollution 
#            and average Crime Rating are less than the mean (for all cities of all countries combined), 
#            and mean of Avg Disposable Income > 2000


# Task 1.5 - Rank the countries separetly based on mean of Gasoline price, Health care, Pollution, Avg Rent and 
#            Avg Disposable Income. Then find mean of these ranks for each country and order the countries by it. 
            





#####################################################################################################################
# Session 4
#####################################################################################################################

# Task 1.1 - Check working directory and import the following files :
#             "elonmusk_tweets.csv"

setwd("E:\\Assignment R\\city_rankings_elons_tweets_dataset")

# Task 1.2 - Find count of Elon's tweets by Session of the week

tweets <- read.csv("elonmusk_tweets.csv")

tweets<-Sys.time()
p<-as.POSIXlt(tweets)
p
## [1] "2020-01-26 21:27:18 IST"
names(unclass(p))
##  [1] "sec"    "min"    "hour"   "mday"   "mon"    "year"   "wday"  
##  [8] "yday"   "isdst"  "zone"   "gmtoff"
p$hour
## [1] 21
p$sec
## [1] 18.67306
count1 <- strftime(tweets,"%W")
count1
## [1] "03"
# Task 1.3 - Find count of Elon's tweets by Hour of Session 
count2 <- strftime(tweets,"%H")
count2
## [1] "21"
# TAsk 1.4 - Find count of Elon's tweets by By year and Month

count3 <- strftime(tweets,"%B")
count3
## [1] "January"
# Task 1.5 - Fid the weekSessions with the word 'u' in them


# Task 1.6 - Find tweets with the word tesla in it

# Task 1.7 - Find the number of times the word 'human' has been mentioned in his tweets. 
#            Then find the % of times it was mentioned on a 'WednesSession'