Skip to content

Instantly share code, notes, and snippets.

@SajibDevnath
SajibDevnath / split.R
Last active January 20, 2021 04:19
Splitting a data.frame to multiple worksheets
# Read data
df <- readxl::read_excel("df.xlsx")
# Splitting the data.frame
newlist <- vocab %>% split((1:nrow(df))%/%50) # 50 - number of rows per sheet
# Writing to a workbook
writexl::write_xlsx(newlist, "new_df.xlsx")
```r
library(tidyverse)
library(stringr)
library(Cairo)
library(scales)
library(showtext)
font_add_google(name = "Playfair Display", family = "playfair")
hike_data <- readr::read_rds(url('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-11-24/hike_data.rds'))
@SajibDevnath
SajibDevnath / permutation.js
Created October 12, 2015 10:29
Permutation in javascript
function permutator(inputArr) {
var results = [];
function permute(arr, memo) {
var cur;
memo = memo || [];
for (var i = 0; i < arr.length; i++) {
cur = arr.splice(i, 1);
@SajibDevnath
SajibDevnath / ntersect.js
Last active August 29, 2015 21:03
Intersect multiple arrays
function intersect_all(lists) {
if (lists.length === 0) return [];
else if (lists.length == 1) return lists[0];
function intersection(array1, array2) {
return array1.filter(function(n) {
return array2.indexOf(n) != -1;
});
}
@SajibDevnath
SajibDevnath / numbervalidation.js
Created August 29, 2015 10:22
USA phone number validation
function telephoneCheck(str) {
// Good luck!
if (str.search(/1?\s?\(?[\d]{3}\)?-?\s?[\d]{3}-?\s?[\d]{4}/) === 0) {
return true;
} else {
return false;
}
}
@SajibDevnath
SajibDevnath / toLowerCase.js
Created August 27, 2015 07:49
Converting to lowerCase with regEx
function toTitleCase(str) {
return str.replace(/\w\S*/g, function(txt){
return txt.charAt(0).toLowerCase() + txt.substr(1).toLowerCase();
});
}
@SajibDevnath
SajibDevnath / filterarray.js
Created August 26, 2015 06:47
Filter out duplicate element from an array
var arr = [2,5,9,5,2,6,9];
var uniqueArray = arr.filter(function(item, pos) {
return arr.indexOf(item) == pos;
})
console.log(uniqueArray);
@SajibDevnath
SajibDevnath / diffarrays.js
Created August 26, 2015 06:10
Comparing two arrays
// sol 1
function diff(arr1, arr2) {
var a=[], newArr=[];
for(var i=0;i<arr1.length;i++)
a[arr1[i]]=true;
for(var i=0;i<arr2.length;i++)
if(a[arr2[i]]) delete a[arr2[i]];
else a[arr2[i]]=true;
for(var k in a)
newArr.push(k);
@SajibDevnath
SajibDevnath / timer.js
Created August 25, 2015 20:52
Timer function The real one!
function st(min) {
var totalSeconds = min * 60 + 1;
var minutes = 0;
var seconds = 0;
var content = document.getElementById('container');
content.innerHTML = '25' + ' : ' + '00';
return function timer() {
if (totalSeconds > 0) {
if (totalSeconds < 59) {
@SajibDevnath
SajibDevnath / reduce.js
Last active August 29, 2015 14:28
Array Reduce add() two arrays with prototype
Array.prototype.add = function () {
return this.reduce ( function (a, b){ return a.concat(b); });
}
var total = [[0, 1],[2, 3]].add();
document.write(total)