0

If I have a data frame (or a tibble) df as the following:

x <- c(2,2,2,1,1,2,3)
y <- c(5,5,4,5,4,4,5)
df <- data.frame(x,y)

Then, if I cross data with the instruction table(df$x,df$y) I get a matrix form:

     4    5
1    1    1
2    2    2
3    0    1

But if I do a View of the same instruction what I see has a different structure, such as you can see next:

 1    4    1
 2    4    2
 3    4    0
 1    5    1
 2    5    2
 3    5    1

Do you know if there is some instruction to make something similar to View (taking into account its description: Invoke a spreadsheet-style data viewer on a matrix-like R object) preserving the first structure for the intersection table?

Thanks in advance.

iago
  • 2,990
  • 4
  • 21
  • 27
  • I think the issue is that a table is not close enough to a matrix-like R object. If I look at str(table(df$x,df$y)), I see that it is a list – Kerry Jackson May 09 '18 at 14:55
  • In fact it has dimensions. I am asking for a way to get the same result that View, but for an object like this. – iago May 09 '18 at 15:04

1 Answers1

1

The result of table(...) is an object of class "table". There's an as.data.frame.table method that rearranges the data; presumably View() is using that. This makes sense, because tables can have other than 2 indices; e.g. table(df$x, df$y, df$x) will have 3 indices, so it needs to be reshaped to become a dataframe.

If you know your table has two indices, you could use unclass()before passing it to View(), i.e. View(unclass(table(df$x, df$y))).

user2554330
  • 37,248
  • 4
  • 43
  • 90