Calculating all possible combinations within a range in R - r

I'm trying to generate all combinations of four variables, where each variable is an integral between 0 and 10. Is there an easy way to do this in R?
X | Y | Z | W
-------------
0 | 0 | 0 | 0
1 | 0 | 0 | 0
1 | 1 | 0 | 0
1 | 1 | 1 | 0
. . . .
. . . .
. . . .
10|10 |10 |10

If W, X, Y and Z exist
expand.grid(W = W, X = X, Y = Y, Z = Z)
W X Y Z
1 0 0 0 0
2 1 0 0 0
3 2 0 0 0
4 3 0 0 0
5 4 0 0 0
6 5 0 0 0
7 6 0 0 0
8 7 0 0 0
9 8 0 0 0
10 9 0 0 0
11 10 0 0 0
12 0 1 0 0
13 1 1 0 0
14 2 1 0 0
15 3 1 0 0
...

All combinations can be done with table. Converting to a data frame yields to what you're looking for.
> as.data.frame(table(W=0:10, X=0:10, Y=0:10, Z=0:10))[, c('W','X','Y','Z')]
W X Y Z
1 0 0 0 0
2 1 0 0 0
3 2 0 0 0
4 3 0 0 0
5 4 0 0 0
6 5 0 0 0
7 6 0 0 0
8 7 0 0 0
9 8 0 0 0
10 9 0 0 0
11 10 0 0 0
12 0 1 0 0
13 1 1 0 0
...

Related

R equivalent of Stata `tabulate , generate( )` command

I want to mimic the behavior of Stata's tabulate , generate() command in R. It is illustrated below; the command's functionality is twofold. First, in my example, it produces a one-way table of frequency counts. Second, it generated dummy variables for each of the values contained on the variable (var1) using the prefix (stubname) declared in option ,generate() to name the generated dummy variables (d_1 - d_7). My question is regarding the second functionality. R-base solutions are preferred, but packaged dependent are also welcome.
[Edit]: My final goal is to generate a data.frame() that emulates the last data set printed on the screen.
clear all
input var1
0
1
2
2
2
2
42
42
777
888
999999
end
tabulate var1 ,gen(d_)
/* var1 | Freq. Percent Cum.
------------+-----------------------------------
0 | 1 9.09 9.09
1 | 1 9.09 18.18
2 | 4 36.36 54.55
42 | 2 18.18 72.73
777 | 1 9.09 81.82
888 | 1 9.09 90.91
999999 | 1 9.09 100.00
------------+-----------------------------------
Total | 11 100.00 */
list, sep(11)
/* +--------------------------------------------------+
| var1 d_1 d_2 d_3 d_4 d_5 d_6 d_7 |
|--------------------------------------------------|
1. | 0 1 0 0 0 0 0 0 |
2. | 1 0 1 0 0 0 0 0 |
3. | 2 0 0 1 0 0 0 0 |
4. | 2 0 0 1 0 0 0 0 |
5. | 2 0 0 1 0 0 0 0 |
6. | 2 0 0 1 0 0 0 0 |
7. | 42 0 0 0 1 0 0 0 |
8. | 42 0 0 0 1 0 0 0 |
9. | 777 0 0 0 0 1 0 0 |
10. | 888 0 0 0 0 0 1 0 |
11. | 999999 0 0 0 0 0 0 1 |
+--------------------------------------------------+ */
set.seed(123)
df = data.frame(var1 = factor(sample(10, 20, TRUE)))
df = data.frame(df, model.matrix(~0+var1, df)) # 0 here is to suppress the intercept. The smallest value will be the base group--and hence will be dropped.
names(df)[-1] = paste0('d_', 1:(ncol(df)-1))
df
var1 d_1 d_2 d_3 d_4 d_5 d_6 d_7 d_8 d_9
1 3 0 1 0 0 0 0 0 0 0
2 3 0 1 0 0 0 0 0 0 0
3 10 0 0 0 0 0 0 0 0 1
4 2 1 0 0 0 0 0 0 0 0
5 6 0 0 0 0 1 0 0 0 0
6 5 0 0 0 1 0 0 0 0 0
7 4 0 0 1 0 0 0 0 0 0
8 6 0 0 0 0 1 0 0 0 0
9 9 0 0 0 0 0 0 0 1 0
10 10 0 0 0 0 0 0 0 0 1
11 5 0 0 0 1 0 0 0 0 0
12 3 0 1 0 0 0 0 0 0 0
13 9 0 0 0 0 0 0 0 1 0
14 9 0 0 0 0 0 0 0 1 0
15 9 0 0 0 0 0 0 0 1 0
16 3 0 1 0 0 0 0 0 0 0
17 8 0 0 0 0 0 0 1 0 0
18 10 0 0 0 0 0 0 0 0 1
19 7 0 0 0 0 0 1 0 0 0
20 10 0 0 0 0 0 0 0 0 1
I guess you are assuming each value in var_1 is unique so that you get dummy variables rather than counts in the d_ fields.
You could try something like this:
var1 <- 1:5
dummy_matrix <- vapply(var1, function(x) as.numeric(var1 == x), rep(1, 5)) # create a matrix of dummy vars
colnames(dummy_matrix) <- paste0("d_", var1) # name the columns
cbind(var1, dummy_matrix) # bind to var1
Output:
var1 d_1 d_2 d_3 d_4 d_5
1 1 1 0 0 0 0
2 2 0 1 0 0 0
3 3 0 0 1 0 0
4 4 0 0 0 1 0
5 5 0 0 0 0 1

