SHARE
TWEET

Untitled

a guest Oct 9th, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Some vectors of numbers
  2. percent_change  <- c(4, -1.91, 3.00, -5.002)
  3. income <-  c(72.19, 1030.18, 10291.93, 1189192.18)
  4. p_values <- c(0.12, 0.98, 0.0000191, 0.00000000002)
  5.  
  6. # Format c(0.0011, 0.011, 1) with digits = 1
  7. format(c(0.0011, 0.011, 1), digits =1)
  8.  
  9. # Format c(1.0011, 2.011, 1) with digits = 1
  10. format(c(1.0011, 2.011, 1), digits=1)
  11.  
  12. # Format percent_change to one place after the decimal point
  13. format(percent_change, digits=2)
  14.  
  15. # Format income to whole numbers
  16. format(income, digits=2)
  17.  
  18. # Format p_values in fixed format
  19. format(p_values, scientific=FALSE)
  20.  
  21. ## 2
  22. formatted_income <- format(income, digits = 2)
  23.  
  24. # Print formatted_income
  25. print(formatted_income)
  26.  
  27. # Call writeLines() on the formatted income
  28. writeLines(formatted_income)
  29.  
  30. # Define trimmed_income
  31. trimmed_income <- format(income, digits=2, trim=TRUE)
  32.  
  33. # Call writeLines() on the trimmed_income
  34. writeLines(trimmed_income)
  35.  
  36. # Define pretty_income
  37. pretty_income <- format(income, digits = 2, big.mark=",")
  38.  
  39. # Call writeLines() on the pretty_income
  40. writeLines(pretty_income)
  41.  
  42.  
  43. #Rather than a scientific argument, formatC() has a format argument that takes a code representing the required format. The most useful are:
  44.  
  45. #"f" for fixed,
  46. #"e" for scientific, and
  47. #"g" for fixed unless scientific saves space
  48.  
  49. # From the format() exercise
  50. x <- c(0.0011, 0.011, 1)
  51. y <- c(1.0011, 2.011, 1)
  52.  
  53. # formatC() on x with format = "f", digits = 1
  54. formatC(x, format='f', digits=1)
  55.  
  56. # formatC() on y with format = "f", digits = 1
  57. formatC(y, format ="f", digits=1)
  58.  
  59. # Format percent_change to one place after the decimal point
  60. formatC(percent_change, format='f',digits=1)
  61.  
  62. # percent_change with flag = "+"
  63. formatC(percent_change, format='f',digits=1, flag="+")
  64.  
  65. # Format p_values using format = "g" and digits = 2
  66. formatC(p_values, format="g", digits=2)
  67.  
  68. # Add $ to pretty_income
  69. paste("$",pretty_income,sep='' )
  70.  
  71. # Add % to pretty_percent
  72. paste(pretty_percent, "%", sep="")
  73.  
  74. # Create vector with elements like 2010: +4.0%`
  75. year_percent <- paste(years, ': ', pretty_percent, "%", sep="")
  76.  
  77. # Collapse all years into single string
  78. paste(year_percent, collapse=', ')
  79.  
  80. ## 3
  81. # Define the names vector
  82. income_names <- c("Year 0", "Year 1", "Year 2", "Project Lifetime")
  83.  
  84. # Create pretty_income
  85. pretty_income <- format(income, digits=2, big.mark=",")
  86.  
  87. # Create dollar_income
  88. dollar_income <- paste("$", pretty_income, sep="")
  89.  
  90. # Create formatted_names
  91. formatted_names <- format(income_names, justify="right")
  92.  
  93. # Create rows
  94. rows<-paste(formatted_names, dollar_income, sep="   ")
  95.  
  96. # Write rows
  97. writeLines(rows)
  98.  
  99. ## 3
  100. # Randomly sample 3 toppings
  101. my_toppings <- sample(toppings, size = 3)
  102.  
  103. # Print my_toppings
  104. my_toppings
  105.  
  106. # Paste "and " to last element: my_toppings_and
  107. my_toppings_and <- paste(c("", "", "and "), my_toppings, sep = "")
  108.  
  109. # Collapse with comma space: these_toppings
  110. these_toppings <- paste(my_toppings_and, collapse = ", ")
  111.  
  112. # Add rest of sentence: my_order
  113. my_order <- paste("I want to order a pizza with ", these_toppings, ".", sep = "")
  114.  
  115. # Order pizza with writeLines()
  116. writeLines(my_order)
  117.  
  118. ## stringr
  119. library(stringr)
  120.  
  121. my_toppings <- c("cheese", NA, NA)
  122. my_toppings_and <- paste(c("", "", "and "), my_toppings, sep = "")
  123.  
  124. # Print my_toppings_and
  125. print(my_toppings_and)
  126.  
  127. # Use str_c() instead of paste(): my_toppings_str
  128. my_toppings_str <- str_c(c("", "", "and "), my_toppings)
  129.  
  130. # Print my_toppings_str
  131. print(my_toppings_str)
  132.  
  133. # paste() my_toppings_and with collapse = ", "
  134. paste(my_toppings_and, collapse = ", ")
  135.  
  136. # str_c() my_toppings_str with collapse = ", "
  137. str_c(my_toppings_str, collapse = ", ")
  138.  
  139. ##
  140. library(stringr)
  141. library(babynames)
  142. library(dplyr)
  143.  
  144. # Extracting vectors for boys' and girls' names
  145. babynames_2014 <- filter(babynames, year == 2014)
  146. boy_names <- filter(babynames_2014, sex == "M")$name
  147. girl_names <- filter(babynames_2014, sex == "F")$name
  148.  
  149. # Take a look at a few boy_names
  150. head(boy_names)
  151.  
  152. # Find the length of all boy_names
  153. boy_length <- str_length(boy_names)
  154.  
  155. # Take a look at a few lengths
  156. head(boy_length)
  157.  
  158. # Find the length of all girl_names
  159. girl_length <- str_length(girl_names)
  160.  
  161. # Find the difference in mean length
  162. mean(girl_length) - mean(boy_length)
  163.  
  164. # Confirm str_length() works with factors
  165. head(str_length(factor(boy_names)))
  166.  
  167. #####
  168. # Extract first letter from boy_names
  169. boy_first_letter <- str_sub(boy_names, 1,1)
  170.  
  171. # Tabulate occurrences of boy_first_letter
  172. table(boy_first_letter)
  173.  
  174. # Extract the last letter in boy_names, then tabulate
  175. boy_last_letter <- str_sub(boy_names, -1,-1)
  176. table(boy_last_letter)
  177.  
  178. # Extract the first letter in girl_names, then tabulate
  179. girl_first_letter <- str_sub(girl_names, 1,1)
  180. table(girl_first_letter)
  181.  
  182. # Extract the last letter in girl_names, then tabulate
  183. girl_last_letter <- str_sub(girl_names, -1,-1)
  184. table(girl_last_letter)
  185.  
  186.  
  187. ####
  188. # Look for pattern "zz" in boy_names
  189. contains_zz <- str_detect(boy_names, pattern="zz")
  190.  
  191. # Examine str() of contains_zz
  192. str(contains_zz)
  193.  
  194. # How many names contain "zz"?
  195. sum(contains_zz)
  196.  
  197. # Which names contain "zz"?
  198. boy_names[contains_zz]
  199.  
  200. # Which rows in boy_df have names that contain "zz"?
  201. boy_df[contains_zz,]
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top