Nizamuddin Siddiqui

Nizamuddin Siddiqui

1,958 Articles Published

Articles by Nizamuddin Siddiqui

Page 94 of 196

How to extract all string values from a vector in R with maximum lengths?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 06-Feb-2021 211 Views

If we have a string vector then all the values in the vector are not likely to be of same size and we might be looking for smaller size or bigger size values. Therefore, if we want to extract all string values from a vector in R with maximum lengths even if there are duplicates can be done by using the max and nchar function as shown in the below examples.Example Live Demox1

Read More

How to find the groupwise number of positive and negative values in an R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 06-Feb-2021 974 Views

To find the groupwise number of positive and negative values in an R data frame, we can use mutate function of dplyr package. For example, if we have a data frame called df with one categorical column x and one numerical column y then the number of positive and negative values for categorical column can be found by using the below command −df%>%group_by(x)%>%mutate(positive=sum(y>0),negative=sum(y

Read More

How to create a linear model with interaction term only in R?

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

To create a linear model with interaction term only, we can use the interaction variable while creating the model. For example, if we have a data frame called df that has two independent variables say V1 and V2 and one dependent variable Y then the linear model with interaction term only can be created as lm(Y~V1:V2,data=df).Consider the below data frame −Example Live Demox1

Read More

How to create a perpendicular arrow in base R plot?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 06-Feb-2021 292 Views

To create a perpendicular arrow in base R plot, we can use arrows function. There are five arguments of arrows function that will be used to create the perpendicular arrow. The first four values define the position of the arrow and the last argument xpd allows R to create the arrow. Check out the below examples to understand how it works.Exampleplot(1:10) arrows(1,-1,1,0,xpd=TRUE)OutputExampleplot(1:10) arrows(1,-1,1,2,xpd=TRUE)OutputExampleplot(1:10) arrows(2,-1,2,2,xpd=TRUE)Output

Read More

How to fill histogram bars using ggplot2 in R with different colors?

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

When we create histogram using ggplot2 we need to pass the number of bins we want to have in the histogram and on the basis of these bin numbers the histogram will be created, these bin numbers are actually the number of bars we will have in the histogram. To fill those bars with different colors, we need to use fill argument and pass a range of values equal to the number of bins as shown in the below example.Consider the below data frame −x

Read More

How to display mean in a histogram using ggplot2 in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 06-Feb-2021 5K+ Views

To display mean in a histogram using ggplot2, we can use geom_vline function where we need to define the x-intercept value as the mean of the column for which we want to create the histogram. Also, we can change the size of the line for mean in the histogram by using size argument inside geom_vline function.Consider the below data frame −x

Read More

How to change the space between bars in a barplot in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 06-Feb-2021 857 Views

By default, the space between bars is equal irrespective of the number of bars in the plot. If we want to have different space between bars then space arguments need to be used inside the barplot function but the first value does not make an impact because the first space is fixed between Y-axis and the first bar. For example, if we have a vector x that contains three values then the barplot with different space between bars can be created by using the below command −barplot(x,space=c(0.5,0.1,0.5))Example Live Demox

Read More

How to extract the residuals and predicted values from linear model in R?

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

The residuals are the difference between actual values and the predicted values and the predicted values are the values predicted for the actual values by the linear model. To extract the residuals and predicted values from linear model, we need to use resid and predict function with the model object.Consider the below data frame −Example Live Demox1

Read More

How to create a qqplot with confidence interval in R?

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

A qqplot is the plot of quantiles that helps to understand whether the supplied data comes from the specified distribution, mostly it is used to check whether the data follows normal distribution or not. If we want to create the qqplot with confidence interval then qqPlot function of car package can be used as shown in the below example.Consider the below data frame −Example Live Demox

Read More

How to convert a date to quarter and year in R?

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

Most of the times the date data is available only in date format and there is not information about the quarter of the year. The quarter of the year is required when we want compare the quarters or do some time series analysis. For the conversion of date into quarter and year can be done by using as.yearqtr function from zoo package as shown in the below examples.Examplelibrary(zoo) as.yearqtr("2021-01-19", format="%Y-%m-%d")Output[1] "2021 Q1"as.yearqtr("2021-04-19", format="%Y-%m-%d")[1] "2021 Q2"as.yearqtr(c("2021-04-19", "2020-05-24", "2020-11-09"), format="%Y-%m-%d")[1] "2021 Q2" "2020 Q2" "2020 Q4" Example Live Demox1

Read More
Showing 931–940 of 1,958 articles
« Prev 1 92 93 94 95 96 196 Next »
Advertisements