All Projects → amrrs → 30-seconds-of-r-code

amrrs / 30-seconds-of-r-code

Licence: other
Collection of small base-R expressions (r snippets)

Programming Languages

Jupyter Notebook
11667 projects

Projects that are alternatives of or similar to 30-seconds-of-r-code

30-seconds-of-code-texteditorsnippets
Files to import the 30-seconds-of-code snippets into VSCode, Atom and Sublime.
Stars: ✭ 35 (+133.33%)
Mutual labels:  snippets-collection, 30-seconds-of-code
30 Seconds Of Code
Short JavaScript code snippets for all your development needs
Stars: ✭ 89,121 (+594040%)
Mutual labels:  snippets-collection
30 Seconds Of Css
Short CSS code snippets for all your development needs
Stars: ✭ 14,945 (+99533.33%)
Mutual labels:  snippets-collection
30 Seconds Of Php
Short PHP code snippets for all your development needs
Stars: ✭ 2,461 (+16306.67%)
Mutual labels:  snippets-collection
30 Seconds Of Interviews
A curated collection of common interview questions to help you prepare for your next interview.
Stars: ✭ 9,410 (+62633.33%)
Mutual labels:  snippets-collection
Java Design Patterns
Design patterns implemented in Java
Stars: ✭ 71,923 (+479386.67%)
Mutual labels:  snippets-collection
30 Seconds Of React
Short React code snippets for all your development needs
Stars: ✭ 3,991 (+26506.67%)
Mutual labels:  snippets-collection
snippets
VSCode extension which lets you manage your code snippets without quitting your editor.
Stars: ✭ 55 (+266.67%)
Mutual labels:  snippets-collection
vscode-hyper-javascript-snippets
Visual Studio Code snippet extension for JavaScript and TypeScript
Stars: ✭ 14 (-6.67%)
Mutual labels:  snippets-collection
code-examples
Short code snippets written by our open source community!
Stars: ✭ 60 (+300%)
Mutual labels:  snippets-collection
30-seconds-of-angular
Curated collection of Angular snippets that you can understand in 30 seconds or less
Stars: ✭ 24 (+60%)
Mutual labels:  snippets-collection
30-seconds-of-csharp
Short C# code snippets for all your development needs
Stars: ✭ 132 (+780%)
Mutual labels:  snippets-collection
cheat-sheet
collection of cheat sheets
Stars: ✭ 150 (+900%)
Mutual labels:  snippets-collection
30-seconds-of-python
Short Python code snippets for all your development needs
Stars: ✭ 8,452 (+56246.67%)
Mutual labels:  snippets-collection
awesome-flutter-snippets
Awesome Flutter Snippets is a collection snippets and shortcuts for commonly used Flutter functions and classes
Stars: ✭ 136 (+806.67%)
Mutual labels:  snippets-collection
10secondsofcode
The team behind 10-seconds-of-code and official 10-seconds projects.
Stars: ✭ 41 (+173.33%)
Mutual labels:  snippets-collection
30-seconds-of-git
Short git snippets for all your development needs
Stars: ✭ 235 (+1466.67%)
Mutual labels:  snippets-collection
Snippet2
A simple Code Snippet with user account and share feature
Stars: ✭ 20 (+33.33%)
Mutual labels:  snippets-collection
30-seconds-of-code-alfredsnippets
30-seconds-of-code.alfredsnippets file which you can then be imported into Alfred 3.
Stars: ✭ 14 (-6.67%)
Mutual labels:  30-seconds-of-code

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

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()

2017-12-12

[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'

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].