Found 2038 Articles for R Programming

How to combine vectors of equal length into a list with corresponding elements representing a single element of the list in R?

Nizamuddin Siddiqui
Updated on 08-Dec-2020 07:14:15

329 Views

To combine three vectors into a list with corresponding elements representing a single element of the list we can use mapply function. For example, if we have three vectors x, y, and z each having number of elements equal to one-hundred then the list with corresponding elements can be created by using mapply(c,x,y,z,SIMPLIFY=FALSE).Example Live Demox1

How to perform tukey HSD in base R?

Nizamuddin Siddiqui
Updated on 08-Dec-2020 07:12:32

808 Views

First thing you must remember while moving on to post hoc analysis is the null hypothesis of the analysis of variance must be rejected, so that we can claim there exists a difference in the group means. Now, once we achieve that the tukey HSD can be performed simply by using TukeyHSD function in base R.ExampleConsider the below data frame − Live Demox1

What is the shortest way to round to the integer value in R?

Nizamuddin Siddiqui
Updated on 08-Dec-2020 07:09:59

71 Views

The shortest way to round to the integer value is using trunc function. The trunc function is used to return the largest integer that is smaller than or equal to the actual value, that means it rounds downs to the nearest integer. It works as a ceiling function for negative number and floor function for positive number.Example Live Demox1

How to create a plot of quadratic regression with fitted values against X in base R?

Nizamuddin Siddiqui
Updated on 08-Dec-2020 07:08:08

146 Views

The quadratic regression model can be plotted by using the plot function but we would need to find the fitted values using the model and this can be done with the help of fitted function. For example, if we have a quadratic model M and the data has an independent variable x then the model against x can be created by using plot(x,fitted(M)).Example Live Demox1

How to create a plot in base R with dates sequence on X-axis?

Nizamuddin Siddiqui
Updated on 08-Dec-2020 07:05:33

1K+ Views

If we have a vector that contains dates as sequence that needs to be plotted on the X-axis and another vector for the response then the plot can be simply created by using the plot function. In the plot function, we would need to pass the dates as the first argument and the response vector as the second argument. Check ou the examples below to understand how it works.Example Live Demox

How to create a plot with dashed regression line in base R?

Nizamuddin Siddiqui
Updated on 08-Dec-2020 07:02:21

706 Views

To create a regression line in base R, we use abline function after creating the scatterplot but if we want to have the line dash format then lty argument must also be used with value equals to 2 after defining the regression model inside abline. For example, if we have two columns x and y stored in a data frame called df then the plot with dashed regression line can be created by using −plot(y~x, data=df) abline(lm(df$y~df$x), lty=2)ExampleConsider the below data frame − Live Demo> x y df dfOutput      x       y 1 5.243553 4.969598 2 4.681088 ... Read More

How to create scatterplot with intercept equals to 1 using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 08-Dec-2020 07:00:50

84 Views

To create a scatterplot with intercept equals to 1 using ggplot2, we can use geom_abline function but we need to pass the appropriate limits for the x axis and y axis values. For example, if we have two columns x and y in a data frame df and both have ranges starting from 0 to 5 then the scatterplot with intercept equals to 1 can be created as −ggplot(df,aes(x,y))+geom_point()+geom_abline(intercept=1)+lims(x=c(0,5),y=c(0,5))ExampleConsider the below data frame − Live Demox

How to multiply two matrices by elements in R?

Nizamuddin Siddiqui
Updated on 08-Dec-2020 06:58:46

5K+ Views

To multiply two matrices by elements in R, we would need to use one of the matrices as vector. For example, if we have two matrices defined by names M1 and M2 then the multiplication of these matrices by elements can be done by using M1*as.vector(M2). The main thing we need to remember while doing this kind of multiplication is that the number of rows in both the matrices are equal.Example Live DemoM1

How to create a plot with reversed Y-axis labels in base R?

Nizamuddin Siddiqui
Updated on 08-Dec-2020 06:40:18

1K+ Views

To create a plot with reversed Y-axis we need to use the rev function for the Y-axis labels with ylim but we would also need to define the range for the y-axis values, otherwise, R will throw an error. For example, if we have two vectors named as x and y then the plot with reversed Y-axis labels can be created by using plot(x,ylim=rev(range(y))).Example Live Demox

How to create a plot with tick marks between X-axis labels in base R?

Nizamuddin Siddiqui
Updated on 08-Dec-2020 06:37:28

4K+ Views

To create a plot with tick marks manually between X-axis values in base R, we first need to create the plot without X-axis labels then add the axis values using axis function with appropriate labels, this will create tick marks as well as labels. After this step, we would need to use the axis function again to add the tick marks without labels.Example Live Demoplot(1:10,xaxt='n',type="l") axis(1,at=1:10) axis(1,at=seq(0,11,0.2),labels=NA)OutputExample Live Demoplot(1,xaxt='n') axis(1,at=1) axis(1,at=seq(0,2,0.05),labels=NA)Output

Advertisements