Base-R expressions to get some basic things done in less than 30 seconds.
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
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 |
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
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
---|---|---|---|---|---|---|---|---|---|
-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 |
plot(iris[,1:4])
cor(iris[,1:4])
Sepal.Length | Sepal.Width | Petal.Length | Petal.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 |
dim(iris)
- 150
- 5
t(iris)
Sepal.Length | 5.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.Width | 3.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.Length | 1.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.Width | 0.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 |
Species | setosa | setosa | setosa | setosa | setosa | setosa | setosa | setosa | setosa | setosa | ... | virginica | virginica | virginica | virginica | virginica | virginica | virginica | virginica | virginica | virginica |
table(iris$Species)
setosa versicolor virginica
50 50 50
aggregate(Sepal.Width ~ Species, iris,FUN=mean)
aggregate(Sepal.Width ~ Species, iris,FUN=max)
Species | Sepal.Width |
---|---|
setosa | 3.428 |
versicolor | 2.770 |
virginica | 2.974 |
Species | Sepal.Width |
---|---|
setosa | 4.4 |
versicolor | 3.4 |
virginica | 3.8 |
my_name_is <- function(name) {paste('My name is ',name)} #declaration
my_name_is('Optimus Prime') #Function Call
'My name is Optimus Prime'
Sys.Date()
Sys.time()
2017-12-12
[1] "2017-12-12 20:17:28 IST"
bool_vector <- c(T,F,T,T)
+bool_vector
- 1
- 0
- 1
- 1
sentence <- 'This is a sample sentence whose length I am trying to find'
nchar(sentence)
58
sentence <- 'This is a sample sentence whose length I am trying to find'
length(unlist(strsplit(sentence,' ')))
12
sentence <- 'This is a sample sentence whose length I am trying to find'
data.frame(words = unlist(strsplit(sentence,' ')), length = nchar(unlist(strsplit(sentence,' '))))
words | length |
---|---|
This | 4 |
is | 2 |
a | 1 |
sample | 6 |
sentence | 8 |
whose | 5 |
length | 6 |
I | 1 |
am | 2 |
trying | 6 |
to | 2 |
find | 4 |
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}}))
- 'sample'
- 'sentence'
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'
sentence <- 'I good and I know a good person'
gsub('good','smart',sentence)
'I smart and I know a smart person'