1

I have to create a table using information stored in a factor. Below is my factor information.

signature_group_factor

 S1  S2  S3  S4  S5 
 24  80 111  15 210 

The table I must create is

          signature           quantity
1            S1                  24
2            S2                  80
3            S3                  111
4            S4                  15
5            S5                  210
PKumar
  • 10,971
  • 6
  • 37
  • 52
DMC
  • 33
  • 3
  • 1
    How is `signature_group_factor` stored? It would be easier to know exactly what you're working with if you posted the output of calling `dput` on it – camille Oct 11 '19 at 17:47
  • I have a data frame of S1,S2,S,S4 and S5. I then used the factor function on this data frame and stored it in a varible called signature_group_factor. I then used the table function table(signature_group_factor). What I now need to do is create the above new table without using a librar. – DMC Oct 14 '19 at 11:31
  • I don't get what you mean by using the factor function on a data frame. That doesn't seem correct, which is why it's very helpful if you can make this a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – camille Oct 14 '19 at 16:08

1 Answers1

1

Here is one option with enframe if the initial input is a named vector

library(tibble)
library(dplyr)
enframe(signature_group_factor, name = "signature", value = "quantity")
# A tibble: 5 x 2
#  signature quantity
#  <chr>        <dbl>
#1 S1              24
#2 S2              80
#3 S3             111
#4 S4              15
#5 S5             210

Or using stack from base R

out <- setNames(stack(signature_group_factor)[2:1], c("signature", "quantity"))
out
#  signature quantity
#1        S1       24
#2        S2       80
#3        S3      111
#4        S4       15
#5        S5      210

data

signature_group_factor <- setNames(c(24, 80, 111, 15, 210), paste0("S", 1:5))
akrun
  • 874,273
  • 37
  • 540
  • 662