30-seconds-of-r-code icon indicating copy to clipboard operation
30-seconds-of-r-code copied to clipboard

Collection of small base-R expressions (r snippets)

30 seconds of r-code

Base-R expressions to get some basic things done in less than 30 seconds.

Related:

Table of contents

  • Creating a 10*10 matrix with random numbers
  • Matrix to Dataframe
  • Scatter Plot to understand Correlation
  • Correlation Matrix
  • Dimension of a dataframe
  • Transposing a dataframe
  • Group Count
  • Grouped Airthmetic Operation
  • New Function Creationg (String Concatenation)
  • Current Time and Date
  • Boolean to Integer Typecasting
  • Counting Number of Characters in a string
  • Counting Number of Words in a string
  • Length of each word in a string
  • Extracting Words starting with 's' in a string
  • String to Lower Case and to Upper Case
  • String Find and Replace

Creating a 10*10 matrix with random numbers

set.seed(123)
new_matrix <- matrix(rnorm(100), nrow = 10, ncol = 10)
new_matrix
-0.56047565 1.2240818 -1.0678237 0.42646422 -0.69470698 0.25331851 0.37963948 -0.4910312 0.005764186 0.9935039
-0.23017749 0.3598138 -0.2179749 -0.29507148 -0.20791728 -0.02854676 -0.50232345 -2.3091689 0.385280401 0.5483970
1.55870831 0.4007715 -1.0260044 0.89512566 -1.26539635 -0.04287046 -0.33320738 1.0057385 -0.370660032 0.2387317
0.07050839 0.1106827 -0.7288912 0.87813349 2.16895597 1.36860228 -1.01857538 -0.7092008 0.644376549-0.6279061
0.12928774 -0.5558411 -0.6250393 0.82158108 1.20796200 -0.22577099 -1.07179123 -0.6880086 -0.220486562 1.3606524
1.71506499 1.7869131 -1.6866933 0.68864025 -1.12310858 1.51647060 0.30352864 1.0255714 0.331781964-0.6002596
0.46091621 0.4978505 0.8377870 0.55391765 -0.40288484 -1.54875280 0.44820978 -0.2847730 1.096839013 2.1873330
-1.26506123 -1.9666172 0.1533731 -0.06191171 -0.46665535 0.58461375 0.05300423 -1.2207177 0.435181491 1.5326106
-0.68685285 0.7013559 -1.1381369 -0.30596266 0.77996512 0.12385424 0.92226747 0.1813035 -0.325931586-0.2357004
-0.44566197 -0.4727914 1.2538149 -0.38047100 -0.08336907 0.21594157 2.05008469 -0.1388914 1.148807618-1.0264209

Matrix to Dataframe

set.seed(123)
new_matrix <- matrix(rnorm(100), nrow = 10, ncol = 10)
new_df <- data.frame(new_matrix)
names(new_df) <- 1:10
new_df
12345678910
-0.56047565 1.2240818 -1.0678237 0.42646422 -0.69470698 0.25331851 0.37963948 -0.4910312 0.005764186 0.9935039
-0.23017749 0.3598138 -0.2179749 -0.29507148 -0.20791728 -0.02854676 -0.50232345 -2.3091689 0.385280401 0.5483970
1.55870831 0.4007715 -1.0260044 0.89512566 -1.26539635 -0.04287046 -0.33320738 1.0057385 -0.370660032 0.2387317
0.07050839 0.1106827 -0.7288912 0.87813349 2.16895597 1.36860228 -1.01857538 -0.7092008 0.644376549-0.6279061
0.12928774 -0.5558411 -0.6250393 0.82158108 1.20796200 -0.22577099 -1.07179123 -0.6880086 -0.220486562 1.3606524
1.71506499 1.7869131 -1.6866933 0.68864025 -1.12310858 1.51647060 0.30352864 1.0255714 0.331781964-0.6002596
0.46091621 0.4978505 0.8377870 0.55391765 -0.40288484 -1.54875280 0.44820978 -0.2847730 1.096839013 2.1873330
-1.26506123 -1.9666172 0.1533731 -0.06191171 -0.46665535 0.58461375 0.05300423 -1.2207177 0.435181491 1.5326106
-0.68685285 0.7013559 -1.1381369 -0.30596266 0.77996512 0.12385424 0.92226747 0.1813035 -0.325931586-0.2357004
-0.44566197 -0.4727914 1.2538149 -0.38047100 -0.08336907 0.21594157 2.05008469 -0.1388914 1.148807618-1.0264209

