Found 26504 Articles for Server Side Programming

How to convert columns of an R data frame into rows?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 09:26:48

24K+ Views

If the row values are incorrectly recorded into columns then we might want to convert the columns into rows. Thus, to convert columns of an R data frame into rows we can use transpose function t. For example, if we have a data frame df with five columns and five rows then we can convert the columns of the df into rows by using as.data.frame(t(df)).Example Live Demoset.seed(4) x1

How to create plot in R with different shape of points?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 09:09:46

1K+ Views

In base R, the plot with different shape of points can be created by using pch argument inside the plot function. The list of pch values with shape is as written below −pch = 0 display square pch = 1 display circle pch = 2 display triangle point up pch = 3 display plus pch = 4 display cross pch = 5 display diamond pch = 6 display triangle point down pch = 7 display square cross pch = 8 display star pch = 9 display diamond plus pch = 10 display circle plus pch = 11 display triangles up ... Read More

How to find the frequency of repeated and unique values in a vector in R?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 09:06:43

329 Views

If we unique values in a vector in R and they are repeated then we can find the frequency of those unique values, this will help us to understand the distribution of the values in the vector. On the basis of that distribution analysis, we can proceed with the further analysis that could be used. This can be done with the help of rle function.Example Live Demox1

How to sort a list that contains single sub-elements in decreasing order in R?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 09:00:48

124 Views

Just like a list can have multiple elements, the elements of the list can have multiple sub-elements and the size of those elements may vary as well hence a list with single sub-elements is also possible. If we have such type of list then we can sort that list in decreasing order by using order function but we also need to unlist those elements.ExampleConsider the below list − Live Demox1

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

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

14K+ 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

478 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

388 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

571 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

Advertisements