Split Data Frame in R with Conditional Row Values

Nizamuddin Siddiqui
Updated on 23-Nov-2020 09:54:01

3K+ Views

The splitting of data frame is mainly done to compare different parts of that data frame but this splitting is based on some condition and this condition can be row values as well. For example, if we have a data frame df where a column represents categorical data then the splitting based on the categories can be done by using subset function as shown in the below examples.Example1Consider the below data frame:Live Demo> Country Ratings df1 df1Output Country Ratings 1 India   1 2 China   2 3 Russia  5 4 Sudan   3 5 India   5 6 China   ... Read More

Display Long X-Axis Labels in Bar Chart Using Plotly in R

Nizamuddin Siddiqui
Updated on 23-Nov-2020 09:50:16

453 Views

Plotly in R is a package specifically designed to create highly-interactive and publication-quality charts. The chart can be created by using plot_ly function of the package and there are three main arguments of plot_ly defined as x, y, and type, where x refers to the X-axis, y refers to the Y-axis and type refers to the chart type but the axes values are stored in a data frame or itself a shared.ExampleLoading plotly package:> library(plotly)Consider the below data frame:Live Demo> x y df dfOutputx y 1 United States of America 501 2 United Kingdom 510 3 Republic of China 505Creating ... Read More

Extract Columns of Data Frame as Time Series Object in R

Nizamuddin Siddiqui
Updated on 23-Nov-2020 09:47:56

471 Views

To access columns of data frame in R, we just need to use $ sign but if the data frame is converted to a time series object then all the columns will behave as a time series, hence, we cannot simply use $ sign. For this purpose, we would need to use single square brackets and pass the appropriate column inside it. Look at the below examples to understand how it works.Example 1Consider the below data frame:Live Demo> set.seed(147) > x1 x2 x3 df1 df1Outputx1 x2 x3 1 5 11 4 2 5 5 3 3 4 6 2 4 ... Read More

Create Function in R with Two Inputs

Nizamuddin Siddiqui
Updated on 23-Nov-2020 09:42:36

5K+ Views

To create a function with two inputs, we just need to provide two different arguments inside function. For example, if we want to create a function to find the square of a+b then we can use x and y inside function. Check out the below examples to understand how we can do it.Example1Live Demo> F F(x=1, y=1) > F(x=2, y=3) > F(x=c(1, 2), y=c(2, 3))Output[1] 4 [1] 25 [1] 9 25Example> F(x=rpois(50, 2), y=rpois(50, 7))Output[1] 36 169 121 36 49 100 144 169 144 81 100 256 121 121 36 64 49 225 121 [20] 16 64 100 36 ... Read More

Extract Variables of an S4 Object in R

Nizamuddin Siddiqui
Updated on 23-Nov-2020 09:38:45

3K+ Views

Suppose we want to create an S4 with defined name data and two numerical columns called by x and y then we can use setClass("data", representation(x1="numeric", x2="numeric")). Now, if we want to extract the variables of this S4 object then we would need to use @ sign instead of $ sign as in a data frame.Example1> setClass("data1", representation(x1="numeric", x2="numeric")) > data1 data1OutputAn object of class "data1" Slot "x1": [1] -0.586187627 0.853689097 -0.602612795 -2.194235741 -1.318522292 [6] -0.984882420 0.273584140 0.364691611 1.025472248 1.198547297 [11] -0.709282551 -0.001441127 -0.201348012 1.296811172 1.520093861 [16] 2.071031215 0.472877022 0.616211695 0.642165615 -0.122773000 Slot "x2": [1] 0.38902289 0.20631450 0.02105516 0.24891420 ... Read More

Remove Rows in an R Data Frame Using Row Names

Nizamuddin Siddiqui
Updated on 23-Nov-2020 09:36:10

16K+ Views

There are a lot of ways to subset an R data frame and sometimes we need to do it by removing rows. In general, the rows are removed by using the row index number but we can do the same by using row names as well. This can be done by storing the row names that should be removed in a vector and then removing through subsetting with single square brackets as shown in the below examples.ExampleConsider the below data frame:> x y row.names(df) dfOutput x y A ... Read More

Determine Isomorphic Strings in JavaScript

AmitDiwan
Updated on 23-Nov-2020 07:08:50

785 Views

Two strings (str1 and str2) are isomorphic if the characters in str1 can be replaced to get str2.For example −const str1 = 'abcde'; const str2 = 'eabdc';These two are an example of isomorphic stringsWe are required to write a JavaScript function that in two strings. The function should determine whether or not the two input strings are isomorphic.Exampleconst str1 = 'abcde'; const str2 = 'eabdc'; const isIsomorphic = (str1 = '', str2 = '') => {    if (str1.length !== str2.length) {       return false;    };    for (let i = 0;    i < str1.length; i++) ... Read More

Count Prime Numbers from 2 to N in JavaScript

AmitDiwan
Updated on 23-Nov-2020 07:07:59

414 Views

We are required to write a JavaScript function that takes in a number, say n, as the first and the only argument.The function should then return the count of all the prime numbers from 2 upto the number n.For example −For n = 10, the output should be: 4 (2, 3, 5, 7) For n = 1, the output should be: 0Exampleconst countPrimesUpto = (num = 1) => {    if (num < 3) {       return 0;    };    let arr = new Array(num).fill(1);    for (let i = 2; i * i < num; i++) ... Read More

Finding Trailing Zeros of a Factorial in JavaScript

AmitDiwan
Updated on 23-Nov-2020 07:06:56

422 Views

Given an integer n, we have to write a function that returns the number of trailing zeroes in n!.For example −trailingZeroes(4) = 0 trailingZeroes(5) = 1 because 5! = 120 trailingZeroes(6) = 1Exampleconst num = 17; const findTrailingZeroes = num => {    let cur = 5, total = 0;    while (cur

Calculate Average from JSON Data Based on Multiple Filters in JavaScript

AmitDiwan
Updated on 23-Nov-2020 07:05:38

1K+ Views

Suppose, we have an array of objects like this −const arr = [    { "SupplierName" : "John", "Category " : "A", "Points" : 3 },    { "SupplierName" : "John", "Category " : "A", "Points" : 11 },    { "SupplierName" : "John", "Category " : "A", "Points" : undefined },    { "SupplierName" : "John", "Category " : "B", "Points" : 2 },    { "SupplierName" : "John", "Category " : "B", "Points" : 6 },    { "SupplierName" : "Praveen", "Category " : "A", "Points" : 3 },    { "SupplierName" : "Praveen", "Category " : "A", ... Read More

Advertisements