Updating running list if event happens

sample Data:
clear
* Input data
input student CITATION EXPELLED hadCITATION hadEXPELLED
1 0 0 0 0
1 0 0 0 0
1 0 0 0 0
1 0 0 0 0
1 0 0 0 0
2 0 0 0 0
2 0 0 0 0
2 1 0 1 0
2 1 0 1 0
2 0 0 1 0
3 1 0 1 0
3 0 1 1 1
3 1 1 1 1
3 1 0 1 1
3 1 0 1 1
4 . . . .
4 . 0 . 0
4 0 0 0 0
4 0 1 0 1
4 1 0 1 0
I want to create these hadCITATION and hadEXPELLED variable columns that update based on the responses of CITATION and EXPELLED.
This may help. I can't see that this makes sense without a time or sequence variable. My guess is that once you've had a CITATION or EXPULSION, then that's your history. The rules may be more complicated, but I can't see that you're explaining them. I can't see the rationale for your example for student 4.
clear
input student CITATION EXPELLED hadCITATION hadEXPELLED
1 0 0 0 0
1 0 0 0 0
1 0 0 0 0
1 0 0 0 0
1 0 0 0 0
2 0 0 0 0
2 0 0 0 0
2 1 0 1 0
2 1 0 1 0
2 0 0 1 0
3 1 0 1 0
3 0 1 1 1
3 1 1 1 1
3 1 0 1 1
3 1 0 1 1
4 . . . .
4 . 0 . 0
4 0 0 0 0
4 0 1 0 1
4 1 0 1 0
end
gen long time = _n
bysort student (time) : gen want1 = sum(CITATION) > 0
by student: gen want2 = sum(EXPELLED) > 0
list student CIT EXP hadCIT hadEXP want?, sepby(student)
+---------------------------------------------------------------------+
| student CITATION EXPELLED hadCIT~N hadEXP~D want1 want2 |
|---------------------------------------------------------------------|
1. | 1 0 0 0 0 0 0 |
2. | 1 0 0 0 0 0 0 |
3. | 1 0 0 0 0 0 0 |
4. | 1 0 0 0 0 0 0 |
5. | 1 0 0 0 0 0 0 |
|---------------------------------------------------------------------|
6. | 2 0 0 0 0 0 0 |
7. | 2 0 0 0 0 0 0 |
8. | 2 1 0 1 0 1 0 |
9. | 2 1 0 1 0 1 0 |
10. | 2 0 0 1 0 1 0 |
|---------------------------------------------------------------------|
11. | 3 1 0 1 0 1 0 |
12. | 3 0 1 1 1 1 1 |
13. | 3 1 1 1 1 1 1 |
14. | 3 1 0 1 1 1 1 |
15. | 3 1 0 1 1 1 1 |
|---------------------------------------------------------------------|
16. | 4 . . . . 0 0 |
17. | 4 . 0 . 0 0 0 |
18. | 4 0 0 0 0 0 0 |
19. | 4 0 1 0 1 0 1 |
20. | 4 1 0 1 0 1 1 |
+---------------------------------------------------------------------+

