Find Mean of Numerical Column by Two Categorical Columns in R Data Frame

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

1K+ Views

If we have two categorical columns along with a numerical column in an R data frame then we can find the mean of the numerical column by using the combination of the categorical columns with the help of aggregate function. For example, if a data frame df contains a numerical column X and two categorical columns C1 and C2 then the mean of X can be found for the combinations of C1 and C2 by using the below command −aggregate(X~C1+C2,data=df,FUN="mean")ExampleConsider the below data frame −C1

Add Variable to Model in Base R

Nizamuddin Siddiqui
Updated on 07-Dec-2020 06:25:50

1K+ Views

If we want to add variables to the model in base R then update function can be used. The update function will update the previous modle by adding the new variable and this variable can be a single variable as well as an interaction of the two or more also any possible transformation of the existing variables.ExampleConsider the below data frame − Live Demox1

Find Range for 95% of All Values in an R Vector

Nizamuddin Siddiqui
Updated on 07-Dec-2020 06:21:52

1K+ Views

The range for 95% of all values actually represents the middle 95% values. Therefore, we can find the 2.5th percentile and 97.5th percentile so that the range for middle 95% can be obtained. For this purpose, we can use quantile function in R. To find the 2.5th percentile, we would need to use the probability = 0.025 and for the 97.5th percentile we can use probability = 0.0975.Example Live Demox1

Convert NA's in Sequence to a Single NA in an R Vector

Nizamuddin Siddiqui
Updated on 07-Dec-2020 06:19:31

130 Views

Sometimes values are missing in a sequence and R program records them as NA (Not Available). In this type of situation, we might want to replace consecutive NA records with single NA value. This can be done by using is.na along with diff function as shown in the below examples.Example Live Demox1

Create Side-by-Side Histograms in Base R

Nizamuddin Siddiqui
Updated on 07-Dec-2020 06:17:24

2K+ Views

To create side by side histograms in base R, we first need to create a histogram using hist function by defining a larger limit of X-axis with xlim argument. After that we can create another histogram that has the larger mean and smaller standard deviation so that the bars do not clash with each other and add=T argument must also be added inside the second hist function.Example Live Demohist(rnorm(5000,mean=5,sd=2.1),col="green",xlim=c(1,20))OutputExamplehist(rnorm(5000,mean=15,sd=1.25),col="red",add=T)Output

Identify Duplicate Values in a Column of Matrix in R

Nizamuddin Siddiqui
Updated on 07-Dec-2020 06:15:50

460 Views

We can easily identify duplicate values in a matrix by using duplicated function but it does not specify that the first occurrence is also duplicated. Therefore, we need to use it with OR sign | and the argument fromLast = TRUE of duplicated function so that the first occurrence of the duplicated values will be also identified as duplicate.Example Live DemoM1

Compare Two Columns in an R Data Frame for Exact Match

Nizamuddin Siddiqui
Updated on 07-Dec-2020 06:12:05

7K+ Views

Sometimes analysis requires the user to check if values in two columns of an R data frame are exactly the same or not, this is helpful to analyze very large data frames if we suspect the comparative values in two columns. This can be easily done with the help of ifelse function.ExampleConsider the below data frame − Live Demox1

Create Scatterplot with Regression Line in R Using ggplot2

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

328 Views

To create a regression line with 0 intercept and slope 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 -1 to 1 then the scatterplot with regression line with 0 intercept and slope equals to 1 can be created as −ggplot(df,aes(x,y))+geom_point()+geom_abline()+lims(x=c(-1,1),y=c(-1,1))ExampleConsider the below data frame − Live Demox

Convert Time Series Object to Vector in R

Nizamuddin Siddiqui
Updated on 07-Dec-2020 06:07:11

2K+ Views

To convert a time series object into a vector, we just need to read that object with as.numeric and store it in some other object or in the same object. For example if we have a time series object x then it can be converted to a vector by using x

Convert Multiple Columns in R Data Frame to a Single Numerical Column

Nizamuddin Siddiqui
Updated on 07-Dec-2020 06:04:55

1K+ Views

When we receive data from any source, it is highly likely that it would not be a perfect data set for the intended analysis, therefore, we need to perform some cleaning or mining based on the characteristics of the data. For example, if we have a column name of a data frame as factor levels of a numerical variable then we might want to convert that data frame in such a way that numerical values are stored in a single column and the column names are stored in another column that will represent a factor so that we can apply ... Read More

Advertisements