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
-
Economics & Finance
Selected Reading
How to convert MANOVA data frame for two-dependent variables into a count table in R?
MANOVA refers to multivariate analysis of variance, in this method we have more than one dependent variable and multiple independent variables. We want to compare each level of the independent variable combination for each of the dependent variables. To convert MANOVA data frame for two-dependent variables into a count table, we can use cast function of reshape package but we need to melt the data frame first so that the casting can be done appropriately.
Example
ID<<sample(c("Y1","Y2","Y3","Y4"),20,replace=TRUE)
Grade<<sample(LETTERS[1:3],20,replace=TRUE)
Sal<<sample(20000:50000,20)
Count<<sample(200:210,20,replace=TRUE)
df2<<data.frame(ID,Grade,Sal,Count)
df2
Output
ID Grade Sal Count 1 Y3 B 28528 204 2 Y3 C 40854 207 3 Y3 A 31199 207 4 Y4 B 25338 207 5 Y3 B 30180 209 6 Y2 B 29921 209 7 Y4 C 46134 210 8 Y4 B 46829 205 9 Y3 B 42607 205 10 Y1 A 38174 202 11 Y2 A 41451 207 12 Y1 C 23912 200 13 Y4 B 44047 209 14 Y2 B 32236 200 15 Y2 A 24851 203 16 Y2 B 36341 207 17 Y3 B 37003 208 18 Y2 C 37285 207 19 Y3 B 45113 207 20 Y3 A 40034 203 df2_melt<−melt(df2)
Using ID, Grade as id variables
Finding counts based on ID and Grade −
cast(df2_melt,ID~Grade+variable)
Aggregation requires fun.aggregate: length used as default
ID A_Sal A_Count B_Sal B_Count C_Sal C_Count 1 Y1 1 1 0 0 1 1 2 Y2 2 2 3 3 1 1 3 Y3 2 2 5 5 1 1 4 Y4 0 0 3 3 1 1
Advertisements
