在R中的行名称和列名。

时间:2021-06-09 14:58:08

Do the following function pairs generate exactly the same results?

下面的函数对生成完全相同的结果吗?

Pair 1) names() & colnames()

对1)名称()和colnames()

Pair 2) rownames() & row.names()

对2)rownames()和row.names()

4 个解决方案

#1


57  

As Oscar Wilde said

就像奥斯卡王尔德说

Consistency is the last refuge of the unimaginative.

一致性是缺乏想象力的人最后的避难所。

R is more of an evolved rather than designed language, so these things happen. names() and colnames() work on a data.frame but names() does not work on a matrix:

R更多的是一个进化而不是设计的语言,所以这些事情发生了。name()和colnames()在数据a上工作,但名称()在矩阵上不起作用:

R> DF <- data.frame(foo=1:3, bar=LETTERS[1:3])
R> names(DF)
[1] "foo" "bar"
R> colnames(DF)
[1] "foo" "bar"
R> M <- matrix(1:9, ncol=3, dimnames=list(1:3, c("alpha","beta","gamma")))
R> names(M)
NULL
R> colnames(M)
[1] "alpha" "beta"  "gamma"
R> 

#2


8  

Just to expand a little on Dirk's example:

我们来看看德克的例子:

It helps to think of a data frame as a list with equal length vectors. That's probably why names works with a data frame but not a matrix.

它有助于将数据框看作具有相同长度向量的列表。这可能就是为什么名字使用数据框而不是矩阵的原因。

The other useful function is dimnames which returns the names for every dimension. You will notice that the rownames function actually just returns the first element from dimnames.

另一个有用的函数是dimnames,它返回每个维度的名称。您会注意到,rownames函数实际上只是从dimnames中返回第一个元素。

Regarding rownames and row.names: I can't tell the difference, although rownames uses dimnames while row.names was written outside of R. They both also seem to work with higher dimensional arrays:

关于行名和行。名称:我不能区分它们,尽管rowname使用的是dimnames,而row.names是在r之外写的,它们似乎都在使用更高维度的数组:

>a <- array(1:5, 1:4)
> a[1,,,]
> rownames(a) <- "a"
> row.names(a)
[1] "a"
> a
, , 1, 1    
  [,1] [,2]
a    1    2

> dimnames(a)
[[1]]
[1] "a"

[[2]]
NULL

[[3]]
NULL

[[4]]
NULL

#3


5  

I think that using colnames and rownames makes the most sense; here's why.

我认为使用colnames和rownames是最有意义的;这是为什么。

Using names has several disadvantages. You have to remember that it means "column names", and it only works with data frame, so you'll need to call colnames whenever you use matrices. By calling colnames, you only have to remember one function. Finally, if you look at the code for colnames, you will see that it calls names in the case of a data frame anyway, so the output is identical.

使用名称有几个缺点。你必须记住它的意思是“列名”,它只适用于数据帧,所以当你使用矩阵时,你需要调用colnames。通过调用colnames,您只需要记住一个函数。最后,如果您查看colnames的代码,您将看到它在数据框架的情况下调用名称,因此输出是相同的。

rownames and row.names return the same values for data frame and matrices; the only difference that I have spotted is that where there aren't any names, rownames will print "NULL" (as does colnames), but row.names returns it invisibly. Since there isn't much to choose between the two functions, rownames wins on the grounds of aesthetics, since it pairs more prettily withcolnames. (Also, for the lazy programmer, you save a character of typing.)

行名和行。名称返回数据框架和矩阵的相同值;我发现的唯一不同之处是,在没有名称的地方,rowname将打印“NULL”(和colnames一样),但是row.name会不可见地返回它。由于在这两个函数之间没有太多的选择,所以rownames以美学为基础而胜出,因为它与colnames更相似。(同时,对于懒惰的程序员,您可以保存键入的字符。)

#4


2  

And another expansion:

和另一个扩展:

# create dummy matrix
set.seed(10)
m <- matrix(round(runif(25, 1, 5)), 5)
d <- as.data.frame(m)

If you want to assign new column names you can do following on data.frame:

如果您想要分配新的列名,您可以在data.frame:

# an identical effect can be achieved with colnames()   
names(d) <- LETTERS[1:5]
> d
  A B C D E
1 3 2 4 3 4
2 2 2 3 1 3
3 3 2 1 2 4
4 4 3 3 3 2
5 1 3 2 4 3

If you, however run previous command on matrix, you'll mess things up:

如果你在矩阵上运行之前的命令,你会把事情弄糟:

names(m) <- LETTERS[1:5]
> m
     [,1] [,2] [,3] [,4] [,5]
[1,]    3    2    4    3    4
[2,]    2    2    3    1    3
[3,]    3    2    1    2    4
[4,]    4    3    3    3    2
[5,]    1    3    2    4    3
attr(,"names")
 [1] "A" "B" "C" "D" "E" NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA 
[20] NA  NA  NA  NA  NA  NA 

Since matrix can be regarded as two-dimensional vector, you'll assign names only to first five values (you don't want to do that, do you?). In this case, you should stick with colnames().

