R tips and coding tricks

Here you can find some simple interesting concepts for coding in R.

How to select all numeric columns in a data frame?

df = data.frame(x = c(1:3),
                y = c('A','B','C'),
                z = c(0.1, 0.2, 0.3))
df
##   x y   z
## 1 1 A 0.1
## 2 2 B 0.2
## 3 3 C 0.3
# Select all numeric columns
df[sapply(df,is.numeric)]
##   x   z
## 1 1 0.1
## 2 2 0.2
## 3 3 0.3
Avatar
Mark Goldberg
Researcher

My research interests include epigenetics and computational biology.

Related

comments powered by Disqus