Found 2038 Articles for R Programming

How to check if all values in a vector are integer or not in R?

Nizamuddin Siddiqui
Updated on 05-Jan-2021 06:03:06

2K+ Views

To check whether all values in a vector in R are integer or not, we can round the vector using floor function then subtract the vector values from it and check whether the output is zero or not. If the output will be zero that means the value is integer otherwise it is not. The floor function returns the largest integer that is smaller or equal to the actual value. For example, if we have a vector x then it can be done as x-floor(x)==0.Example1Live Demo> x1 x1Output[1] 4 0 2 8 6 1 3 7 3 4 0 7 ... Read More

How to convert a matrix into a data frame with column names and row names as variables in R?

Nizamuddin Siddiqui
Updated on 05-Jan-2021 06:01:36

1K+ Views

To convert a matrix into a data frame with column names and row names as variables, we first need to convert the matrix into a table and then read it as data frame using as.data.frame. For example, if we have a matrix M then it can be done by using the below command −as.data.frame(as.table(M))Example1Live Demo> M1 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 ... Read More

How to create an empty data frame with fixed number of rows and without columns in R?

Nizamuddin Siddiqui
Updated on 05-Jan-2021 06:00:17

1K+ Views

To create an empty data frame with fixed number of rows but no columns, we can use data.frame function along with the matrix function. That means we need to create a matrix without any column using matrix and save it in a data frame using data.frame function as shown in the below examples.Example1Live Demo> df1 df1Outputdata frame with 0 columns and 10 rows Example2Live Demo> df2 df2Outputdata frame with 0 columns and 100 rows Example3Live Demo> df3 df3Outputdata frame with 0 columns and 39 rows Example4Live Demo> df4 df4Outputdata frame with 0 columns and 20 rows Example5Live Demo> df5 df5Outputdata ... Read More

How to create a residual plot in R with better looking aesthetics?

Nizamuddin Siddiqui
Updated on 04-Jan-2021 06:57:21

256 Views

The default residual plot can be created by using the model object name in base R but that is not very attractive. To create a residual plot with better looking aesthetics, we can use resid_panel function of ggResidpanel package. It is created in the same way as the residual plot in base R, also it results in all the relevant graph in one window.ExampleConsider the below data frame −Live Demo> x y df dfOutputx y 1 0.48508894 0.217379409 2 0.75113573 -0.657179470 3 -0.13075185 -0.549613217 4 -0.26867557 1.156736294 5 0.40407850 0.640387394 6 -0.23816272 -0.807847198 7 -0.57278583 0.600249694 8 -0.78222676 -0.711133218 9 ... Read More

How to create combinations for each string values in two vectors in R?

Nizamuddin Siddiqui
Updated on 04-Jan-2021 06:56:05

474 Views

If we have two string vectors, each containing more than two values then it becomes a little difficult to create the combinations for each string value in those two vectors. For this purpose, we can make use of do.call function paste0 and expand.grid as shown in the below examples.ExampleLive Demo> x1 y1 do.call(paste0, expand.grid(x1, y1))Output[1] "AK" "BK" "CK" "DK" "EK" "FK" "GK" "HK" "IK" "JK" "AL" "BL" "CL" "DL" "EL" [16] "FL" "GL" "HL" "IL" "JL" "AM" "BM" "CM" "DM" "EM" "FM" "GM" "HM" "IM" "JM" [31] "AN" "BN" "CN" "DN" "EN" "FN" "GN" "HN" "IN" "JN" "AO" "BO" "CO" ... Read More

How to get top values of a numerical column of an R data frame in decreasing order?

Nizamuddin Siddiqui
Updated on 04-Jan-2021 06:54:16

6K+ Views

To get the top values in an R data frame, we can use the head function and if we want the values in decreasing order then sort function will be required. Therefore, we need to use the combination of head and sort function to find the top values in decreasing order. For example, if we have a data frame df that contains a column x then we can find top 20 values of x in decreasing order by using head(sort(df$x, decreasing=TRUE), n=20).ExampleConsider the CO2 data frame in base R −Live Demo> str(CO2)OutputClasses ‘nfnGroupedData’, ‘nfGroupedData’, ‘groupedData’ and 'data.frame': 84 obs. of ... Read More

How to create a column in an R data frame that contains the multiplication of two columns?

Nizamuddin Siddiqui
Updated on 04-Jan-2021 06:52:43

2K+ Views

Sometimes we need the multiplication of two columns and create a new column so that the multiplication can be used further for analysis. For example, to calculate BMI we need mass and height and the height is squared, therefore, we would be needing the square of height. For this purpose, we can either multiply height with height or simply take the square both the ways work. Hence, if only have height column in an R data frame then we can multiply it with itself.ExampleConsider the below data frame −Live Demo> set.seed(957) > x y z df dfOutputx y z 1 ... Read More

How to change the width of whisker lines in a boxplot using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 04-Jan-2021 06:50:59

2K+ Views

In R, by default the whisker lines are as wide as the box of the boxplot but it would be great if we reduce that width or increase it because it will get attention of the viewer in that way. This can be done by using the width argument inside the stat_boxplot function of ggplot2 package. Check out the below example to understand how it works.ExampleConsider the below data frame −ExampleLive Demo> x y df dfOutputx y 1 B 5 2 B 4 3 A 6 4 A 9 5 B 2 6 B 4 7 B 6 8 B ... Read More

How to find the sum of division in R if zero exists in the vectors?

Nizamuddin Siddiqui
Updated on 04-Jan-2021 06:49:25

176 Views

To find the sum of division if zero exists in the vectors, we need to assign NA to zeros in both the vectors and then use the sum function with na.rm set to TRUE. For example, if we have two vectors x and y that contains some zeros then we can divide x by y using the below commands −x[x==0] y yOutput[1] 1 5 3 1 9 1 3 8 9 0 1 7 3 2 3 3 2 9 3 1 9 5 5 2 5 4 4 7 4 5 9 1 9 9 4 2 3 [38] ... Read More

How to identify the difference between Kolmogorov Smirnov test and Chi Square Goodness of fit test in R?

Nizamuddin Siddiqui
Updated on 04-Jan-2021 06:48:00

1K+ Views

The Chi Square Goodness of fit test is used to test whether the distribution of nominal variables is same or not as well as for other distribution matches and on the other hand the Kolmogorov Smirnov test is only used to test to the goodness of fit for a continuous data. The difference is not about the programming tool, it is a concept of statistics.ExampleLive Demo> x xOutput[1] 0.078716115 -0.682154062 0.655436957 -1.169616157 -0.688543382 [6] 0.646087104 0.472429834 2.277750805 0.963105637 0.414918478 [11] 0.575005958 -1.286604138 -1.026756390 2.692769261 -0.835433410 [16] 0.007544065 0.925296720 1.058978610 0.906392907 0.973050503Example> ks.test(x, pnorm) One-sample Kolmogorov-Smirnov test data: x ... Read More

Advertisements