library(tidyverse)
## -- Attaching packages ----------------------------------------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.2.1     v purrr   0.3.3
## v tibble  2.1.3     v dplyr   0.8.4
## v tidyr   1.0.2     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.4.0
## -- Conflicts -------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
df_wide <-read.csv2("https://raw.githubusercontent.com/johnsuh23/DATA-621/master/unicef-u5mr.csv",header = TRUE, sep = ",")
head(df_wide)
##         CountryName U5MR.1950 U5MR.1951 U5MR.1952 U5MR.1953 U5MR.1954 U5MR.1955
## 1       Afghanistan                                                            
## 2           Albania                                                            
## 3           Algeria                                               251     249.9
## 4           Andorra                                                            
## 5            Angola                                                            
## 6 Antigua & Barbuda                                                            
##   U5MR.1956 U5MR.1957 U5MR.1958 U5MR.1959 U5MR.1960 U5MR.1961 U5MR.1962
## 1                                                       356.5     350.6
## 2                                                                      
## 3       249       248     247.5     246.7     246.3     246.1     246.2
## 4                                                                      
## 5                                                                      
## 6                                                                      
##   U5MR.1963 U5MR.1964 U5MR.1965 U5MR.1966 U5MR.1967 U5MR.1968 U5MR.1969
## 1       345     339.7     334.1     328.7     323.3     318.1       313
## 2                                                                      
## 3     246.8     247.4     248.2     248.7     248.4     247.4     245.3
## 4                                                                      
## 5                                                                      
## 6                                                                      
##   U5MR.1970 U5MR.1971 U5MR.1972 U5MR.1973 U5MR.1974 U5MR.1975 U5MR.1976
## 1     307.8     302.1     296.4     290.8     284.9     279.4     273.6
## 2                                                                      
## 3     241.7     236.5       230     222.5     214.2       205     195.2
## 4                                                                      
## 5                                                                      
## 6                                                                      
##   U5MR.1977 U5MR.1978 U5MR.1979 U5MR.1980 U5MR.1981 U5MR.1982 U5MR.1983
## 1     267.8     261.6     255.5     249.1     242.7     236.2     229.7
## 2                91.1      84.7      78.6        73      67.8      62.8
## 3     184.9     173.8     161.8     148.1     132.5     115.8      99.2
## 4                                                                      
## 5                                   234.1     232.8     231.5     230.2
## 6                                                                      
##   U5MR.1984 U5MR.1985 U5MR.1986 U5MR.1987 U5MR.1988 U5MR.1989 U5MR.1990
## 1     222.9       216     209.2     202.1       195     187.8       181
## 2      58.3      54.3      50.7      47.6      44.9      42.5      40.6
## 3      83.8      71.2      61.9      55.4      51.2      48.5      46.8
## 4                                                                   8.5
## 5     229.1     228.3     227.5     226.9     226.5     226.2       226
## 6                                                                  25.5
##   U5MR.1991 U5MR.1992 U5MR.1993 U5MR.1994 U5MR.1995 U5MR.1996 U5MR.1997
## 1     174.2     167.8       162     156.8     152.3     148.6     145.5
## 2      38.8      37.3        36      34.6      33.2      31.8      30.3
## 3      45.7      44.9      44.1      43.3      42.5      41.8      41.1
## 4       7.9       7.4       6.9       6.4         6       5.7       5.3
## 5     225.9       226     225.8     225.5     224.8       224     222.6
## 6      24.2      23.1      21.9      20.8      19.7      18.8      17.9
##   U5MR.1998 U5MR.1999 U5MR.2000 U5MR.2001 U5MR.2002 U5MR.2003 U5MR.2004
## 1     142.6     139.9       137     133.8     130.3     126.8     123.2
## 2      28.9      27.5      26.2      24.9      23.6      22.5      21.5
## 3      40.6      40.2      39.7      38.9      37.8      36.5      35.1
## 4         5       4.8       4.6       4.4       4.2       4.1         4
## 5     220.8     218.9     216.7     214.1     211.7     209.2     206.7
## 6        17      16.2      15.5      14.8      14.1      13.5      12.9
##   U5MR.2005 U5MR.2006 U5MR.2007 U5MR.2008 U5MR.2009 U5MR.2010 U5MR.2011
## 1     119.6     116.3     113.2     110.4     107.6       105     102.3
## 2      20.5      19.5      18.7      17.9      17.3      16.6        16
## 3      33.6      32.1      30.7      29.4      28.3      27.3      26.6
## 4       3.9       3.7       3.6       3.5       3.4       3.3       3.2
## 5     203.9     200.5     196.4       192     187.3     182.5     177.3
## 6      12.4      11.8      11.3      10.9      10.4       9.9       9.5
##   U5MR.2012 U5MR.2013 U5MR.2014 U5MR.2015
## 1      99.5      96.7      93.9      91.1
## 2      15.5      14.9      14.4        14
## 3      26.1      25.8      25.6      25.5
## 4       3.1         3       2.9       2.8
## 5     172.2     167.1     162.2     156.9
## 6       9.1       8.7       8.4       8.1
df_long <- gather(df_wide, year, measurement, U5MR.1950:U5MR.2015, factor_key=TRUE)
## Warning: attributes are not identical across measure variables;
## they will be dropped
df_long$year<-str_remove_all(df_long$year,"U5MR.")
df_long<-filter(df_long,measurement !="")
head(df_long)
##          CountryName year measurement
## 1          Australia 1950        31.6
## 2             Canada 1950        48.7
## 3              Benin 1950       348.2
## 4            Denmark 1950        34.1
## 5 Dominican Republic 1950         156
## 6               Fiji 1950       135.7