How to Use Mean() In R with Examples

mean() can be used to calculate mean in R. The following is the basic statement. mean(x, trim = 0, na.rm = FALSE) x: It is an R object.  trim: It is in the range of (0 to 0.5). The number indicates the percentage of observations to be trimmed from each end of sorted x, before the mean is computed. … Read more

Categories R

How to Interpret Interaction Effects in Linear Regression (4 Steps)

This tutorial shows how to interpret interaction effects in linear regression models. In summary, there are two perspectives, (a) mean difference perspective and (b) slope difference perspective. Interpret Interaction Effect in Linear Regression (a) Mean Difference Perspective: One way to interpret interaction effects in linear regression is based on mean differences. A significant interaction means … Read more

How to Plot 2-Way ANOVA Interaction in R (with Example)

This tutorial shows how to plot the figure for the interaction in 2-way ANOVA in R. Steps of Plotting 2-Way ANOVA Interaction in R Step 1: Prepare the data The following is the data being used for the examples. It has two categorical variables (i.e., City and Brand) and one continuous variable (i.e., sales). Output: … Read more

Categories R

How to Use stringsAsFactors in R

stringsAsFactors decides the strings will be considered as factors or just strings or characters. The following uses two examples to explains the difference. Example 1: stringsAsFactors= FALSE The following sets stringsAsFactors= FALSE. We can see that the data type of the column of Names is characters. (chr represents characters.) Output: Names Ages 1 Jack 32 … Read more

Categories R

How to Calculate Means by Group in R (2 Examples)

This tutorial is about how to calculate means by group in R. There are two methods of calculating means by groups in R, either aggregate() or dplyr. Method 1: Use base R aggregate(df$col_being_aggregated, list(df$col_1_groupby,df$col_2_groupby,…), FUN=mean) Method 2: Use package of dplyr library(dplyr) df%>% group_by(col_1_groupby,col_2_groupby,…) %>% summarise_at(vars(col_being_aggregated), list(name = mean)) Example for Method 1 The following … Read more

Categories R

How to Avoid Including Dataframe Index when Saving as CSV file in Python

To avoid including the index column when saving as a CSV file, you can add index=False in df.to_csv() to avoid including the index clumn when saving a Python Pandas dataframe as a CSV file. df.to_csv(‘filename.csv’, index=False) Output: brands models 0 Tesla Model 3 1 Toyota RAV4 The following code with index=False is not to include … Read more

How to Convert 2-D Arrays to 1-D Arrays

There are 3 methods to convert 2-D arrays to 1-D ones in Numpy. The following shows the key syntax. Method 1: numpy.ravel() Method 2: ndarray.flatten(order=’C’) Method 3: ndarray.reshape(-1) Example 1 (Method 1): We can use numpy.ravel() to convert 2-D arrays to 1-D ones. Below is the example. Output: 2-D array: [[ 5 2 3 4 … Read more

How to Fix: Data must be 1-dimensional

You might encounter the following error when trying to convert Numpy arrays to a pandas dataframe. Exception: Data must be 1-dimensional 1. Reproduce the Error Output: Exception: Data must be 1-dimensional 2. Why the Error Happens It happens because pd.DataFrame is expecting to have 1-D numpy arrays or lists, since it is how columns within … Read more