0

I have a table:

table(sex)

 male  female 
58     48 

I would like to put it like that:

male   58
female 48

Is that possible?

E.bl
  • 27
  • 4

1 Answers1

1

We can wrap with data.frame

as.data.frame(table(sex))
#     sex Freq
#1 female   42
#2   male   58

data

set.seed(24)
sex <- sample(c("male", "female"), 100, replace=TRUE)
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thank you for your answer. Is that possible to have exactly that i requested without #? – E.bl Apr 17 '16 at 08:45
  • 3
    @E.bl Have you even tried the solution? The `#` are used in SO posts to display the output, thereby making clear that these lines are not part of the code. Here's a [demo](https://ideone.com/5qWYmq) of akrun's answer. – RHertel Apr 17 '16 at 08:50
  • Yes, i tried it. I need that for write an function and i don't work – E.bl Apr 17 '16 at 08:56
  • @E.bl Maybe [this variant](https://ideone.com/U2c4Mm) helps: `x <- \`colnames<-\`(as.data.frame(table(sex)), NULL); rownames(x) <- x[,1]; x <- x[,2,drop=FALSE]`. – RHertel Apr 17 '16 at 09:03
  • @E.bl If you want to preserve the `table` class (although it is generally more difficult to handle than a `data.frame`) you can try `t(t(table(sex))`. – RHertel Apr 17 '16 at 09:14
  • sounded like E.bi wanted to get rid of the # table decorators. – Chris Apr 17 '16 at 09:21