Renaming rows and columns in R

时间:2021-07-18 04:38:33

I'm running the following script:

我正在运行以下脚本:

cause = c(1, 1, 1, 1, 1, 2, 1, 2, 2, 2, 2); 
time =  c(1, 1, 2, 3, 3, 2, 2, 1, 1, 2, 2); 
table(cause, time)

And I get the following:

我得到以下内容:

    time
cause 1 2 3
    1 2 2 2
    2 2 3 0

What I want is this:

我想要的是这个:

      time
cause     1 2 3
Maltreat  2 2 2
Non-Maltr 2 3 0

So, my question is: how do you rename the rows of a table in R?

所以,我的问题是:如何重命名R中表的行?

In the same vein, how would you rename the columns of that table?

同样,你如何重命名该表的列?

3 个解决方案

#1


4  

One way to do it is to use factors or lists of strings instead of indexes. So:

一种方法是使用字符串的因子或列表而不是索引。所以:

cause1 <- c("Maltreat", "Non-malt")[cause]

> print(cause1)
 [1] "Maltreat" "Maltreat" "Maltreat" "Maltreat" "Maltreat" "Non-malt"
 [7] "Maltreat" "Non-malt" "Non-malt" "Non-malt" "Non-malt"

> table(cause1, time)
          time
cause1     1 2 3
  Maltreat 2 2 2
  Non-malt 2 3 0

And, in case you're worried about memory or speed, R is pretty good at representing this sort of thing efficiently internally, with only a single instance of the whole string stored, and the rest done with indexes.

而且,如果你担心内存或速度,R很擅长在内部有效地表示这种事情,只存储整个字符串的一个实例,其余的用索引完成。

Incidentally, you'll be happier in the long run with data frames:

顺便说一句,从长远来看,你会更乐意使用数据框:

> df <- data.frame(cause=as.factor(c("Maltreat", "Non-malt")[cause]), time=time)
> summary(df)
      cause        time      
 Maltreat:6   Min.   :1.000  
 Non-malt:5   1st Qu.:1.000  
              Median :2.000  
              Mean   :1.818  
              3rd Qu.:2.000  
              Max.   :3.000  
> table(df)
          time
cause      1 2 3
  Maltreat 2 2 2
  Non-malt 2 3 0

#2


5  

There are two easy ways to do this:

有两种简单的方法可以做到这一点:

z <- table(cause, time)

Use the colnames/rownames functions:

使用colnames / rownames函数:

> colnames(z)
[1] "1" "2" "3"
> rownames(z)
[1] "1" "2"

Or use dimnames:

或者使用dimnames:

> dimnames(z)
$cause
[1] "1" "2"
$time
[1] "1" "2" "3"
> dimnames(z)$cause
[1] "1" "2"

In any case, choose your names as a vector and assign them:

无论如何,选择你的名字作为矢量并分配它们:

> dimnames(z)$cause <- c("Maltreat","Non-malt")
> z
          time
cause      1 2 3
  Maltreat 2 2 2
  Non-malt 2 3 0

#3


1  

Don't forget plyr's wonderful "revalue" and "rename" command!

不要忘记plyr精彩的“重估”和“重命名”命令!

#1


4  

One way to do it is to use factors or lists of strings instead of indexes. So:

一种方法是使用字符串的因子或列表而不是索引。所以:

cause1 <- c("Maltreat", "Non-malt")[cause]

> print(cause1)
 [1] "Maltreat" "Maltreat" "Maltreat" "Maltreat" "Maltreat" "Non-malt"
 [7] "Maltreat" "Non-malt" "Non-malt" "Non-malt" "Non-malt"

> table(cause1, time)
          time
cause1     1 2 3
  Maltreat 2 2 2
  Non-malt 2 3 0

And, in case you're worried about memory or speed, R is pretty good at representing this sort of thing efficiently internally, with only a single instance of the whole string stored, and the rest done with indexes.

而且,如果你担心内存或速度,R很擅长在内部有效地表示这种事情,只存储整个字符串的一个实例,其余的用索引完成。

Incidentally, you'll be happier in the long run with data frames:

顺便说一句,从长远来看,你会更乐意使用数据框:

> df <- data.frame(cause=as.factor(c("Maltreat", "Non-malt")[cause]), time=time)
> summary(df)
      cause        time      
 Maltreat:6   Min.   :1.000  
 Non-malt:5   1st Qu.:1.000  
              Median :2.000  
              Mean   :1.818  
              3rd Qu.:2.000  
              Max.   :3.000  
> table(df)
          time
cause      1 2 3
  Maltreat 2 2 2
  Non-malt 2 3 0

#2


5  

There are two easy ways to do this:

有两种简单的方法可以做到这一点:

z <- table(cause, time)

Use the colnames/rownames functions:

使用colnames / rownames函数:

> colnames(z)
[1] "1" "2" "3"
> rownames(z)
[1] "1" "2"

Or use dimnames:

或者使用dimnames:

> dimnames(z)
$cause
[1] "1" "2"
$time
[1] "1" "2" "3"
> dimnames(z)$cause
[1] "1" "2"

In any case, choose your names as a vector and assign them:

无论如何,选择你的名字作为矢量并分配它们:

> dimnames(z)$cause <- c("Maltreat","Non-malt")
> z
          time
cause      1 2 3
  Maltreat 2 2 2
  Non-malt 2 3 0

#3


1  

Don't forget plyr's wonderful "revalue" and "rename" command!

不要忘记plyr精彩的“重估”和“重命名”命令!