All

# Apply gather() to bmi and save the result as bmi_long

library(tidyr)

bmi_long <- gather(bmi, year, bmi_val, -Country)

# View the first 20 rows of the result

head(bmi_long,20)

# Apply spread() to bmi_long

bmi_wide <- spread(bmi_long,year,bmi_val)

# View the head of bmi_wide

head(bmi_wide)

# Apply separate() to bmi_cc

bmi_cc_clean <- separate(bmi_cc, col = Country_ISO, into = c("Country", "ISO"), sep = "/")

# Print the head of the result

head(bmi_cc_clean)

# Apply unite() to bmi_cc_clean

bmi_cc <- unite(bmi_cc_clean, Country_ISO,Country,ISO, sep = "-")

# View the head of the result

head(bmi_cc)

## tidyr and dplyr are already loaded for you

# View the head of census

head(census)

# Gather the month columns

census2 <- gather(census, month, amount, -YEAR)

# Arrange rows by YEAR using dplyr's arrange

census2 <- arrange(census2, YEAR)

# View first 20 rows of census2

head(census2, 20)

# View first 50 rows of census_long

head(census_long,50)

# Spread the type column

census_long2 <- spread(census_long,type,amount)

# View first 20 rows of census_long2

head(census_long2,20)

# View the head of census_long3

head(census_long3)

# Separate the yr_month column into two

census_long4 <- separate(census_long3,yr_month,c("year","month"))

# View the first 6 rows of the result

head(census_long4)

# Preview students2 with str()

str(students2)

# Load the lubridate package

library(lubridate)

# Parse as date

dmy("17 Sep 2015")

# Parse as date and time (with no seconds!)

mdy_hm("July 15, 2012 12:56")

# Coerce dob to a date (with no time)

students2$dob <- ymd(students2$dob)

# Coerce nurse_visit to a date and time

students2$nurse_visit <- ymd_hms(students2$nurse_visit)

# Look at students2 once more with str()

str(students2)

# Load the stringr package

library(stringr)

# Trim all leading and trailing whitespace

c("  Filip ", "Nick  ", " Jonathan")

str_trim(c("  Filip ", "Nick  ", " Jonathan"))

# Pad these strings with leading zeros

c("23485W", "8823453Q", "994Z")

str_pad(c("23485W", "8823453Q", "994Z"),width=9,side="left",pad="0")

# Print state abbreviations

states

# Make states all uppercase and save result to states_upper

states_upper<-toupper(states)

# Make states_upper all lowercase again

tolower(states_upper)

## stringr has been loaded for you

# Look at the head of students2

head(students2)

# Detect all dates of birth (dob) in 1997

str_detect(students2$dob,"1997")

# In the sex column, replace "F" with "Female"...

students2$sex <- str_replace(students2$sex,"F","Female")

# ...And "M" with "Male"

students2$sex <- str_replace(students2$sex,"M","Male")

# View the head of students2

head(students2)

# Call is.na() on the full social_df to spot all NAs

is.na(social_df)

# Use the any() function to ask whether there are any NAs in the data

any(is.na(social_df))

# View a summary() of the dataset

summary(social_df)

# Call table() on the status column

table(social_df$status)

你可能感兴趣的:(All)