Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Some vectors of numbers
- percent_change <- c(4, -1.91, 3.00, -5.002)
- income <- c(72.19, 1030.18, 10291.93, 1189192.18)
- p_values <- c(0.12, 0.98, 0.0000191, 0.00000000002)
- # Format c(0.0011, 0.011, 1) with digits = 1
- format(c(0.0011, 0.011, 1), digits =1)
- # Format c(1.0011, 2.011, 1) with digits = 1
- format(c(1.0011, 2.011, 1), digits=1)
- # Format percent_change to one place after the decimal point
- format(percent_change, digits=2)
- # Format income to whole numbers
- format(income, digits=2)
- # Format p_values in fixed format
- format(p_values, scientific=FALSE)
- ## 2
- formatted_income <- format(income, digits = 2)
- # Print formatted_income
- print(formatted_income)
- # Call writeLines() on the formatted income
- writeLines(formatted_income)
- # Define trimmed_income
- trimmed_income <- format(income, digits=2, trim=TRUE)
- # Call writeLines() on the trimmed_income
- writeLines(trimmed_income)
- # Define pretty_income
- pretty_income <- format(income, digits = 2, big.mark=",")
- # Call writeLines() on the pretty_income
- writeLines(pretty_income)
- #Rather than a scientific argument, formatC() has a format argument that takes a code representing the required format. The most useful are:
- #"f" for fixed,
- #"e" for scientific, and
- #"g" for fixed unless scientific saves space
- # From the format() exercise
- x <- c(0.0011, 0.011, 1)
- y <- c(1.0011, 2.011, 1)
- # formatC() on x with format = "f", digits = 1
- formatC(x, format='f', digits=1)
- # formatC() on y with format = "f", digits = 1
- formatC(y, format ="f", digits=1)
- # Format percent_change to one place after the decimal point
- formatC(percent_change, format='f',digits=1)
- # percent_change with flag = "+"
- formatC(percent_change, format='f',digits=1, flag="+")
- # Format p_values using format = "g" and digits = 2
- formatC(p_values, format="g", digits=2)
- # Add $ to pretty_income
- paste("$",pretty_income,sep='' )
- # Add % to pretty_percent
- paste(pretty_percent, "%", sep="")
- # Create vector with elements like 2010: +4.0%`
- year_percent <- paste(years, ': ', pretty_percent, "%", sep="")
- # Collapse all years into single string
- paste(year_percent, collapse=', ')
- ## 3
- # Define the names vector
- income_names <- c("Year 0", "Year 1", "Year 2", "Project Lifetime")
- # Create pretty_income
- pretty_income <- format(income, digits=2, big.mark=",")
- # Create dollar_income
- dollar_income <- paste("$", pretty_income, sep="")
- # Create formatted_names
- formatted_names <- format(income_names, justify="right")
- # Create rows
- rows<-paste(formatted_names, dollar_income, sep=" ")
- # Write rows
- writeLines(rows)
- ## 3
- # Randomly sample 3 toppings
- my_toppings <- sample(toppings, size = 3)
- # Print my_toppings
- my_toppings
- # Paste "and " to last element: my_toppings_and
- my_toppings_and <- paste(c("", "", "and "), my_toppings, sep = "")
- # Collapse with comma space: these_toppings
- these_toppings <- paste(my_toppings_and, collapse = ", ")
- # Add rest of sentence: my_order
- my_order <- paste("I want to order a pizza with ", these_toppings, ".", sep = "")
- # Order pizza with writeLines()
- writeLines(my_order)
- ## stringr
- library(stringr)
- my_toppings <- c("cheese", NA, NA)
- my_toppings_and <- paste(c("", "", "and "), my_toppings, sep = "")
- # Print my_toppings_and
- print(my_toppings_and)
- # Use str_c() instead of paste(): my_toppings_str
- my_toppings_str <- str_c(c("", "", "and "), my_toppings)
- # Print my_toppings_str
- print(my_toppings_str)
- # paste() my_toppings_and with collapse = ", "
- paste(my_toppings_and, collapse = ", ")
- # str_c() my_toppings_str with collapse = ", "
- str_c(my_toppings_str, collapse = ", ")
- ##
- library(stringr)
- library(babynames)
- library(dplyr)
- # Extracting vectors for boys' and girls' names
- babynames_2014 <- filter(babynames, year == 2014)
- boy_names <- filter(babynames_2014, sex == "M")$name
- girl_names <- filter(babynames_2014, sex == "F")$name
- # Take a look at a few boy_names
- head(boy_names)
- # Find the length of all boy_names
- boy_length <- str_length(boy_names)
- # Take a look at a few lengths
- head(boy_length)
- # Find the length of all girl_names
- girl_length <- str_length(girl_names)
- # Find the difference in mean length
- mean(girl_length) - mean(boy_length)
- # Confirm str_length() works with factors
- head(str_length(factor(boy_names)))
- #####
- # Extract first letter from boy_names
- boy_first_letter <- str_sub(boy_names, 1,1)
- # Tabulate occurrences of boy_first_letter
- table(boy_first_letter)
- # Extract the last letter in boy_names, then tabulate
- boy_last_letter <- str_sub(boy_names, -1,-1)
- table(boy_last_letter)
- # Extract the first letter in girl_names, then tabulate
- girl_first_letter <- str_sub(girl_names, 1,1)
- table(girl_first_letter)
- # Extract the last letter in girl_names, then tabulate
- girl_last_letter <- str_sub(girl_names, -1,-1)
- table(girl_last_letter)
- ####
- # Look for pattern "zz" in boy_names
- contains_zz <- str_detect(boy_names, pattern="zz")
- # Examine str() of contains_zz
- str(contains_zz)
- # How many names contain "zz"?
- sum(contains_zz)
- # Which names contain "zz"?
- boy_names[contains_zz]
- # Which rows in boy_df have names that contain "zz"?
- boy_df[contains_zz,]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement