Found 2038 Articles for R Programming

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

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

388 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

139 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 exclude extra margin between points and the axes for a plot created by using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 19-Oct-2020 15:02:27

1K+ Views

In a plot created by using ggplot package there exists an extra area around all the sides of the plot which uses extra space, thus we might want to get rid of that space by removing that extra margin area. It can be done by setting the scale for both the axes to zero with the help of scale_x_continuous and scale_y_continuous function.Consider the below data frame −Example Live Demoset.seed(151) x

How to find the intersection between two or more lists in R?

Nizamuddin Siddiqui
Updated on 19-Oct-2020 15:01:22

7K+ Views

The intersection of lists means the elements that are unique and common between the lists. For example, if we have a list that contains 1, 2, 3, 3, 3, 2, 1 and the other list that contains 2, 2, 1, 2, 1 then the intersection will return only those elements that are common between the lists and also unique, hence for this example we will get 1 and 2. In R, we can do this by using intersection function along with Reduce function.Consider the below lists −Example Live DemoList1

How to apply manually created x-axis labels in a histogram created by hist function in R?

Nizamuddin Siddiqui
Updated on 19-Oct-2020 14:57:52

2K+ Views

When we generate a histogram in R using hist function, the x-axis labels are automatically generated but we might want to change them to values defined by researchers or by any other authority. Therefore, firstly we need to create the histogram by ignoring the labels and then axis function can be used for new values.Consider the below vector x and create a histogram of x by ignoring x-axis labels −Exampleset.seed(1999) x

How to find the sum of diagonal elements in a table in R?

Nizamuddin Siddiqui
Updated on 19-Oct-2020 14:57:23

2K+ Views

The sum of diagonal elements could be required in matrix analysis therefore, we can convert the matrix into a table and find the sum of diagonal elements. This can be easily done by using sun function by extracting diagonal elements of the table using diag function. For example, if we have a table T then the sum of diagonal elements of T can be found as sum(diag(T)).Example Live DemoTable1

How to create multiple plots of different sizes in base R?

Nizamuddin Siddiqui
Updated on 19-Oct-2020 14:45:56

287 Views

Often, we have multiple values, vectors or columns of an R data frame that needs to be plotted on a single graph so that we can compare them at the same time or they have some kind of relationship among them. Therefore, we can use layout function along with matrix function to divide the plot window as shown in the below exampleConsider the below layout and plot of individual values −Examplelayout(matrix(c(1, 2, 3, 3, 4, 5, 6, 6), nrow=4, ncol=2, byrow=FALSE)) plot(500) plot(525) plot(530) plot(531) plot(540) plot(528)OutputChanging the layout and creating the plots −Examplelayout(matrix(c(1, 2, 3, 3, 4, 5, 6, ... Read More

How to perform fisher test in R?

Nizamuddin Siddiqui
Updated on 19-Oct-2020 14:43:53

849 Views

The fisher test helps us to understand whether there exists a significant non-random relationship among categorical variables or not. It is applied on contingency tables because these tables are used to represent the frequency for categorical variables and we can apply it on a matrix as well as matrices have the similar form. In R, we can use fisher.test function to perform the fisher test.Example Live DemoM1

How to remove everything before values starting after underscore from column values of an R data frame?

Nizamuddin Siddiqui
Updated on 19-Oct-2020 14:42:05

1K+ Views

If a column in an R data frame contain string values that are separated with an underscore and stretches the size of the column values that also contain common values then it would be wise to remove underscore sign from all the values at once along with the values that is common. This will help us to read the data properly as well as analysis will become easy. For this purpose, we can use gsub functionConsider the below data frame −Example Live Demoset.seed(191) ID

How to remove rows from data frame in R based on grouping value of a particular column?

Nizamuddin Siddiqui
Updated on 19-Oct-2020 14:34:53

2K+ Views

If we have a grouping column in an R data frame and we believe that one of the group values is not useful for our analysis then we might want to remove all the rows that contains that value and proceed with the analysis, also it might be possible that the one of the values are repeated and we want to get rid of that. In this situation, we can do subsetting of the data frame using negation and single square brackets.Example Live Demoset.seed(1212) x

Advertisements