Server Side Programming Articles - Page 1512 of 2646

How to create histogram of all columns in an R data frame?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 08:57:46

15K+ Views

To create histogram of all columns in an R data frame, we can use hist.data.frame function of Hmisc package. For example, if we have a data frame df that contains five columns then the histogram for all the columns can be created by using a single line code as hist.data.frame(df).ExampleConsider the below data frame − Live Demoset.seed(9) x1

How to find the frequency of NA values per row in an R data frame?

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

510 Views

Since column represent variables, we often find missing values in the columns of a data frame but we may want to find missing values(NA) for cases as well so that we can replace them based on case characteristic instead of the distribution of the variable. In R, we can use rowSums with apply function.ExampleConsider the below data frame − Live Demoset.seed(8) x1

How to find the maximum value for each column of a matrix in R?

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

5K+ Views

To find the maximum value for each column of a matrix, we need to use apply function. For example, if we have a matrix M that contains 2 rows and 2 columns with values 1, 2 in the first row and 3, 4 in the second row then the maximum for each of the columns in that matrix can be found by using the syntax; apply(M,2,max), hence the result will be 3, 4.Example Live DemoM1−-matrix(1:36,ncol=6) M1Output   [,1] [,2] [,3] [,4] [,5] [,6] [1,] 1    7    13   19   25   31 [2,] 2    8    14   20   26   32 [3,] 3    9    15   21   27   33 [4,] 4    10   16   22   28   34 [5,] 5    11   17   23   29   35 [6,] 6    12   18   24   30   36Exampleapply(M1,2,max)Output[1] 6 12 18 24 30 36Example Live DemoM2

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

426 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

595 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

575 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

111 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

335 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

Advertisements