Nizamuddin Siddiqui

Nizamuddin Siddiqui

1,958 Articles Published

Articles by Nizamuddin Siddiqui

Page 82 of 196

How to subset a data frame based on a vector values in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 04-Mar-2021 4K+ Views

If we have a vector and a data frame, and the data frame has a column that contains the values similar as in the vector then we can create a subset of the data frame based on that vector. This can be done with the help of single square brackets and %in% operator. The %in% operator will help us to find the values in the data frame column that matches with the vector values. Check out the below examples to understand how it works.Example1Consider the below data frame df1 and vector v1 −Live Demo> x1 x2 df1 df1Outputx1 x2 1 ...

Read More

How to display average line for y variable using ggplot2 in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 04-Mar-2021 2K+ Views

To display the average line for y variable using ggplot2, we can use geom_hline function along with the yintercept. In the yintercept, we would need to calculate the mean of the y variable and we can also change the colour of the line using color argument inside the geom_hline function.ExampleConsider the below data frame −Live Demo> x y df dfOutputx y 1 -1.07323904 0.368641641 2 0.92531148 -0.196530651 3 -0.57433739 0.710957804 4 1.17367100 0.300110517 5 0.00769624 -1.287517035 6 0.64901161 -0.476105351 7 0.70197701 -0.683592585 8 -0.80807441 -1.716264317 9 0.10827026 0.116964308 10 -1.10451308 0.660382307 11 -0.01612692 -1.182533283 12 2.20292198 -1.890223763 13 -1.03368161 -0.526983486 ...

Read More

How to highlight a bar in base R histogram?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 04-Mar-2021 628 Views

To highlight a bar in base R histogram, we need to understand the X-axis values and pass the col argument inside hist function appropriately. We just need to put a separate value for the bar that we want to highlight and set the colouring of the rest of the bars to 0 (that is default in base R). Check out the below examples to understand how it works.Example1> x hist(x,col = c(rep(0,5),4,rep(0,5)))OutputExample2> y hist(y,col = c(rep(0,3),4,rep(0,9)))Output

Read More

How to convert a binary matrix to logical matrix in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 04-Mar-2021 1K+ Views

A binary matrix contains values such as Yes or NO, 1 or 0, or any other two values that represents opposite mostly and the globally accepted logical values are FALSE and TRUE. Therefore, to convert a binary matrix to logical matrix, we can use ifelse function and convert the one category of binary variable to appropriate logical value and for the rest returns the left-out value. This is a very easy task in R, check out the below examples to understand how it can be done.Example1Live Demo> M1 M1Output[, 1] [, 2] [1, ] "No" "Yes" [2, ] "No" "No" ...

Read More

How to extract the split string elements in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 04-Mar-2021 3K+ Views

To split string vector elements, we can use strsplit function. And if we want to extract the string elements after splitting then double and single square brackets will be used. The double square bracket will extract the string vector element and the single square will extract the element after splitting. Check out the examples to understand how it works.Example1Live Demo> x1 x1Output[1] "Tutorialspoint is an E-learning platform" [2] "E-learning is important" [3] "It helps in learning and growing at a faster rate"Example> x1 x1Output[[1]] [1] "Tutorialspoint" "is" "an" "E-learning" [5] "platform" [[2]] [1] "E-learning" "is" "important" [[3]] [1] ...

Read More

How to display upper and lower quartiles through different line in a boxplot in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 04-Mar-2021 217 Views

To display the upper and lower quartiles through different line in base R boxplot, we can use abline function but we need to find the quartiles inside abline using quantile for the respective quartiles. The lines created by using abline and quantiles and the boxplot function may not coincide because of the differences in calculation. The calculation method for boxplot is explained below −The two ‘hinges’ are versions of the first and third quartile. The hinges equal the quartiles for odd n (where n x boxplot(x)OutputExample> abline(h=quantile(x,c(0.25,0.75)),col="blue")Output

Read More

How to change the legend shape using ggplot2 in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Feb-2021 1K+ Views

By default, the shape of legend is circular but we can change it by using the guides function of ggplot2 package. For example, if we have a data frame with two numerical columns say x and y, and one categorical column Group then the scatterplot between x and y for different color values of categories in categorical column Group having different shape of legends can be created by using the below command −ggplot(df, aes(x, y, color=Group))+geom_point()+guides(colour=guide_legend(override.aes=list(shape=0)))Here, we can change the shape argument value to any value between starting from 0 to 25.Consider the below data frame −Example Live Demox

Read More

How to collapse data frame rows in R by summing using dplyr?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Feb-2021 3K+ Views

To collapse data frame rows by summing using dplyr package, we can use summarise_all function of dplyr package. For example, if we have a data frame called df that has a categorical column say Group and one numerical column then collapsing of rows by summing can be done by using the command −df%>%group_by(Group)%>%summarise_all(funs(sum))Consider the below data frame −Example Live DemoGroup

Read More

How to create a subset using character column with multiple matches in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Feb-2021 741 Views

Subsetting is one of the most important aspects of data analysis. One such situation could be subsetting the character column based on multiple values. For example, if a character column of an R data frame has 5 categories then we might want to extract only 2 or 3 or 4 values then it can be done by using the filter function of dplyr package with str_detect function of stringr package.Consider the below data frame −Example Live DemoGroup

Read More

How to find the frequency vector elements that exists in another vector in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Feb-2021 233 Views

If a vector value exists in another vector then we might want to find the frequency/count for such values in the other vector. For example, if we have two vectors say x and y, and some of the values in y exists in x as well. Therefore, we can find the frequency of values in x for y values can be found by using the command colSums(outer(x,y,"==")).Example Live Demox1

Read More
Showing 811–820 of 1,958 articles
« Prev 1 80 81 82 83 84 196 Next »
Advertisements