Give row names to table in R

Jay Patel picture Jay Patel · Nov 6, 2014 · Viewed 19.1k times · Source

I have a CSV file which somewhat looks like this:

enter image description here

I need to cluster "NoOffaces" and count how many datasets has 1 face, 2 face and so on.

Here is what I did in R:

data<-read.csv('test.csv')
a<-table(data$NoOffaces)
a  #for printing a

And here is the output:

 0   1   2   3   4    5   6   7   8   9  10  14  15  19 
448 375 104  33  16   7   4   2   2   3   1   3   1   1 

But, I want to give name to the first two rows so that it looks somewhat like this

 Faces :0   1   2    3   4    5   6   7   8   9  10  14  15  19 
 Count :448 375 104  33  16   7   4   2   2   3   1   3   1   1 

I am not able to name the rows, also how to access the each value in the column?

I am a beginner in R, some help will be appreciated.

Thank you:)

Answer

Sven Hohenstein picture Sven Hohenstein · Nov 6, 2014

You can create a matrix based on the table and assign row names to it.

# an example vector
x <- c(1:5, 1:3, 4:6)

a <- table(x)

mat <- rbind(as.numeric(names(a)), a)
rownames(mat) <- c("Faces", "Count")
mat
#       1 2 3 4 5 6
# Faces 1 2 3 4 5 6
# Count 2 2 2 2 2 1