R: Print omitted 0's in table() - contingency tables [duplicate]

I am using the following R code to produce a confusion matrix comparing the true labels of some data to the output of a neural network.
t <- table(as.factor(test.labels), as.factor(nnetpredict))
However, sometimes the neural network doesn't predict any of a certain class, so the table isn't square (as, for example, there are 5 levels in the test.labels factor, but only 3 levels in the nnetpredict factor). I want to make the table square by adding in any factor levels necessary, and setting their counts to zero.
How should I go about doing this?
Example:
> table(as.factor(a), as.factor(b))
1 2 3 4 5 6 7 8 9 10
1 1 0 0 0 0 0 0 1 0 0
2 0 1 0 0 0 0 0 0 1 0
3 0 0 1 0 0 0 0 0 0 1
4 0 0 0 1 0 0 0 0 0 0
5 0 0 0 0 1 0 0 0 0 0
6 0 0 0 0 0 1 0 0 0 0
7 0 0 0 0 0 0 1 0 0 0
You can see in the table above that there are 7 rows, but 10 columns, because the a factor only has 7 levels, whereas the b factor has 10 levels. What I want to do is to pad the table with zeros so that the row labels and the column labels are the same, and the matrix is square. From the example above, this would produce:
1 2 3 4 5 6 7 8 9 10
1 1 0 0 0 0 0 0 1 0 0
2 0 1 0 0 0 0 0 0 1 0
3 0 0 1 0 0 0 0 0 0 1
4 0 0 0 1 0 0 0 0 0 0
5 0 0 0 0 1 0 0 0 0 0
6 0 0 0 0 0 1 0 0 0 0
7 0 0 0 0 0 0 1 0 0 0
8 0 0 0 0 0 0 0 0 0 0
9 0 0 0 0 0 0 0 0 0 0
10 0 0 0 0 0 0 0 0 0 0
The reason I need to do this is two-fold:
For display to users/in reports
So that I can use a function to calculate the Kappa statistic, which requires a table formatted like this (square, same row and col labels)
EDIT - round II to address the additional details in the question. I deleted my first answer since it wasn't relevant anymore.
This has produced the desired output for the test cases I've given it, but I definitely advise testing thoroughly with your real data. The approach here is to find the full list of levels for both inputs into the table and set that full list as the levels before generating the table.
squareTable <- function(x,y) {
x <- factor(x)
y <- factor(y)
commonLevels <- sort(unique(c(levels(x), levels(y))))
x <- factor(x, levels = commonLevels)
y <- factor(y, levels = commonLevels)
table(x,y)
}
Two test cases:
> #Test case 1
> set.seed(1)
> x <- factor(sample(0:9, 100, TRUE))
> y <- factor(sample(3:7, 100, TRUE))
>
> table(x,y)
y
x 3 4 5 6 7
0 2 1 3 1 0
1 1 0 2 3 0
2 1 0 3 4 3
3 0 3 6 3 2
4 4 4 3 2 1
5 2 2 0 1 0
6 1 2 3 2 3
7 3 3 3 4 2
8 0 4 1 2 4
9 2 1 0 0 3
> squareTable(x,y)
y
x 0 1 2 3 4 5 6 7 8 9
0 0 0 0 2 1 3 1 0 0 0
1 0 0 0 1 0 2 3 0 0 0
2 0 0 0 1 0 3 4 3 0 0
3 0 0 0 0 3 6 3 2 0 0
4 0 0 0 4 4 3 2 1 0 0
5 0 0 0 2 2 0 1 0 0 0
6 0 0 0 1 2 3 2 3 0 0
7 0 0 0 3 3 3 4 2 0 0
8 0 0 0 0 4 1 2 4 0 0
9 0 0 0 2 1 0 0 3 0 0
> squareTable(y,x)
y
x 0 1 2 3 4 5 6 7 8 9
0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0
2 0 0 0 0 0 0 0 0 0 0
3 2 1 1 0 4 2 1 3 0 2
4 1 0 0 3 4 2 2 3 4 1
5 3 2 3 6 3 0 3 3 1 0
6 1 3 4 3 2 1 2 4 2 0
7 0 0 3 2 1 0 3 2 4 3
8 0 0 0 0 0 0 0 0 0 0
9 0 0 0 0 0 0 0 0 0 0
>
> #Test case 2
> set.seed(1)
> xx <- factor(sample(0:2, 100, TRUE))
> yy <- factor(sample(3:5, 100, TRUE))
>
> table(xx,yy)
yy
xx 3 4 5
0 4 14 9
1 14 15 9
2 11 11 13
> squareTable(xx,yy)
y
x 0 1 2 3 4 5
0 0 0 0 4 14 9
1 0 0 0 14 15 9
2 0 0 0 11 11 13
3 0 0 0 0 0 0
4 0 0 0 0 0 0
5 0 0 0 0 0 0
> squareTable(yy,xx)
y
x 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 0 0 0 0
2 0 0 0 0 0 0
3 4 14 11 0 0 0
4 14 15 11 0 0 0
5 9 9 13 0 0 0

