Home>

Now run the table function on the next data frame to get the crosstab.

sample.df<-data.frame (X = c ("A", "A", "A", "A", "B", "B", "B"),
                        Y = c ("a", "a", "a", "a", "a", "a", "b"))
(sample.table<-table (sample.df))
 Y
X a b
  A 4 0
  B 2 1

From this table, I would like to get the following label/occurrence number pair.

X Y Number of appearances
A a Four
A b 0
B a 2
B b 1

You can change the order of the lines.

Hopefully this table will be json.

[{X: A, Y: a, value: 4},
 {...},
 ...
]

I would like to ask you the following three points.

  1. sample.tableHow to get "label and appearance number pair" with R from
  2. How to get the above json in R from "label and appearance number pair"
  3. If there is a way to execute the above (1) and (2) all at once with R, that method
  • Answer # 1

    How to get "label and number of occurrences pair" with R from sample.table

    >as.data.frame (table (sample.df))
      X Y Freq
    1 A a 4
    2 B a 2
    3 A b 0
    4 B b 1

    How to get the above json in R from "label and appearance number pair"

    When I think "Is there a ready-made package that can do this easily" in R, it is standard to catch CRAN.