R Tips
Basic R
- Use column names as the iterable with sapply.
iris_data <- iris[,-5] par(mfrow = c(2,2)) temp <- sapply(colnames(iris_data), function(x) plot(iris_data[,x], main = x))
- Use
setNames(object = nm, nm)
to give values names.- Example: We want to add IQR alongside with summary
> c(summary(iris$Sepal.Length), setNames(IQR(iris$Sepal.Length), 'IQR'))
Min. 1st Qu. Median Mean 3rd Qu. Max. IQR 4.300000 5.100000 5.800000 5.843333 6.400000 7.900000 1.300000
- Example: We want to add IQR alongside with summary
- Use such form
(function(x) do_stuff_with(x)) (a)
to apply a anonymous function to a single variable.- Example: We want to add IQR, SD and Var along side with summary
> (function(x) print(c(summary(x), setNames(IQR(x), 'IQR'), setNames(sd(x), 'SD'), setNames(var(x), 'Var')))) (iris$Sepal.Length)
Min. 1st Qu. Median Mean 3rd Qu. Max. IQR SD Var 4.3000000 5.1000000 5.8000000 5.8433333 6.4000000 7.9000000 1.3000000 0.8280661 0.6856935
- Example: We want to add IQR, SD and Var along side with summary