How to sum leading diagonal of table in R

I have a table created using the table() command in R:
y
x 0 1 2 3 4 5 6 7 8 9
0 23 0 0 0 0 1 0 0 0 0
1 0 23 1 0 1 0 1 2 0 2
2 1 1 28 0 0 0 1 0 2 2
3 0 1 0 24 0 1 0 0 0 1
4 1 1 0 0 34 0 3 0 0 0
5 0 0 0 0 0 33 0 0 0 0
6 0 0 0 0 0 2 32 0 0 0
7 0 1 0 1 0 0 0 36 0 1
8 1 1 1 1 0 0 0 1 20 1
9 1 3 0 1 0 1 0 1 0 24
This table shows the results of a classification, and I want to sum the leading diagonal of it (the diagonal with the large numbers - like 23, 23, 28 etc). Is there a sensible/easy way to do this in R?
How about sum(diag(tbl)), where tbl is your table?

Force `table` to include all factors from both arrays in R

I am using the following R code to produce a confusion matrix comparing the true labels of some data to the output of a neural network.
t <- table(as.factor(test.labels), as.factor(nnetpredict))
However, sometimes the neural network doesn't predict any of a certain class, so the table isn't square (as, for example, there are 5 levels in the test.labels factor, but only 3 levels in the nnetpredict factor). I want to make the table square by adding in any factor levels necessary, and setting their counts to zero.
How should I go about doing this?
Example:
> table(as.factor(a), as.factor(b))
1 2 3 4 5 6 7 8 9 10
1 1 0 0 0 0 0 0 1 0 0
2 0 1 0 0 0 0 0 0 1 0
3 0 0 1 0 0 0 0 0 0 1
4 0 0 0 1 0 0 0 0 0 0
5 0 0 0 0 1 0 0 0 0 0
6 0 0 0 0 0 1 0 0 0 0
7 0 0 0 0 0 0 1 0 0 0
You can see in the table above that there are 7 rows, but 10 columns, because the a factor only has 7 levels, whereas the b factor has 10 levels. What I want to do is to pad the table with zeros so that the row labels and the column labels are the same, and the matrix is square. From the example above, this would produce:
1 2 3 4 5 6 7 8 9 10
1 1 0 0 0 0 0 0 1 0 0
2 0 1 0 0 0 0 0 0 1 0
3 0 0 1 0 0 0 0 0 0 1
4 0 0 0 1 0 0 0 0 0 0
5 0 0 0 0 1 0 0 0 0 0
6 0 0 0 0 0 1 0 0 0 0
7 0 0 0 0 0 0 1 0 0 0
8 0 0 0 0 0 0 0 0 0 0
9 0 0 0 0 0 0 0 0 0 0
10 0 0 0 0 0 0 0 0 0 0
The reason I need to do this is two-fold:
For display to users/in reports
So that I can use a function to calculate the Kappa statistic, which requires a table formatted like this (square, same row and col labels)
EDIT - round II to address the additional details in the question. I deleted my first answer since it wasn't relevant anymore.
This has produced the desired output for the test cases I've given it, but I definitely advise testing thoroughly with your real data. The approach here is to find the full list of levels for both inputs into the table and set that full list as the levels before generating the table.
squareTable <- function(x,y) {
x <- factor(x)
y <- factor(y)
commonLevels <- sort(unique(c(levels(x), levels(y))))
x <- factor(x, levels = commonLevels)
y <- factor(y, levels = commonLevels)
table(x,y)
}
Two test cases:
> #Test case 1
> set.seed(1)
> x <- factor(sample(0:9, 100, TRUE))
> y <- factor(sample(3:7, 100, TRUE))
>
> table(x,y)
y
x 3 4 5 6 7
0 2 1 3 1 0
1 1 0 2 3 0
2 1 0 3 4 3
3 0 3 6 3 2
4 4 4 3 2 1
5 2 2 0 1 0
6 1 2 3 2 3
7 3 3 3 4 2
8 0 4 1 2 4
9 2 1 0 0 3
> squareTable(x,y)
y
x 0 1 2 3 4 5 6 7 8 9
0 0 0 0 2 1 3 1 0 0 0
1 0 0 0 1 0 2 3 0 0 0
2 0 0 0 1 0 3 4 3 0 0
3 0 0 0 0 3 6 3 2 0 0
4 0 0 0 4 4 3 2 1 0 0
5 0 0 0 2 2 0 1 0 0 0
6 0 0 0 1 2 3 2 3 0 0
7 0 0 0 3 3 3 4 2 0 0
8 0 0 0 0 4 1 2 4 0 0
9 0 0 0 2 1 0 0 3 0 0
> squareTable(y,x)
y
x 0 1 2 3 4 5 6 7 8 9
0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0
2 0 0 0 0 0 0 0 0 0 0
3 2 1 1 0 4 2 1 3 0 2
4 1 0 0 3 4 2 2 3 4 1
5 3 2 3 6 3 0 3 3 1 0
6 1 3 4 3 2 1 2 4 2 0
7 0 0 3 2 1 0 3 2 4 3
8 0 0 0 0 0 0 0 0 0 0
9 0 0 0 0 0 0 0 0 0 0
>
> #Test case 2
> set.seed(1)
> xx <- factor(sample(0:2, 100, TRUE))
> yy <- factor(sample(3:5, 100, TRUE))
>
> table(xx,yy)
yy
xx 3 4 5
0 4 14 9
1 14 15 9
2 11 11 13
> squareTable(xx,yy)
y
x 0 1 2 3 4 5
0 0 0 0 4 14 9
1 0 0 0 14 15 9
2 0 0 0 11 11 13
3 0 0 0 0 0 0
4 0 0 0 0 0 0
5 0 0 0 0 0 0
> squareTable(yy,xx)
y
x 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 0 0 0 0
2 0 0 0 0 0 0
3 4 14 11 0 0 0
4 14 15 11 0 0 0
5 9 9 13 0 0 0

Resources