Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Articles on Trending Technologies
Technical articles with clear explanations and examples
How to perform paired t test in R with a factor column in the data frame?
When we have a factor column in an R data frame that has two levels and a numerical column then we can apply paired-test on this data frame but the data must be collected for same subjects, otherwise it will not be a paired data. The t.test application on the data discussed here can be done by using the command t.test(y1~x1,data=df), where y1 is the numerical column, x1 is the factor column, and both these columns are stored in data frame called df.ExampleConsider the below data frame −x1
Read MoreHow to extract vector using different index for columns in an R matrix?
Suppose we have a matrix and a vector containing indices of equal size as the matrix then we can extract the vector from matrix using the index vector. For this purpose, we can use cbind function as shown in the below examples.Example1> M1 M1Output [,1] [,2] [1,] 4 0 [2,] 1 1 [3,] 1 2 [4,] 2 0 [5,] 3 2 [6,] 2 2 [7,] 1 6 [8,] 1 2 [9,] 3 1 [10,] 1 2 [11,] 2 3 [12,] 2 0 [13,] 3 0 [14,] 0 1 [15,] 2 4 [16,] 1 1 [17,] 3 1 [18,] 0 2 [19,] 2 1 [20,] 2 0Example> Index_M1 Index_M1Output[1] 2 1 2 1 2 2 1 1 2 1 1 2 1 1 1 1 2 2 1 1Example> M1[cbind(seq_along(Index_M1),Index_M1)]Output[1] 0 1 2 2 2 2 1 1 1 1 2 0 3 0 2 1 1 2 2 2Example2> M2 M2Output [,1] [,2] [,3] [,4] [1,] 10 9 9 11 [2,] 13 6 16 8 [3,] 11 11 8 10 [4,] 15 11 9 9 [5,] 10 8 9 9 [6,] 7 14 9 15 [7,] 8 6 8 7 [8,] 4 8 9 12 [9,] 7 12 11 10 [10,] 8 8 9 13 [11,] 9 13 11 6 [12,] 12 5 11 8 [13,] 8 6 15 8 [14,] 6 17 12 7 [15,] 8 10 9 8 [16,] 13 7 11 13 [17,] 5 10 7 7 [18,] 10 11 8 8 [19,] 5 9 9 13 [20,] 5 10 7 6Example> Index_M2 Index_M2Output[1] 3 4 3 3 3 1 3 4 4 3 1 4 3 4 4 1 2 1 1 2Example> M2[cbind(seq_along(Index_M2),Index_M2)]Output[1] 9 8 8 9 9 7 8 12 10 9 9 8 15 7 8 13 10 10 5 10
Read MoreHow to find the sum of every n values in R data frame columns?
To find the sum of every n values in R data frame columns, we can use rowsum function along with rep function that will repeat the sum for rows. For example, if we have a data frame called df that contains 4 columns each containing twenty values then we can find the column sums for every 5 rows by using the command rowsum(df,rep(1:5,each=4)).ExampleConsider the below data frame −x1
Read MoreHow to convert a matrix column into list in R?
To convert a matrix column into list can be done by using the apply function. We will have to read the columns of the matrix as list by using as.list function. For example, if we have a matrix called M then the columns in M can be converted into list by using the command apply(M, 2, as.list).Example1> M1 M1Output [, 1] [, 2] [1, ] -1.3256074 -0.07328026 [2, ] 1.1997584 -1.06542989 [3, ] -0.2214659 -1.75903298 [4, ] 1.4446361 -0.12859397 [5, ] -0.1504967 0.97264445Converting M1 columns to a list −> apply(M1, 2, as.list)Output[[1]] [[1]][[1]] ...
Read MoreHow to get the combinations for a range of values with repetition in R?
The combination of values with repetition is the combination where the values can be repeated when creating the combination. For example, if we have three values say 1 and 2 then the combination of these values with repetition will be as follows −1 1 2 1 1 2 2 2For this purpose, we can use expand.grid function as shown in the below examples.Example 1expand.grid(rep(list(1:2),2))Output Var1 Var2 1 1 1 2 2 1 3 1 2 4 2 2Example2expand.grid(rep(list(1:2),3))Output Var1 Var2 Var3 1 1 1 1 2 2 1 1 3 1 2 1 4 2 2 1 5 1 1 2 6 2 1 2 7 1 2 2 8 2 2 2Example3expand.grid(rep(list(1:2),4))Output Var1 Var2 Var3 Var4 1 1 1 1 1 2 2 1 1 1 3 1 2 1 1 4 2 2 1 1 5 1 1 2 1 6 2 1 2 1 7 1 2 2 1 8 2 2 2 1 9 1 1 1 2 10 2 1 1 2 11 1 2 1 2 12 2 2 1 2 13 1 1 2 2 14 2 1 2 2 15 1 2 2 2 16 2 2 2 2Example4expand.grid(rep(list(1:2),5))Output Var1 Var2 Var3 Var4 Var5 1 1 1 1 1 1 2 2 1 1 1 1 3 1 2 1 1 1 4 2 2 1 1 1 5 1 1 2 1 1 6 2 1 2 1 1 7 1 2 2 1 1 8 2 2 2 1 1 9 1 1 1 2 1 10 2 1 1 2 1 11 1 2 1 2 1 12 2 2 1 2 1 13 1 1 2 2 1 14 2 1 2 2 1 15 1 2 2 2 1 16 2 2 2 2 1 17 1 1 1 1 2 18 2 1 1 1 2 19 1 2 1 1 2 20 2 2 1 1 2 21 1 1 2 1 2 22 2 1 2 1 2 23 1 2 2 1 2 24 2 2 2 1 2 25 1 1 1 2 2 26 2 1 1 2 2 27 1 2 1 2 2 28 2 2 1 2 2 29 1 1 2 2 2 30 2 1 2 2 2 31 1 2 2 2 2 32 2 2 2 2 2
Read MoreHow to change the order of boxplot by means using ggplot2 in R?
To change the order of boxplot by means using ggplot2, we can use reorder function inside aes of ggplot. For example, if we have a data frame called df that contains two columns say x (categorical) and y(count) then the boxplot ordered by means can be created by using the command ggplot(df, aes(x=reorder(x, y, mean), y))+geom_boxplot()ExampleConsider the below data frame −> x y df dfOutput x y 1 A 22 2 A 17 3 A 20 4 A 36 5 A 34 6 A 25 7 A 25 8 A 30 9 A 23 10 A 29 11 B 8 ...
Read MoreHow to create a blank column with randomization in an R data frame?
To create a blank column with randomization in an R data frame, we can use sample function and pass the blanks with single space. For example, if we want to create a vector say x that will be added in the data frame can be created by using the command −x
Read MoreHow to display p-value with coefficients in stargazer output for linear regression model in R?
To display p-value in stargazer output for linear regression model, we can use the report argument. For example, if we have a model called RegressionModel then to display the p-value with coefficients can be done by using the below command −stargazer(RegressionModel,type="text",report=("vc*p"))ExampleConsider the below data frame −x1
Read MoreHow to apply a manually created function to two columns in an R data frame?
Suppose we created a function that can take two different values at a time then we can apply that function to two columns of an R data frame by using mapply. For example, if we have a manually created function say func that multiply two values then we can apply it to a data frame called df that has two columns x and y by using the below command −mapply(func, df$x, df$y) Manually created function named as func: func mapply(func, df1$x1, df1$x2)Output[1] 24 35 18 5 56 25 4 48 16 28 30 7 24 30 30 25 12 ...
Read MoreHow to create a frequency column for categorical variable in an R data frame?
To create a frequency column for categorical variable in an R data frame, we can use the transform function by defining the length of categorical variable using ave function. The output will have the duplicated frequencies as one value in the categorical column is likely to be repeated. Check out the below examples to understand how it can be done.ExampleConsider the below data frame −Country
Read More