Give row names to table in R

房东的猫 提交于 2019-12-10 11:24:39

问题


I have a CSV file which somewhat looks like this:

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


回答1:


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



回答2:


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...



来源:https://stackoverflow.com/questions/26774426/give-row-names-to-table-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!