Scatter Plot to understand Correlation

plot(iris[,1:4])

png

Correlation Matrix

cor(iris[,1:4])
Sepal.LengthSepal.WidthPetal.LengthPetal.Width
Sepal.Length 1.0000000-0.1175698 0.8717538 0.8179411
Sepal.Width-0.1175698 1.0000000-0.4284401-0.3661259
Petal.Length 0.8717538-0.4284401 1.0000000 0.9628654
Petal.Width 0.8179411-0.3661259 0.9628654 1.0000000

Dimension of a dataframe

dim(iris)
  1. 150
  2. 5

Transposing a dataframe

t(iris)
Sepal.Length5.1 4.9 4.7 4.6 5.0 5.4 4.6 5.0 4.4 4.9 ... 6.7 6.9 5.8 6.8 6.7 6.7 6.3 6.5 6.2 5.9
Sepal.Width3.5 3.0 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ... 3.1 3.1 2.7 3.2 3.3 3.0 2.5 3.0 3.4 3.0
Petal.Length1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ... 5.6 5.1 5.1 5.9 5.7 5.2 5.0 5.2 5.4 5.1
Petal.Width0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ... 2.4 2.3 1.9 2.3 2.5 2.3 1.9 2.0 2.3 1.8
Speciessetosa setosa setosa setosa setosa setosa setosa setosa setosa setosa ... virginicavirginicavirginicavirginicavirginicavirginicavirginicavirginicavirginicavirginica

Group Count

table(iris$Species)
    setosa versicolor  virginica 
        50         50         50 

Grouped Airthmetic Operation

aggregate(Sepal.Width ~ Species, iris,FUN=mean) 
aggregate(Sepal.Width ~ Species, iris,FUN=max)
SpeciesSepal.Width
setosa 3.428
versicolor2.770
virginica 2.974
SpeciesSepal.Width
setosa 4.4
versicolor3.4
virginica 3.8

New Function Creationg (String Concatenation)

my_name_is <- function(name) {paste('My name is ',name)} #declaration
my_name_is('Optimus Prime') #Function Call

'My name is Optimus Prime'

Current Time and Date

Sys.Date()
Sys.time()

[1] "2017-12-12 20:17:28 IST"

Boolean to Integer Typecasting

bool_vector <- c(T,F,T,T)
+bool_vector
  1. 1
  2. 0
  3. 1
  4. 1

Counting Number of Characters in a string

sentence <- 'This is a sample sentence whose length I am trying to find'
nchar(sentence)

58

Counting Number of Words in a string

sentence <- 'This is a sample sentence whose length I am trying to find'
length(unlist(strsplit(sentence,' ')))

12

Length of each word in a string

sentence <- 'This is a sample sentence whose length I am trying to find'
data.frame(words = unlist(strsplit(sentence,' ')), length = nchar(unlist(strsplit(sentence,' '))))
wordslength
This 4
is 2
a 1
sample 6
sentence8
whose 5
length 6
I 1
am 2
trying 6
to 2
find 4

Extracting Words starting with 's' in a string

sentence <- 'This is a sample sentence whose length I am trying to find'
unlist(lapply(unlist(strsplit(sentence, ' ')),function(x){if(startsWith(x,'s')){x}}))
  1. 'sample'
  2. 'sentence'

String to Lower Case and to Upper Case

sentence <- 'This is a sample sentence whose length I am trying to find'
casefold(sentence,F)
casefold(sentence,T)

'this is a sample sentence whose length i am trying to find'

'THIS IS A SAMPLE SENTENCE WHOSE LENGTH I AM TRYING TO FIND'

String Find and Replace

sentence <- 'I good and I know a good person'
gsub('good','smart',sentence)

'I smart and I know a smart person'