Advertisement
JF11579

Jack.R

Apr 28th, 2020
1,052
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.26 KB | None | 0 0
  1.  
  2. getwd()
  3. options(scipen = 999) #this prevents conversion to scientific notation
  4.  
  5. # putting a HASHTAG or number sign prevents everything
  6. # to the right on that line from being run
  7.  
  8. '''
  9. For comments that are too long to be a single line
  10. one can insert 3 single quote marks like
  11. i did here.
  12. '''
  13.  
  14. library(tidyverse)
  15. library(lubridate)
  16. #library(readODS)
  17.  
  18.  
  19. ###############  DATA:  https://databank.worldbank.org/reports.aspx?source=world-development-indicators#
  20.  
  21. data<- read_csv("Data_2.csv")
  22. View(data)  # See the data in its entiruity
  23. dim(data)  # rowss & columns
  24. head(data, 5) #  see the first 5 rows.(Ommit 5 and defaults to 10)
  25. tail(data) # you guessed it
  26. glimpse(data)  # pointless option
  27. ls(data) #list of variables
  28.  
  29.  
  30. A<- data
  31. A<- data%>%
  32.      rename(variable = Years)
  33. #US_deaths<- US_deaths %>%
  34. #rename(Country = Entity ,Deaths = `Daily confirmed deaths (deaths)`)
  35.  
  36. skinny_2<- A%>%
  37.   gather("year" , "value", -variable)
  38.  
  39.  
  40.  
  41. skinny_1<- A%>%
  42.             gather("category" , "value", -Years)
  43.  
  44. skinny_1
  45. View(skinny_1)
  46.  
  47. #skinny_1 <- pivot_longer(data, c( 1960,1961,1962,1963,1964,1965,1966,1967,1968,
  48.                                  1969,1970,1971,1972,1973,1974,1975,1976,1977,1978,1979,1980))
  49.  
  50. #skinny_1<- pivot_longer(data, c(`1960` : `1980`))
  51.  
  52. #skinny_1 <- data%>%
  53.   mutate_if(is.numeric,as.character, is.factor, as.character) %>%
  54.   pivot_longer(  names_to = "key",
  55.                 values_to = "val")
  56.            
  57.  
  58. #small_diamonds %>%
  59.   small_diamonds %>%
  60.   mutate_if(is.numeric,as.character, is.factor, as.character) %>%
  61.   pivot_longer( - row_num,
  62.                 names_to = "key",
  63.                 values_to = "val")
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.   stocks <- tibble(
  71.     time = as.Date('2009-01-01') + 0:9,
  72.     X = rnorm(10, 0, 1),
  73.     Y = rnorm(10, 0, 2),
  74.     Z = rnorm(10, 0, 4)
  75.   )
  76. stocks
  77.  
  78. gather(stocks, "stock", "price", -time)
  79.  
  80.  
  81.  
  82. test<- read_csv("test_data.csv")
  83.  test$year <- as.Date(as.character(test$year) , format = "%Y")
  84.  test$pop <- as.numeric(test$pop)
  85.  test$city<- as.factor(test$city)
  86.  
  87.  
  88.  p<- ggplot(test, aes(year , pop, color = city, group = city))+
  89.      geom_line()
  90. p
  91.  
  92. str(test)
  93.  
  94. Boston<-
  95.     filter(test, city == "Boston")
  96.      
  97. Boston
  98.  
  99. p<- ggplot(Boston, aes(year , pop))+
  100.   geom_line()
  101. p
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement