Skip to content

Instantly share code, notes, and snippets.

@somerealnumbers
Created November 20, 2015 23:36
Show Gist options
  • Select an option

  • Save somerealnumbers/76a92f161fb777290c64 to your computer and use it in GitHub Desktop.

Select an option

Save somerealnumbers/76a92f161fb777290c64 to your computer and use it in GitHub Desktop.
guess_weight = function(posts){
# reading in the guesses
guesses = readLines(posts)
# Match the guesses in kilograms and store result in vector
kgmatch = regmatches(guesses,regexpr('[1-9]+\\.?[0-9]* *(k|K) *(g|G)(s|S)?',guesses))
# Changing to numeric and storing in a vector
kgnumerics = as.numeric(regmatches(kgmatch,regexpr('[1-9]+\\.?[0-9]*',kgmatch)))
# Match the guesses in pounds and store result in vector
lbmatch = regmatches(guesses,regexpr('[1-9]+\\.?[0-9]* *(l|L) *(b|B)(s|S)?',guesses))
# Changing to numeric and storing in a vector
lbnumerics = as.numeric(regmatches(lbmatch,regexpr('[1-9]+\\.?[0-9]*',lbmatch)))
# Converting lbs to kgs
lbnumericsK = lbnumerics*0.453592
# Combining both measures
totalGuesses = append(kgnumerics,lbnumericsK)
# calculating the mean of the guesses
myGuess = mean(totalGuesses)
# summary statistics
print(summary(totalGuesses))
# histogram of guesses
hist(totalGuesses)
# vector of cleaned guesses
invisible(totalGuesses)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment