Give row names to table in R

2019-05-23 11:24发布

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:)

2条回答
放我归山
2楼-- · 2019-05-23 11:50

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
查看更多
Melony?
3楼-- · 2019-05-23 12:01

I suppose something like that would work :

t(data.frame(Faces=names(a),Counts=as.vector(a)))

Also you can keep a as it is, names(a) gives you the faces and a the corresponding counts...

查看更多
登录 后发表回答