Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- RESPOSTAS DATACAMP
- COMO USAR:
- OLHAR O LINK DA PÁGINA.
- https://campus.datacamp.com/courses/intermediate-r/chapter-4-the-apply-family?ex=3
- CAPTIULO 4 EXERCICIO 3, USAR ENTAO BUSCA POR 4/3
- FÁCIL DEMAIS NÉ
- # Comparison of logicals
- TRUE == FALSE
- # Comparison of numerics
- (-6 * 14) != (17 - 101)
- # Comparison of character strings
- "useR" == "user"
- # Compare a logical with a numeric
- 1 == TRUE
- 1/3
- # Comparison of numerics
- (-6 *5 + 2) >= (-10+1)
- # Comparison of character strings
- "raining" <= "raining dogs"
- # Comparison of logicals
- TRUE > FALSE
- 1/4
- # The linkedin and facebook vectors have already been created for you
- linkedin <- c(16, 9, 13, 5, 2, 17, 14)
- facebook <- c(17, 7, 5, 16, 8, 13, 14)
- # Popular days
- linkedin > 15
- # Quiet days
- linkedin <= 5
- # LinkedIn more popular than Facebook
- linkedin > facebook
- 1/5
- # The social data has been created for you
- linkedin <- c(16, 9, 13, 5, 2, 17, 14)
- facebook <- c(17, 7, 5, 16, 8, 13, 14)
- views <- matrix(c(linkedin, facebook), nrow = 2, byrow = TRUE)
- # When does views equal 13?
- views == 13
- # When is views less than or equal to 14?
- views <= 14
- 1/7
- # The linkedin and last variable are already defined for you
- linkedin <- c(16, 9, 13, 5, 2, 17, 14)
- last <- tail(linkedin, 1)
- # Is last under 5 or above 10?
- last < 5 | last > 10
- # Is last between 15 (exclusive) and 20 (inclusive)?
- last > 15 & last <=20
- 1/8
- # The social data (linkedin, facebook, views) has been created for you
- # linkedin exceeds 10 but facebook below 10
- (linkedin > 10) & (facebook < 10)
- # When were one or both visited at least 12 times?
- (linkedin >= 12) | (facebook >=12)
- # When is views between 11 (exclusive) and 14 (inclusive)?
- views > 11 & views <= 14
- 1/10
- # li_df is pre-loaded in your workspace
- # Select the second column, named day2, from li_df: second
- second = li_df[,2]
- # Build a logical vector, TRUE if value in second is extreme: extremes
- extremes <- (second > 25) | second<5
- # Count the number of TRUEs in extremes
- sum(extremes)
- # Solve it with a one-liner
- 1/12
- # Variables related to your last day of recordings
- medium <- "LinkedIn"
- num_views <- 14
- # Examine the if statement for medium
- if (medium == "LinkedIn") {
- print("Showing LinkedIn information")
- }
- # Write the if statement for num_views
- if(num_views > 15)
- {
- print("You're popular!")
- }
- 1/13
- # Variables related to your last day of recordings
- medium <- "LinkedIn"
- num_views <- 14
- # Control structure for medium
- if (medium == "LinkedIn") {
- print("Showing LinkedIn information")
- }
- else{
- print("Unknown medium")
- }
- # Control structure for num_views
- if (num_views > 15) {
- print("You're popular!")
- }
- else{
- print("Try to be more visible!")
- }
- 1/14
- # Variables related to your last day of recordings
- medium <- "LinkedIn"
- num_views <- 14
- # Control structure for medium
- if (medium == "LinkedIn") {
- print("Showing LinkedIn information")
- } else if (medium == "Facebook") {
- print("Showing Facebook information")
- } else {
- print("Unknown medium")
- }
- # Control structure for num_views
- if (num_views > 15) {
- print("You're popular!")
- } else if (num_views <= 15 & num_views > 10) {
- print("Your number of views is average")
- } else {
- print("Try to be more visible!")
- }
- 1/16
- # Variables related to your last day of recordings
- li <- 15
- fb <- 9
- # Code the control-flow construct
- if (li >= 15 & fb >=15) {
- sms <- 2 * (li + fb)
- } else if(li < 10 & fb < 10) {
- sms <- 0.5 * (li + fb)
- } else {
- sms <- li + fb
- }
- # Print the resulting sms to the console
- sms
- capitulo 3 funções
- 3/2
- # Consult the documentation on the mean() function
- ?mean
- # Inspect the arguments of the mean() function
- args(mean)
- 3/3
- # The linkedin and facebook vectors have already been created for you
- linkedin <- c(16, 9, 13, 5, 2, 17, 14)
- facebook <- c(17, 7, 5, 16, 8, 13, 14)
- # Calculate average number of views
- avg_li <- mean (linkedin)
- avg_fb <- mean(facebook)
- # Inspect avg_li and avg_fb
- avg_li
- avg_fb
- 3/4
- # The linkedin and facebook vectors have already been created for you
- linkedin <- c(16, 9, 13, 5, 2, 17, 14)
- facebook <- c(17, 7, 5, 16, 8, 13, 14)
- # Calculate the mean of the sum
- avg_sum <- mean(linkedin + facebook)
- # Calculate the trimmed mean of the sum
- avg_sum_trimmed <- mean(linkedin + facebook, trim = 0.2)
- # Inspect both new variables
- avg_sum
- avg_sum_trimmed
- 3/5
- # The linkedin and facebook vectors have already been created for you
- linkedin <- c(16, 9, 13, 5, NA, 17, 14)
- facebook <- c(17, NA, 5, 16, 8, 13, 14)
- # Basic average of linkedin
- mean(linkedin)
- # Advanced average of linkedin
- mean(linkedin, na.rm = TRUE )
- 3/6
- # The linkedin and facebook vectors have already been created for you
- linkedin <- c(16, 9, 13, 5, NA, 17, 14)
- facebook <- c(17, NA, 5, 16, 8, 13, 14)
- # Calculate the mean absolute deviation
- mean(abs(linkedin - facebook),na.rm = TRUE)
- 3/ 9 functions
- # Create a function pow_two()
- pow_two <-function(a)
- {
- a<- a*a
- return(a)
- }
- # Use the function
- pow_two(12)
- # Create a function sum_abs()
- sum_abs <-function(a,b)
- {
- return(abs(a)+abs(b) )
- }
- # Use the function
- sum_abs(-2,3)
- 3/10
- # Define the function hello()
- hello <- function()
- {
- print("Hi there!")
- return (TRUE)
- }
- # Call the function hello()
- hello()
- 3/14
- # The linkedin and facebook vectors have already been created for you
- # Define the interpret function
- interpret <- function(num_views) {
- if (num_views > 15) {
- print("You're popular!")
- return(num_views)
- } else {
- print("Try to be more visible!")
- return(0)
- }
- }
- # Call the interpret function twice
- interpret(linkedin[1])
- interpret(facebook[2])
- 3/15
- # The linkedin and facebook vectors have already been created for you
- linkedin <- c(16, 9, 13, 5, 2, 17, 14)
- facebook <- c(17, 7, 5, 16, 8, 13, 14)
- # The interpret() can be used inside interpret_all()
- interpret <- function(num_views) {
- if (num_views > 15) {
- print("You're popular!")
- return(num_views)
- } else {
- print("Try to be more visible!")
- return(0)
- }
- }
- # Define the interpret_all() function
- # views: vector with data to interpret
- # return_sum: return total number of views on popular days?
- interpret_all <- function(views, return_sum = TRUE) {
- count <- 0
- for (v in views) {
- count <- count + interpret(v)
- }
- if (return_sum) {
- return (count)
- } else {
- return (NULL)
- }
- }
- # Call the interpret_all() function on both linkedin and facebook
- interpret_all(linkedin)
- interpret_all(facebook)
- 3/17
- # Load the ggplot2 package
- library(ggplot2)
- # Retry the qplot() function
- qplot(mtcars$wt, mtcars$hp)
- # Check out the currently attached packages again
- search()
- 4/2
- # The vector pioneers has already been created for you
- pioneers <- c("GAUSS:1777", "BAYES:1702", "PASCAL:1623", "PEARSON:1857")
- # Split names from birth year
- split_math <- strsplit(pioneers, split = ":")
- # Convert to lowercase strings: split_low
- split_low <- lapply(split_math,tolower)
- # Take a look at the structure of split_low
- str(split_low)
- 4/3
- # Code from previous exercise:
- pioneers <- c("GAUSS:1777", "BAYES:1702", "PASCAL:1623", "PEARSON:1857")
- split <- strsplit(pioneers, split = ":")
- split_low <- lapply(split, tolower)
- # Write function select_first()
- select_first <- function(x) {
- x[1]
- }
- # Apply select_first() over split_low: names
- names <- lapply(split_low,select_first)
- # Write function select_second()
- select_second <- function(x)
- {
- x[2]
- }
- # Apply select_second() over split_low: years
- years <-lapply(split_low,select_second)
- 4/4
- # Definition of split_low
- pioneers <- c("GAUSS:1777", "BAYES:1702", "PASCAL:1623", "PEARSON:1857")
- split <- strsplit(pioneers, split = ":")
- split_low <- lapply(split, tolower)
- names <- lapply(split_low, function(x){x[1]} )
- years <- lapply(split_low, function(x){x[2]})
- 4/5
- # Definition of split_low
- pioneers <- c("GAUSS:1777", "BAYES:1702", "PASCAL:1623", "PEARSON:1857")
- split <- strsplit(pioneers, split = ":")
- split_low <- lapply(split, tolower)
- # Generic select function
- select_el <- function(x, index) {
- x[index]
- }
- # Use lapply() twice on split_low: names and years
- names <- lapply(split_low,select_el,1)
- years <- lapply(split_low,select_el,2)
- 4/9
- # temp is already defined in the workspace
- # Finish function definition of extremes_avg
- extremes_avg <- function(x) {
- ( min(x) + max(x) ) / 2
- }
- # Apply extremes_avg() over temp using sapply()
- sapply(temp,extremes_avg)
- # Apply extremes_avg() over temp using lapply()
- lapply(temp,extremes_avg)
- 4/10
- # temp is already available in the workspace
- # Create a function that returns min and max of a vector: extremes
- extremes <- function(x) {
- c(min = min(x), max = max(x))
- }
- # Apply extremes() over temp with sapply()
- sapply(temp,extremes)
- # Apply extremes() over temp with lapply()
- lapply(temp,extremes)
- 4/11
- # temp is already prepared for you in the workspace
- # Definition of below_zero()
- below_zero <- function(x) {
- return(x[x < 0])
- }
- # Apply below_zero over temp using sapply(): freezing_s
- freezing_s <- sapply(temp, below_zero)
- # Apply below_zero over temp using lapply(): freezing_l
- freezing_l <- lapply(temp,below_zero)
- # Are freezing_s and freezing_l identical?
- identical(freezing_l,freezing_s)
- 4/12
- # temp is already available in the workspace
- # Definition of print_info()
- print_info <- function(x) {
- cat("The average temperature is", mean(x), "\n")
- }
- # Apply print_info() over temp using sapply()
- sapply(temp,print_info)
- # Apply print_info() over temp using lapply()
- lapply(temp,print_info)
- 4/15
- # temp is already available in the workspace
- # Definition of basics()
- basics <- function(x) {
- c(min = min(x), mean = mean(x), max = max(x))
- }
- # Apply basics() over temp using vapply()
- vapply(temp,basics,numeric(3))
- 4/16
- # temp is already available in the workspace
- # Definition of the basics() function
- basics <- function(x) {
- c(min = min(x), mean = mean(x), median = median(x), max = max(x))
- }
- # Fix the error:
- vapply(temp, basics, numeric(4))
- 4/17
- # temp is already defined in the workspace
- # Convert to vapply() expression
- vapply(temp, max,numeric(1))
- # Convert to vapply() expression
- vapply(temp, function(x, y) { mean(x) > y }, y = 5 , logical(1))
- 5/2
- # The errors vector has already been defined for you
- errors <- c(1.9, -2.6, 4.0, -9.5, -3.4, 7.3)
- # Sum of absolute rounded values of errors
- sum(round(abs(errors)))
- 5/3
- # Don't edit these two lines
- vec1 <- c(1.5, 2.5, 8.4, 3.7, 6.3)
- vec2 <- rev(vec1)
- # Fix the error
- mean(c(abs(vec1), abs(vec2)))
- 5/4
- # The linkedin and facebook lists have already been created for you
- linkedin <- list(16, 9, 13, 5, 2, 17, 14)
- facebook <- list(17, 7, 5, 16, 8, 13, 14)
- # Convert linkedin and facebook to a vector: li_vec and fb_vec
- li_vec <- unlist(linkedin)
- fb_vec <- unlist(facebook)
- # Append fb_vec to li_vec: social_vec
- social_vec <- append(li_vec,fb_vec)
- # Sort social_vec
- sort(social_vec,decreasing = TRUE)
- 5/5
- # Fix me
- rep(seq(1, 7, by = 2), times = 7)
- 5/6
- # Create first sequence: seq1
- seq1 <- seq(from = 1,to = 500,3)
- # Create second sequence: seq2
- seq2 <- seq(from = 1200,to = 900,-7)
- # Calculate total sum of the sequences
- sum(c(seq1,seq2))
- 5/8
- # The emails vector has already been defined for you
- emails <- c("john.doe@ivyleague.edu", "education@world.gov", "dalai.lama@peace.org",
- "invalid.edu", "quant@bigdatacollege.edu", "cookie.monster@sesame.tv")
- # Use grepl() to match for "edu"
- grepl(pattern= "edu",x = emails)
- # Use grep() to match for "edu", save result to hits
- hits <- grep(pattern="edu",x=emails)
- # Subset emails using hits
- emails[hits]
- 5/10
- # The emails vector has already been defined for you
- emails <- c("john.doe@ivyleague.edu", "education@world.gov", "global@peace.org",
- "invalid.edu", "quant@bigdatacollege.edu", "cookie.monster@sesame.tv")
- # Use sub() to convert the email domains to datacamp.edu
- sub(pattern="@.*\\.edu$","@datacamp.edu",x = emails)
- 5/14
- # Definition of character strings representing dates
- str1 <- "May 23, '96"
- str2 <- "2012-03-15"
- str3 <- "30/January/2006"
- # Convert the strings to dates: date1, date2, date3
- date1 <- as.Date(str1, format = "%b %d, '%y")
- date2 <- as.Date(str2, format = "%Y-%m-%d")
- date3 <- as.Date(str3,format = "%d/%B/%Y")
- # Convert dates to formatted strings
- format(date1, "%A")
- format(date2,"%d")
- format(date3,"%b %Y")
- 5/16
- # day1, day2, day3, day4 and day5 are already available in the workspace
- # Difference between last and first pizza day
- day5-day1
- # Create vector pizza
- pizza <- c(day1, day2, day3, day4, day5)
- # Create differences between consecutive pizza days: day_diff
- day_diff <- diff(pizza)
- # Average period between two consecutive pizza days
- mean(day_diff)
- 5/17
- # login and logout are already defined in the workspace
- # Calculate the difference between login and logout: time_online
- time_online <- logout - login
- # Inspect the variable time_online
- time_online
- # Calculate the total time online
- sum(logout-login)
- # Calculate the average time online
- mean(logout-login)
- 5/18
- # Convert astro to vector of Date objects: astro_dates
- astro_dates<- as.Date(astro,format="%d-%b-%Y")
- # Convert meteo to vector of Date objects: meteo_dates
- meteo_dates<- as.Date(meteo,format="%B %d,%y")
- # Calculate the maximum absolute difference between astro_dates and meteo_dates
- max(abs(astro_dates-meteo_dates))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement