1

I'm running frequency table of frequencies, I want to convert the table to two lists of numbers.

numbers <- c(1,2,3,4,1,2,3,1,2,3,1,2,3,4,2,3,5,1,2,3,4)

freq_of_freq <- table(table(numbers))

> freq_of_freq

1 3 5 6 
1 1 1 2 

From the table freq_of_freq, I'd like to get create two list, x and y, one containing the numbers 1,3,5,6 and the other with the frequency values 1,1,1,2

I tried this x <- freq_of_freq[ 1 , ] and y <- freq_of_freq[ 2 , ], but this doesn't work.

Any help greatly appreciated. Thanks

H.Cheung
  • 855
  • 5
  • 12

1 Answers1

1

One approach is to use stack() to create a list.

numbers <- c(1,2,3,4,1,2,3,1,2,3,1,2,3,4,2,3,5,1,2,3,4)
freq_of_freq <- table(table(numbers))

stack(freq_of_freq)
#>   values ind
#> 1      1   1
#> 2      1   3
#> 3      1   5
#> 4      2   6

To exactly match your expected output, you could do:

x = as.integer(names(freq_of_freq))
y = unname(freq_of_freq)

Note, the OP attempt of freq_of_freq[1, ] does not work because table returns a named integer vector for this example dataset. That is, we can't subset using matrix or data.frame notation because we only have one dimension.

Cole
  • 11,130
  • 1
  • 9
  • 24