将行名赋予R中的表 [英] Give row names to table in R
本文介绍了将行名赋予R中的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个CSV文件,看起来像这样:
I have a CSV file which somewhat looks like this:
我需要对"NoOffaces"进行聚类,并计算有1个面,2个面等的数据集.
I need to cluster "NoOffaces" and count how many datasets has 1 face, 2 face and so on.
这是我在 R 中所做的事情:
Here is what I did in R:
data<-read.csv('test.csv')
a<-table(data$NoOffaces)
a #for printing a
这是输出:
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?
我是 R 的初学者,我们将为您提供一些帮助.
I am a beginner in R, some help will be appreciated.
谢谢:)
推荐答案
您可以基于表创建矩阵并为其分配行名.
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
这篇关于将行名赋予R中的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文