Takes a vector as the only input,
Returns a summary of numeric vector (mean, 5-number summaries, std dev), and
Returns a frequency table of a character vector
vecSum <- function(x){
if (class(x)=="numeric")
{
Mean = mean(x)
Min = min(x)
Quant1 = quantile(x, 0.25)
Med = median(x)
Quant3 = quantile(x, 0.75)
Max = max(x)
Sd = sd(x)
Summary = c(Mean = Mean, Minimum = Min, "1st Quantile" = Quant1, Median = Med, "3rd Quantile" = Quant3, Maximum = Max, "Standard Deviation" = Sd)
return(Summary)
}
else if (class(x)=="character") {
return(table(x))
}
else {
print("You must enter a vector")
}
}
x = c(12, 20, 18, 32, 15, 20) and y = c("M", "M", "F", "F", "M", "M", "M", "F")x <- c(12, 20, 18, 32, 15, 20)
y <- c("M", "M", "F", "F", "M", "M", "M", "F")
vecSum(x)
## Mean Minimum 1st Quantile.25% Median
## 19.500000 12.000000 15.750000 19.000000
## 3rd Quantile.75% Maximum Standard Deviation
## 20.000000 32.000000 6.862944
vecSum(y)
## x
## F M
## 3 5
Takes a character vector as the only input, and
Returns a character vector such that each element of it only has the initial letter in uppercase
FirstUp <- function(x){
if (class(x)=="character")
{
x <- tolower(x)
substr(x, 1, 1) <- toupper(substr(x, 1, 1))
return(x)
}
else {
print("You must enter a character vector")
}
}
row.names(mtcars) dataset in base RFirstUp(row.names(mtcars))
## [1] "Mazda rx4" "Mazda rx4 wag" "Datsun 710"
## [4] "Hornet 4 drive" "Hornet sportabout" "Valiant"
## [7] "Duster 360" "Merc 240d" "Merc 230"
## [10] "Merc 280" "Merc 280c" "Merc 450se"
## [13] "Merc 450sl" "Merc 450slc" "Cadillac fleetwood"
## [16] "Lincoln continental" "Chrysler imperial" "Fiat 128"
## [19] "Honda civic" "Toyota corolla" "Toyota corona"
## [22] "Dodge challenger" "Amc javelin" "Camaro z28"
## [25] "Pontiac firebird" "Fiat x1-9" "Porsche 914-2"
## [28] "Lotus europa" "Ford pantera l" "Ferrari dino"
## [31] "Maserati bora" "Volvo 142e"