因为矩阵可以被看作是二维向量,所以你只需要给前五个值指定名称(你不想这样做,对吧?)在这种情况下,您应该使用colnames()。

So there...

所以…

#1


57  

As Oscar Wilde said

就像奥斯卡王尔德说

Consistency is the last refuge of the unimaginative.

一致性是缺乏想象力的人最后的避难所。

R is more of an evolved rather than designed language, so these things happen. names() and colnames() work on a data.frame but names() does not work on a matrix:

R更多的是一个进化而不是设计的语言,所以这些事情发生了。name()和colnames()在数据a上工作,但名称()在矩阵上不起作用:

R> DF <- data.frame(foo=1:3, bar=LETTERS[1:3])
R> names(DF)
[1] "foo" "bar"
R> colnames(DF)
[1] "foo" "bar"
R> M <- matrix(1:9, ncol=3, dimnames=list(1:3, c("alpha","beta","gamma")))
R> names(M)
NULL
R> colnames(M)
[1] "alpha" "beta"  "gamma"
R> 

#2


8  

Just to expand a little on Dirk's example:

我们来看看德克的例子:

It helps to think of a data frame as a list with equal length vectors. That's probably why names works with a data frame but not a matrix.

它有助于将数据框看作具有相同长度向量的列表。这可能就是为什么名字使用数据框而不是矩阵的原因。

The other useful function is dimnames which returns the names for every dimension. You will notice that the rownames function actually just returns the first element from dimnames.

另一个有用的函数是dimnames,它返回每个维度的名称。您会注意到,rownames函数实际上只是从dimnames中返回第一个元素。

Regarding rownames and row.names: I can't tell the difference, although rownames uses dimnames while row.names was written outside of R. They both also seem to work with higher dimensional arrays:

关于行名和行。名称:我不能区分它们,尽管rowname使用的是dimnames,而row.names是在r之外写的,它们似乎都在使用更高维度的数组:

>a <- array(1:5, 1:4)
> a[1,,,]
> rownames(a) <- "a"
> row.names(a)
[1] "a"
> a
, , 1, 1    
  [,1] [,2]
a    1    2

> dimnames(a)
[[1]]
[1] "a"

[[2]]
NULL

[[3]]
NULL

[[4]]
NULL

#3


5  

I think that using colnames and rownames makes the most sense; here's why.

我认为使用colnames和rownames是最有意义的;这是为什么。

Using names has several disadvantages. You have to remember that it means "column names", and it only works with data frame, so you'll need to call colnames whenever you use matrices. By calling colnames, you only have to remember one function. Finally, if you look at the code for colnames, you will see that it calls names in the case of a data frame anyway, so the output is identical.

使用名称有几个缺点。你必须记住它的意思是“列名”,它只适用于数据帧,所以当你使用矩阵时,你需要调用colnames。通过调用colnames,您只需要记住一个函数。最后,如果您查看colnames的代码,您将看到它在数据框架的情况下调用名称,因此输出是相同的。

rownames and row.names return the same values for data frame and matrices; the only difference that I have spotted is that where there aren't any names, rownames will print "NULL" (as does colnames), but row.names returns it invisibly. Since there isn't much to choose between the two functions, rownames wins on the grounds of aesthetics, since it pairs more prettily withcolnames. (Also, for the lazy programmer, you save a character of typing.)

行名和行。名称返回数据框架和矩阵的相同值;我发现的唯一不同之处是,在没有名称的地方,rowname将打印“NULL”(和colnames一样),但是row.name会不可见地返回它。由于在这两个函数之间没有太多的选择,所以rownames以美学为基础而胜出,因为它与colnames更相似。(同时,对于懒惰的程序员,您可以保存键入的字符。)

#4


2  

And another expansion:

和另一个扩展:

# create dummy matrix
set.seed(10)
m <- matrix(round(runif(25, 1, 5)), 5)
d <- as.data.frame(m)

If you want to assign new column names you can do following on data.frame:

如果您想要分配新的列名,您可以在data.frame:

# an identical effect can be achieved with colnames()   
names(d) <- LETTERS[1:5]
> d
  A B C D E
1 3 2 4 3 4
2 2 2 3 1 3
3 3 2 1 2 4
4 4 3 3 3 2
5 1 3 2 4 3

If you, however run previous command on matrix, you'll mess things up:

如果你在矩阵上运行之前的命令,你会把事情弄糟:

names(m) <- LETTERS[1:5]
> m
     [,1] [,2] [,3] [,4] [,5]
[1,]    3    2    4    3    4
[2,]    2    2    3    1    3
[3,]    3    2    1    2    4
[4,]    4    3    3    3    2
[5,]    1    3    2    4    3
attr(,"names")
 [1] "A" "B" "C" "D" "E" NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA 
[20] NA  NA  NA  NA  NA  NA 

Since matrix can be regarded as two-dimensional vector, you'll assign names only to first five values (you don't want to do that, do you?). In this case, you should stick with colnames().

因为矩阵可以被看作是二维向量,所以你只需要给前五个值指定名称(你不想这样做,对吧?)在这种情况下,您应该使用colnames()。

So there...

所以…