matryoshka
matryoshka

Reputation: 135

How do I create a table in R with information that's not included in the data from my csv file?

I'm trying to create a table that documents the number of times a type of book is mentioned in my data set. This is what I've tried:

my_table <- table(library$Title)

Now I've been looking online and found a couple sites that are helpful, such as this one. They have this example:

is.na(clinical.trial$age) <- sample(1:100, 20)
summary(clinical.trial)

#    patient            age            treatment       center
# Min.   :  1.00   Min.   :46.61   Treatment:50   Center A:22
# 1st Qu.: 25.75   1st Qu.:56.19   Control  :50   Center B:10
# Median : 50.50   Median :60.59                  Center C:28
# Mean   : 50.50   Mean   :60.57                  Center D:23
# 3rd Qu.: 75.25   3rd Qu.:64.84                  Center E:17
# Max.   :100.00   Max.   :77.83
#                  NA's   :20.00    

table(clinical.trial$center)

# Center A Center B Center C Center D Center E
# 22       10       28       23       17  

That's along the lines of what I want, however I simply have a column with book titles in my csv document that the program is calling.

For example: Cosmos, The Hobbit, The Story of Light, and The Great Gatsby are mentioned and the printed result simply says "26, 30, 22, 35". Not only are they not labeled, I also don't know which value goes with what.

If this information helps, I'm writing the code in an R Markdown file, knitting it and results pop up in a separate html file.

What do I need to do in order to have the book title match with the number of times they are mentioned?

Upvotes: 0

Views: 43

Answers (1)

IRTFM
IRTFM

Reputation: 263451

I've always resorted to the hack of using as.matrix around a table when I want the columns to be "downgoing" instead of "horizontal":

> tbl <- table( sample(letters[1:10], 30, repl=TRUE))
> tbl

a b c d e f g h i j 
4 3 2 3 2 2 2 3 1 8 
> as.matrix(tbl)
  [,1]
a    4
b    3
c    2
d    3
e    2
f    2
g    2
h    3
i    1
j    8

Upvotes: 1

Related Questions