Found 33676 Articles for Programming

How to find the median of all columns in an R data frame?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 08:35:42

9K+ Views

The median is the value in a vector that divide the data into two equal parts. To find the median of all columns, we can use apply function. For example, if we have a data frame df that contains numerical columns then the median for all the columns can be calculated as apply(df,2,median).ExampleConsider the below data frame − Live Demoset.seed(7) x1

How to remove a column from a data frame that contains same value in R?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 08:30:45

384 Views

If we have only one value in all of the rows of an R data frame then we might want to remove the whole column because the effect of that column will not make any sense in the data analysis objectives. Thus, instead of removing the column we can extract the columns that contains different values.Example Live Demoset.seed(1001) x1

How to find the confidence interval for the predictive value using regression model in R?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 08:20:36

567 Views

The confidence interval for the predictive value using regression model can be found with the help of predict function, we just need to use interval argument for confidence and the appropriate level for that. For example, if we have a model M and the data frame for the values of independent variable is named as newdata then we can use the following syntax for the confidence interval −predict(M,newdata,se.fit=TRUE,interval="confidence",level=0.95)ExampleConsider the below data frame − Live Demoset.seed(1234) x1

How to remove outliers from multiple boxplots created with the help of boxplot function for columns of a data frame using single line code in R?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 08:16:08

540 Views

A data frame can have multiple numerical columns and we can create boxplot for each of the columns just by using boxplot function with data frame name but if we want to exclude outliers then outline argument can be used. For example, if we have a data frame df with multiple numerical columns that contain outlying values then the boxplot without outliers can be created as boxplot(df,outline=FALSE).ExampleConsider the below data frame: Live Demoset.seed(151) x1

How to create a scatterplot using five vectors in a single plot window without separating the plots in R?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 08:10:28

92 Views

To create more than one scatterplot in a single plot window we should create the scatterplot for first vector and then add the point of the remaining vectors by using points function and they can be displayed with different colors so that it becomes easy to differentiate among the points of the vectors.ExampleConsider the below vectors − Live Demox1

How to convert negative values in an R data frame to positive values?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 08:07:31

8K+ Views

Sometimes we need to use absolute values but few values in the data set are negative, therefore, we must convert them to positive. This can be done by using abs function. For example, if we have a data frame df with many columns and each of them having some negative values then those values can be converted to positive values by just using abs(df).ExampleConsider the below data frame − Live Demoset.seed(41) x1

How to replace one vector elements with another vector elements in R?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 07:26:30

296 Views

If the data is incorrectly recorded then we need to replace that with the correct. One such case could be reading a vector with values that belong to another vector. In this type of situation, we should replace the vectors appropriate. In R, it can be easily done by using

How to create boxplot for multiple categories in base R?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 07:20:27

2K+ Views

To create the boxplot for multiple categories, we should create a vector for categories and construct data frame for categorical and numerical column. Once the construction of the data frame is done, we can simply use boxplot function in base R to create the boxplots by using tilde operator as shown in the below example.ExampleConsider the below data frame − Live DemoCategories

How to find the median for factor levels in R?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 07:17:00

1K+ Views

The second most used measure of central tendency median is calculated when we have ordinal data or the continuous data has outliers, also if there are factors data then we might need to find the median for levels to compare them with each other. The easiest way to do this is finding summary with aggregate function.ExampleConsider the below data frame that contains one factor column − Live Demoset.seed(191) x1

How to calculate root mean square error for linear model in R?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 07:10:35

3K+ Views

To find the root mean square error, we first need to find the residuals (which are also called error and we need to root mean square for these values) then root mean of these residuals needs to be calculated. Therefore, if we have a linear regression model object say M then the root mean square error can be found as sqrt(mean(M$residuals^2)).Example Live Demox1

Advertisements