A simple example of using ‘sprintf’

In this example, I am showing a very brief example of using ‘sprintf’ in a user-defined function for R

Removing duplicates in R using ‘dplyr’ and ‘data.table’

In this post, I will show how to remove duplicates of observations in a data frame.

 

Package ‘tibble’ in R

What is ‘tibble’ package?

According to Hadley Wickham “Tibbles are a modern reimagining of the data.frame, keeping what time has proven to be effective, and throwing out what is not.

The name comes from dplyr: originally you created these objects with tbl_df(), which was most easily pronounced as “tibble diff”. “

Find its similarities and dissimilarities with data.frame. More info here : tibble

mapply in R – an example

mapply() looks like an interesting function in R. here an example of what you can do with mapply() function

The results are :

apply and lapply, to loop over multiple columns in regression

I must say sorry to not citing the original source for this codes. I have ‘modified’ the codes to suit data in health and medicine. Can try these codes in your R

##################################

#data frame for covariates/independent variables (X)
set.seed(1000)
values <- runif(50)
values[sample(1:length(values), 10)] <- NA
ind.var <- data.frame(matrix(values, ncol=5))
colnames(ind.var) <- paste0(“X”, 1:5)
# a dependent var (Y)
dep <- runif(10)
dep

# create regression between each column in the dataframe against dep variable
lms3 <- lapply(colnames(ind.var), function(x) {
form.reg <- paste0(“dep~”,x)
lm(form.reg, data=ind.var)
})
lms3

# the secret lies in these 2 lines — see how this happens to explain above
lapply(colnames(ind.var), function(x) {
form.reg<-paste0(“dep~”,x)})

### USING apply ONLY, and see how the results differ
# create regression between each column in Y and X
lms4 <- apply(ind.var,2, function(x) {
lm(dep~x,data=ind.var)
})

lms4
lm(dep~X5,data=ind.var)
lm(dep~X4,data=ind.var)

 

########## END #########################################

Programming in R

http://manuals.bioinformatics.ucr.edu/home/programming-in-r#TOC-Functions

General Overview

One of the main attractions of using the R (http://cran.at.r-project.org) environment is the ease with which users can write their own programs and custom functions. The R programming syntax is extremely easy to learn, even for users with no previous programming experience. Once the basic R programming control structures are understood, users can use the R language as a powerful environment to perform complex custom analyses of almost any type of data.