Found 33676 Articles for Programming

How to fix the coefficient of an independent variable in R?

Nizamuddin Siddiqui
Updated on 06-Nov-2020 11:09:08

655 Views

While doing the analysis we might know the variation in an independent variable or we might want to understand how other independent variables behave if we fix a certain variable. Therefore, we can fix the coefficient of an independent variable while creating the model and this can be done by using offset function with the coefficient of the variable for which we want to fix the value of the coefficient.ExampleConsider the below data frame:Live Demo> set.seed(854) > x1 x2 y1 df1 df1Output x1 x2 ... Read More

How to create a barplot with gaps on Y-axis scale in R?

Nizamuddin Siddiqui
Updated on 06-Nov-2020 11:03:03

685 Views

If we want to have gaps on Y-axis scale in a barplot then it cannot be done in base R. For this purpose, we can make use of gap.barplot function of plotrix package. The gap.barplot function is very handy, we just need to pass the vector for which we want to create the barplot and the gap values simply by using gap argument.Loading plotrix package:> library(plotrix)Example1Live Demo> x xOutput[1] 2 6 5 4 7 2 5 2 5 2 8 6 8 13 3 5 7 7 5 6> gap.barplot(x, gap=c(2, 4)) ylim 0 11Warning message:In gap.barplot(x, gap = c(2, ... Read More

How to set the diagonal elements of a matrix to 1 in R?

Nizamuddin Siddiqui
Updated on 06-Nov-2020 10:58:04

1K+ Views

First thing we need to understand is diagonal elements are useful only if we have a square matrix, otherwise it would not make sense to set diagonal elements, this is known to almost all mathematicians but some freshman might get confused because we can create diagonal in a non-square matrix which should not be called a diagonal. In R, we can set the diagonal elements of a matrix to 1 by using diag function.Example1Live Demo> M1 M1Output     [, 1] [, 2] [, 3] [, 4] [, 5] [1, ]   1    6   11   16   21 ... Read More

How to add a horizontal line in a boxplot created in base R?

Nizamuddin Siddiqui
Updated on 06-Nov-2020 10:49:01

4K+ Views

A boxplot in base R already consists three horizontal lines that represents minimum, median, and the maximum but we might to create an extra horizontal to showcase some threshold value. For example, we might to create a horizontal line at 2 to understand the variation in values that are greater than say 2. This can be done very easily by using abline function after creating the boxplot.Example1Live Demo> x boxplot(x) > abline(h=1)Output:Example2Live Demo> y boxplot(y) > abline(h=15)Output:Example3Live Demo> z boxplot(z) > abline(h=3)Output:

How to create normal quantile-quantile plot for a logarithmic model in R?

Nizamuddin Siddiqui
Updated on 06-Nov-2020 10:44:02

260 Views

How to create normal quantile-quantile plot for a logarithmic model in R?A logarithmic model is the type of model in which we take the log of the dependent variable and then create the linear model in R. If we want to create the normal quantile-quantile plot for a logarithmic model then plot function can be used with the model object name and which = 2 argument must be introduced to get the desired plot.Example1Live Demo> x1 x1Output[1]  4.735737 3.631521 5.522580 5.538314 5.580952 4.341072 4.736899 2.455681 [9]  4.042295 5.534034 4.717607 6.146558 4.466849 5.444437 5.390151 4.491595 [17] 4.227620 4.223362 5.452378 5.690660 5.321716 ... Read More

How to remove multiple rows from an R data frame using dplyr package?

Nizamuddin Siddiqui
Updated on 06-Nov-2020 08:28:03

522 Views

Sometimes we get unnecessary information in our data set that needs to be removed, this information could be a single case, multiple cases, whole variable or any other thing that is not helpful in achieving our analytical objective, hence we want to remove it. If we want to remove such type of rows from an R data frame with the help of dplyr package then anti_join function can be used.ExampleConsider the below data frame:Live Demo> set.seed(2514) > x1 x2 df1 df1Output x1 x2 1 5.567262 4.998607 2 5.343063 4.931962 3 2.211267 ... Read More

How to generate passwords with varying lengths in R?

Nizamuddin Siddiqui
Updated on 06-Nov-2020 07:58:23

201 Views

To generate passwords, we can use stri_rand_strings function of stringi package. If we want to have passwords of varying length then we need to create the passwords using the particular size separately. For example, for a size or length of the password equals to 8, we can use the argument length in the stri_rand_strings function.Loading stringi package:> library(stringi)Example1> stri_rand_strings(n=5, length=8, pattern="[0-9a-zA-Z]") [1] "YkIEDYQz" "t42JCzYO" "rOE9YN8U" "2lu9AonY" "6lDUxScX"Example2> stri_rand_strings(n=20, length=8, pattern="[0-9a-zA-Z]") [1] "glH3ysoX" "X0Sgvg3F" "P3YOePTa" "45GOb2hA" "tLCwszus" "CerCi1ks" [7] "UtFwzrSc" "pG8AJCQX" "NTCdMRHj" "5thI1wKb" "Ic8Rol1Y" "JakWa1Wd" [13] "9AfeXo7T" "SFJVn9XV" "lIRhLbJ9" "DNFyAbkJ" "jV4jJRZk" "IthkzfEU" [19] "talj9nBq" "Nak9Tidh"Example3> ... Read More

How to find and filter Duplicate rows in Pandas ?

Kiran P
Updated on 10-Nov-2020 09:38:28

7K+ Views

Sometimes during our data analysis, we need to look at the duplicate rows to understand more about our data rather than dropping them straight away.Luckily, in pandas we have few methods to play with the duplicates..duplciated()This method allows us to extract duplicate rows in a DataFrame. We will use a new dataset with duplicates. I have downloaded the Hr Dataset from link.import pandas as pd import numpy as np # Import HR Dataset with certain columns df = pd.read_csv("https://raw.githubusercontent.com/sasankac/TestDataSet/master/HRDataset.csv", usecols = ("Employee_Name""PerformanceScore", "Position", "CitizenDesc")) #Sort the values on employee name and make it permanent df.sort_values("Employee_Name"inplace = True) df.head(3)Employee_NamePositionCitizenDescPerformanceScore0AdinolfiProduction ... Read More

How to select subsets of data In SQL Query Style in Pandas?

Kiran P
Updated on 10-Nov-2020 06:52:12

356 Views

IntroductionIn this post, I will show you how to perform Data Analysis with SQL style filtering with Pandas. Most of the corporate company’s data are stored in databases that require SQL to retrieve and manipulate it. For instance, there are companies like Oracle, IBM, Microsoft having their own databases with their own SQL implementations.Data scientists have to deal with SQL at some stage of their career as the data is not always stored in CSV files. I personally prefer to use Oracle, as the majority of my company’s data is stored in Oracle.Scenario – 1 Suppose we are given a ... Read More

How to use String Format to show decimal up to 2 places or simple integer in C#?

Nizamuddin Siddiqui
Updated on 05-Nov-2020 13:56:19

4K+ Views

Converts the value of objects to strings based on the formats specified and inserts them into another string.Namespace:System Assembly:System.Runtime.dllEach overload of the Format method uses the composite formatting feature to include zero-based indexed placeholders, called format items, in a composite format string. At run time, each format item is replaced with the string representation of the corresponding argument in a parameter list. If the value of the argument is null, the format item is replaced with String.Empty.Exampleclass Program{    static void Main(string[] args){       int number = 123;       var s = string.Format("{0:0.00}", number);     ... Read More

Advertisements