Advertisement
Guest User

Untitled

a guest
Aug 13th, 2017
678
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 13.56 KB | None | 0 0
  1. RESPOSTAS DATACAMP
  2. COMO USAR:
  3. OLHAR O LINK DA PÁGINA.
  4.  
  5. https://campus.datacamp.com/courses/intermediate-r/chapter-4-the-apply-family?ex=3
  6.  
  7. CAPTIULO 4 EXERCICIO 3, USAR ENTAO BUSCA POR 4/3
  8. FÁCIL DEMAIS NÉ
  9.  
  10.  
  11.  
  12. # Comparison of logicals
  13. TRUE == FALSE
  14.  
  15. # Comparison of numerics
  16. (-6 * 14) != (17 - 101)
  17.  
  18. # Comparison of character strings
  19. "useR" == "user"
  20.  
  21. # Compare a logical with a numeric
  22. 1 == TRUE
  23.  
  24. 1/3
  25. # Comparison of numerics
  26. (-6 *5 + 2) >= (-10+1)
  27.  
  28. # Comparison of character strings
  29.  
  30. "raining" <= "raining dogs"
  31.  
  32. # Comparison of logicals
  33. TRUE > FALSE
  34.  
  35. 1/4
  36.  
  37. # The linkedin and facebook vectors have already been created for you
  38. linkedin <- c(16, 9, 13, 5, 2, 17, 14)
  39. facebook <- c(17, 7, 5, 16, 8, 13, 14)
  40.  
  41. # Popular days
  42.  
  43. linkedin > 15
  44.  
  45. # Quiet days
  46.  
  47. linkedin <= 5
  48.  
  49. # LinkedIn more popular than Facebook
  50.  
  51. linkedin > facebook
  52.  
  53. 1/5
  54. # The social data has been created for you
  55. linkedin <- c(16, 9, 13, 5, 2, 17, 14)
  56. facebook <- c(17, 7, 5, 16, 8, 13, 14)
  57. views <- matrix(c(linkedin, facebook), nrow = 2, byrow = TRUE)
  58.  
  59. # When does views equal 13?
  60. views == 13
  61.  
  62. # When is views less than or equal to 14?
  63.  
  64. views <= 14
  65.  
  66.  
  67. 1/7
  68. # The linkedin and last variable are already defined for you
  69. linkedin <- c(16, 9, 13, 5, 2, 17, 14)
  70. last <- tail(linkedin, 1)
  71.  
  72. # Is last under 5 or above 10?
  73.  
  74. last < 5 | last > 10
  75.  
  76. # Is last between 15 (exclusive) and 20 (inclusive)?
  77.  
  78. last > 15 & last <=20
  79.  
  80.  
  81.  
  82. 1/8
  83.  
  84. # The social data (linkedin, facebook, views) has been created for you
  85.  
  86. # linkedin exceeds 10 but facebook below 10
  87. (linkedin > 10) & (facebook < 10)
  88.  
  89. # When were one or both visited at least 12 times?
  90. (linkedin >= 12) | (facebook >=12)
  91.  
  92.  
  93. # When is views between 11 (exclusive) and 14 (inclusive)?
  94.  
  95. views > 11 & views <= 14
  96.  
  97. 1/10
  98. # li_df is pre-loaded in your workspace
  99.  
  100. # Select the second column, named day2, from li_df: second
  101.  
  102. second = li_df[,2]
  103. # Build a logical vector, TRUE if value in second is extreme: extremes
  104.  extremes <- (second > 25) | second<5
  105.  
  106. # Count the number of TRUEs in extremes
  107. sum(extremes)
  108.  
  109. # Solve it with a one-liner
  110.  
  111. 1/12
  112. # Variables related to your last day of recordings
  113. medium <- "LinkedIn"
  114. num_views <- 14
  115.  
  116. # Examine the if statement for medium
  117. if (medium == "LinkedIn") {
  118.   print("Showing LinkedIn information")
  119. }
  120.  
  121. # Write the if statement for num_views
  122.  
  123. if(num_views > 15)
  124. {
  125.   print("You're popular!")
  126. }
  127.  
  128. 1/13
  129. # Variables related to your last day of recordings
  130. medium <- "LinkedIn"
  131. num_views <- 14
  132.  
  133. # Control structure for medium
  134. if (medium == "LinkedIn") {
  135.   print("Showing LinkedIn information")
  136. }
  137. else{
  138.   print("Unknown medium")
  139. }
  140.  
  141.  
  142. # Control structure for num_views
  143. if (num_views > 15) {
  144.   print("You're popular!")
  145. }
  146. else{
  147.   print("Try to be more visible!")
  148. }
  149.  
  150. 1/14
  151. # Variables related to your last day of recordings
  152. medium <- "LinkedIn"
  153. num_views <- 14
  154.  
  155. # Control structure for medium
  156. if (medium == "LinkedIn") {
  157.   print("Showing LinkedIn information")
  158. } else if (medium == "Facebook") {
  159.   print("Showing Facebook information")
  160. } else {
  161.   print("Unknown medium")
  162. }
  163.  
  164. # Control structure for num_views
  165. if (num_views > 15) {
  166.   print("You're popular!")
  167. } else if (num_views <= 15 & num_views > 10) {
  168.   print("Your number of views is average")
  169. } else {
  170.   print("Try to be more visible!")
  171. }
  172.  
  173. 1/16
  174. # Variables related to your last day of recordings
  175. li <- 15
  176. fb <- 9
  177.  
  178. # Code the control-flow construct
  179. if (li >= 15 & fb >=15) {
  180.   sms <- 2 * (li + fb)
  181. } else if(li < 10 & fb < 10) {
  182.   sms <- 0.5 * (li + fb)
  183. } else {
  184.   sms <- li + fb
  185. }
  186.  
  187. # Print the resulting sms to the console
  188. sms
  189.  
  190. capitulo 3 funções
  191.  
  192. 3/2
  193.  
  194. # Consult the documentation on the mean() function
  195. ?mean
  196.  
  197. # Inspect the arguments of the mean() function
  198.  
  199. args(mean)
  200.  
  201. 3/3
  202. # The linkedin and facebook vectors have already been created for you
  203. linkedin <- c(16, 9, 13, 5, 2, 17, 14)
  204. facebook <- c(17, 7, 5, 16, 8, 13, 14)
  205.  
  206. # Calculate average number of views
  207.  
  208. avg_li <- mean (linkedin)
  209. avg_fb <- mean(facebook)
  210.  
  211.  
  212. # Inspect avg_li and avg_fb
  213. avg_li
  214. avg_fb
  215.  
  216. 3/4
  217. # The linkedin and facebook vectors have already been created for you
  218. linkedin <- c(16, 9, 13, 5, 2, 17, 14)
  219. facebook <- c(17, 7, 5, 16, 8, 13, 14)
  220.  
  221. # Calculate the mean of the sum
  222. avg_sum <- mean(linkedin + facebook)
  223.  
  224. # Calculate the trimmed mean of the sum
  225. avg_sum_trimmed <- mean(linkedin + facebook, trim = 0.2)
  226.  
  227. # Inspect both new variables
  228. avg_sum
  229. avg_sum_trimmed
  230.  
  231.  
  232. 3/5
  233. # The linkedin and facebook vectors have already been created for you
  234. linkedin <- c(16, 9, 13, 5, NA, 17, 14)
  235. facebook <- c(17, NA, 5, 16, 8, 13, 14)
  236.  
  237. # Basic average of linkedin
  238.  
  239. mean(linkedin)
  240.  
  241. # Advanced average of linkedin
  242.  
  243. mean(linkedin, na.rm = TRUE )
  244.  
  245. 3/6
  246. # The linkedin and facebook vectors have already been created for you
  247. linkedin <- c(16, 9, 13, 5, NA, 17, 14)
  248. facebook <- c(17, NA, 5, 16, 8, 13, 14)
  249.  
  250. # Calculate the mean absolute deviation
  251.  
  252. mean(abs(linkedin - facebook),na.rm = TRUE)
  253.  
  254.  
  255. 3/ 9 functions
  256. # Create a function pow_two()
  257.  
  258. pow_two <-function(a)
  259. {
  260.   a<- a*a
  261.   return(a)
  262. }
  263.  
  264.  
  265. # Use the function
  266. pow_two(12)
  267.  
  268. # Create a function sum_abs()
  269. sum_abs <-function(a,b)
  270. {
  271.   return(abs(a)+abs(b) )
  272. }
  273.  
  274.  
  275. # Use the function
  276. sum_abs(-2,3)
  277.  
  278. 3/10
  279. # Define the function hello()
  280. hello <- function()
  281. {
  282.   print("Hi there!")
  283.   return (TRUE)
  284. }
  285.  
  286. # Call the function hello()
  287.  
  288. hello()
  289.  
  290. 3/14
  291. # The linkedin and facebook vectors have already been created for you
  292.  
  293. # Define the interpret function
  294. interpret <- function(num_views) {
  295.   if (num_views > 15) {
  296.     print("You're popular!")
  297.     return(num_views)
  298.   } else {
  299.     print("Try to be more visible!")
  300.     return(0)
  301.   }
  302. }
  303.  
  304. # Call the interpret function twice
  305. interpret(linkedin[1])
  306. interpret(facebook[2])
  307.  
  308.  
  309. 3/15
  310. # The linkedin and facebook vectors have already been created for you
  311. linkedin <- c(16, 9, 13, 5, 2, 17, 14)
  312. facebook <- c(17, 7, 5, 16, 8, 13, 14)
  313.  
  314. # The interpret() can be used inside interpret_all()
  315. interpret <- function(num_views) {
  316.   if (num_views > 15) {
  317.     print("You're popular!")
  318.     return(num_views)
  319.   } else {
  320.     print("Try to be more visible!")
  321.     return(0)
  322.   }
  323. }
  324.  
  325. # Define the interpret_all() function
  326. # views: vector with data to interpret
  327. # return_sum: return total number of views on popular days?
  328. interpret_all <- function(views, return_sum = TRUE) {
  329.   count <- 0
  330.  
  331.   for (v in views) {
  332.     count <- count + interpret(v)
  333.   }
  334.  
  335.   if (return_sum) {
  336.     return (count)
  337.   } else {
  338.     return (NULL)
  339.   }
  340. }
  341.  
  342. # Call the interpret_all() function on both linkedin and facebook
  343. interpret_all(linkedin)
  344. interpret_all(facebook)
  345.  
  346.  
  347. 3/17
  348. # Load the ggplot2 package
  349. library(ggplot2)
  350.  
  351. # Retry the qplot() function
  352. qplot(mtcars$wt, mtcars$hp)
  353.  
  354. # Check out the currently attached packages again
  355.  
  356. search()
  357.  
  358. 4/2
  359. # The vector pioneers has already been created for you
  360. pioneers <- c("GAUSS:1777", "BAYES:1702", "PASCAL:1623", "PEARSON:1857")
  361.  
  362. # Split names from birth year
  363. split_math <- strsplit(pioneers, split = ":")
  364.  
  365. # Convert to lowercase strings: split_low
  366.  
  367. split_low <- lapply(split_math,tolower)
  368.  
  369. # Take a look at the structure of split_low
  370.  
  371. str(split_low)
  372.  
  373. 4/3
  374. # Code from previous exercise:
  375. pioneers <- c("GAUSS:1777", "BAYES:1702", "PASCAL:1623", "PEARSON:1857")
  376. split <- strsplit(pioneers, split = ":")
  377. split_low <- lapply(split, tolower)
  378.  
  379. # Write function select_first()
  380. select_first <- function(x) {
  381.   x[1]
  382. }
  383.  
  384.  
  385. # Apply select_first() over split_low: names
  386. names <- lapply(split_low,select_first)
  387.  
  388. # Write function select_second()
  389. select_second <- function(x)
  390. {
  391.   x[2]
  392. }
  393.  
  394.  
  395.  
  396. # Apply select_second() over split_low: years
  397. years <-lapply(split_low,select_second)
  398.  
  399. 4/4
  400. # Definition of split_low
  401. pioneers <- c("GAUSS:1777", "BAYES:1702", "PASCAL:1623", "PEARSON:1857")
  402. split <- strsplit(pioneers, split = ":")
  403. split_low <- lapply(split, tolower)
  404.  
  405.  
  406. names <- lapply(split_low, function(x){x[1]} )
  407.  
  408. years <- lapply(split_low, function(x){x[2]})
  409.  
  410. 4/5
  411. # Definition of split_low
  412. pioneers <- c("GAUSS:1777", "BAYES:1702", "PASCAL:1623", "PEARSON:1857")
  413. split <- strsplit(pioneers, split = ":")
  414. split_low <- lapply(split, tolower)
  415.  
  416. # Generic select function
  417. select_el <- function(x, index) {
  418.   x[index]
  419. }
  420. # Use lapply() twice on split_low: names and years
  421. names <- lapply(split_low,select_el,1)
  422. years <- lapply(split_low,select_el,2)
  423.  
  424. 4/9
  425.  
  426. # temp is already defined in the workspace
  427.  
  428. # Finish function definition of extremes_avg
  429. extremes_avg <- function(x) {
  430.   ( min(x) + max(x) ) / 2
  431. }
  432.  
  433. # Apply extremes_avg() over temp using sapply()
  434. sapply(temp,extremes_avg)
  435.  
  436. # Apply extremes_avg() over temp using lapply()
  437. lapply(temp,extremes_avg)
  438.  
  439. 4/10
  440. # temp is already available in the workspace
  441.  
  442. # Create a function that returns min and max of a vector: extremes
  443. extremes <- function(x) {
  444.   c(min = min(x), max = max(x))
  445. }
  446.  
  447. # Apply extremes() over temp with sapply()
  448. sapply(temp,extremes)
  449.  
  450.  
  451. # Apply extremes() over temp with lapply()
  452. lapply(temp,extremes)
  453.  
  454.  
  455.  
  456. 4/11
  457. # temp is already prepared for you in the workspace
  458.  
  459. # Definition of below_zero()
  460. below_zero <- function(x) {
  461.   return(x[x < 0])
  462. }
  463.  
  464. # Apply below_zero over temp using sapply(): freezing_s
  465. freezing_s <- sapply(temp, below_zero)
  466.  
  467. # Apply below_zero over temp using lapply(): freezing_l
  468. freezing_l <- lapply(temp,below_zero)
  469.  
  470. # Are freezing_s and freezing_l identical?
  471.  
  472. identical(freezing_l,freezing_s)
  473.  
  474. 4/12
  475. # temp is already available in the workspace
  476.  
  477. # Definition of print_info()
  478. print_info <- function(x) {
  479.   cat("The average temperature is", mean(x), "\n")
  480. }
  481.  
  482. # Apply print_info() over temp using sapply()
  483.  
  484. sapply(temp,print_info)
  485.  
  486. # Apply print_info() over temp using lapply()
  487. lapply(temp,print_info)
  488.  
  489.  
  490. 4/15
  491. # temp is already available in the workspace
  492.  
  493. # Definition of basics()
  494. basics <- function(x) {
  495.   c(min = min(x), mean = mean(x), max = max(x))
  496. }
  497.  
  498. # Apply basics() over temp using vapply()
  499.  
  500. vapply(temp,basics,numeric(3))
  501.  
  502. 4/16
  503. # temp is already available in the workspace
  504.  
  505. # Definition of the basics() function
  506. basics <- function(x) {
  507.   c(min = min(x), mean = mean(x), median = median(x), max = max(x))
  508. }
  509.  
  510. # Fix the error:
  511. vapply(temp, basics, numeric(4))
  512.  
  513.  
  514. 4/17
  515. # temp is already defined in the workspace
  516.  
  517. # Convert to vapply() expression
  518. vapply(temp, max,numeric(1))
  519.  
  520. # Convert to vapply() expression
  521. vapply(temp, function(x, y) { mean(x) > y }, y = 5 , logical(1))
  522.  
  523. 5/2
  524. # The errors vector has already been defined for you
  525. errors <- c(1.9, -2.6, 4.0, -9.5, -3.4, 7.3)
  526.  
  527. # Sum of absolute rounded values of errors
  528. sum(round(abs(errors)))
  529.  
  530. 5/3
  531. # Don't edit these two lines
  532. vec1 <- c(1.5, 2.5, 8.4, 3.7, 6.3)
  533. vec2 <- rev(vec1)
  534.  
  535. # Fix the error
  536. mean(c(abs(vec1), abs(vec2)))
  537.  
  538.  
  539. 5/4
  540. # The linkedin and facebook lists have already been created for you
  541. linkedin <- list(16, 9, 13, 5, 2, 17, 14)
  542. facebook <- list(17, 7, 5, 16, 8, 13, 14)
  543.  
  544. # Convert linkedin and facebook to a vector: li_vec and fb_vec
  545. li_vec <- unlist(linkedin)
  546. fb_vec <- unlist(facebook)
  547.  
  548.  
  549. # Append fb_vec to li_vec: social_vec
  550. social_vec <- append(li_vec,fb_vec)
  551.  
  552. # Sort social_vec
  553. sort(social_vec,decreasing = TRUE)
  554.  
  555. 5/5
  556. # Fix me
  557. rep(seq(1, 7, by = 2), times = 7)
  558.  
  559. 5/6
  560. # Create first sequence: seq1
  561.  
  562. seq1 <- seq(from = 1,to = 500,3)
  563. # Create second sequence: seq2
  564. seq2 <- seq(from = 1200,to = 900,-7)
  565.  
  566. # Calculate total sum of the sequences
  567.  
  568. sum(c(seq1,seq2))
  569.  
  570. 5/8
  571. # The emails vector has already been defined for you
  572. emails <- c("john.doe@ivyleague.edu", "education@world.gov", "dalai.lama@peace.org",
  573.             "invalid.edu", "quant@bigdatacollege.edu", "cookie.monster@sesame.tv")
  574.  
  575. # Use grepl() to match for "edu"
  576.  
  577. grepl(pattern= "edu",x = emails)
  578.  
  579. # Use grep() to match for "edu", save result to hits
  580.  
  581. hits <- grep(pattern="edu",x=emails)
  582.  
  583. # Subset emails using hits
  584. emails[hits]
  585.  
  586. 5/10
  587. # The emails vector has already been defined for you
  588. emails <- c("john.doe@ivyleague.edu", "education@world.gov", "global@peace.org",
  589.             "invalid.edu", "quant@bigdatacollege.edu", "cookie.monster@sesame.tv")
  590.  
  591. # Use sub() to convert the email domains to datacamp.edu
  592.  
  593. sub(pattern="@.*\\.edu$","@datacamp.edu",x = emails)
  594.  
  595.  
  596. 5/14
  597. # Definition of character strings representing dates
  598. str1 <- "May 23, '96"
  599. str2 <- "2012-03-15"
  600. str3 <- "30/January/2006"
  601.  
  602. # Convert the strings to dates: date1, date2, date3
  603. date1 <- as.Date(str1, format = "%b %d, '%y")
  604. date2 <- as.Date(str2, format = "%Y-%m-%d")
  605. date3 <- as.Date(str3,format = "%d/%B/%Y")
  606.  
  607. # Convert dates to formatted strings
  608. format(date1, "%A")
  609. format(date2,"%d")
  610. format(date3,"%b %Y")
  611.  
  612.  
  613. 5/16
  614. # day1, day2, day3, day4 and day5 are already available in the workspace
  615.  
  616. # Difference between last and first pizza day
  617. day5-day1
  618.  
  619. # Create vector pizza
  620. pizza <- c(day1, day2, day3, day4, day5)
  621.  
  622. # Create differences between consecutive pizza days: day_diff
  623. day_diff <- diff(pizza)
  624.  
  625. # Average period between two consecutive pizza days
  626.  
  627. mean(day_diff)
  628.  
  629. 5/17
  630. # login and logout are already defined in the workspace
  631. # Calculate the difference between login and logout: time_online
  632. time_online <- logout - login
  633.  
  634. # Inspect the variable time_online
  635.  
  636. time_online
  637.  
  638. # Calculate the total time online
  639.  
  640. sum(logout-login)
  641.  
  642. # Calculate the average time online
  643. mean(logout-login)
  644.  
  645. 5/18
  646. # Convert astro to vector of Date objects: astro_dates
  647.  
  648. astro_dates<- as.Date(astro,format="%d-%b-%Y")
  649.  
  650. # Convert meteo to vector of Date objects: meteo_dates
  651.  
  652. meteo_dates<- as.Date(meteo,format="%B %d,%y")
  653.  
  654. # Calculate the maximum absolute difference between astro_dates and meteo_dates
  655. max(abs(astro_dates-meteo_dates))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement