Frequency Table
Newfunc <- function(x) {
if (is.numeric(x)==TRUE) {
return(summary(x), sd(x))
} else {
return(table(x))}
}
x = c("M", "M", "F", "F", "M", "M", "M", "F")
Newfunc(x)
## x
## F M
## 3 5
Summary
Newfunc <- function(x) {
if (is.numeric(x)==TRUE) {
return(c(summary(x), "SD" = sd(x)))
} else {
return(table(x))}
}
x = c(12, 20, 18, 32, 15, 20)
Newfunc(x)
## Min. 1st Qu. Median Mean 3rd Qu. Max. SD
## 12.000000 15.750000 19.000000 19.500000 20.000000 32.000000 6.862944
UpperFunc <- function(x) {
substr(x,1,1) <- toupper(substr(x,1,1))
x
}
UpperFunc(names(mtcars))
## [1] "Mpg" "Cyl" "Disp" "Hp" "Drat" "Wt" "Qsec" "Vs" "Am" "Gear"
## [11] "Carb"
I wanted to try to figure out how to do it another way using more of the suggested functions to get more familiarity. I can’t figure out how to replace them entirely however.
capitalize <- function(x) {
a <- paste0(toupper(substr(x,1,1)),substr(x,2,5))
}
headers <- names(mtcars)
sapply(headers, capitalize)
## mpg cyl disp hp drat wt qsec vs am gear carb
## "Mpg" "Cyl" "Disp" "Hp" "Drat" "Wt" "Qsec" "Vs" "Am" "Gear" "Carb"