删除使用unname的data.frame的dimnames无效

时间:2022-04-27 13:17:54

I tried to remove both rownames and colnames of a data.frame. As Jason saied, this can be done with unname then set rownames to NULL. I noticed that the manual says using unname with option force = T would do the same thing in single step:

我试图删除data.frame的行名和colname。正如Jason所说,这可以用unname完成,然后将行名称设置为NULL。我注意到手册上说使用unname和option force = T会在单步中做同样的事情:

force logical; if true, the dimnames (names and row names) are removed even from data.frames.

力的逻辑;如果为真,则从data.frame中删除dimnames(名称和行名称)。

however, this doesn't work for me:

然而,这并不适合我:

d <- data.frame(a = c(x1 = 1, x2 = 2), b = c(x1 = 'a', x2 = 'b'))
unname(d, force = T)

# Error in `dimnames<-.data.frame`(`*tmp*`, value = NULL) : 
#   invalid 'dimnames' given for data frame

what is the reason?

的原因是什么?

sessionInfo()
# R version 3.3.1 (2016-06-21)
# Platform: x86_64-pc-linux-gnu (64-bit)
# Running under: Ubuntu 16.04.1 LTS
# 
# locale:
#  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
#  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
#  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
#  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
#  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
# [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
# 
# attached base packages:
# [1] stats     graphics  grDevices utils     datasets  methods   base

2 个解决方案

#1


3  

Weird. Looks like a bug from here (code behaviour doesn't match documentation).

奇怪。从这里看起来像是一个bug(代码行为与文档不匹配)。

As a work-around, to remove the rownames, you'll need to:

作为一个解决方案,要删除行名,您需要:

> d <- unname(d)
> rownames(d) <- NULL
> d

1 1 a
2 2 b

The left-most column of numbers is R's standard data.frame row numbering and is applied to data.frames without rownames.

最左边的数字列是R的标准数据。frame行编号,并应用于数据。没有rowname的帧。

#2


-1  

in case you need a solution for your data but doesn't give answer for why does force = T not working.

如果你需要一个数据的解决方案,但是没有给出为什么force = T不工作的答案。

#read data
d <- data.frame(a = c(x1 = 1, x2 = 2), b = c(x1 = 'a', x2 = 'b'))

删除使用unname的data.frame的dimnames无效

#convert to matrix
m1 <- as.matrix(d) 
#remove dimnames
m2 <- matrix(mm1, ncol = ncol(d), dimnames = NULL)
m2 #output the data

删除使用unname的data.frame的dimnames无效

Seems that unname() is not working for data.frame even if does say it works in documentation.

看起来unname()对data.frame不起作用,即使它在文档中工作。

#1


3  

Weird. Looks like a bug from here (code behaviour doesn't match documentation).

奇怪。从这里看起来像是一个bug(代码行为与文档不匹配)。

As a work-around, to remove the rownames, you'll need to:

作为一个解决方案,要删除行名,您需要:

> d <- unname(d)
> rownames(d) <- NULL
> d

1 1 a
2 2 b

The left-most column of numbers is R's standard data.frame row numbering and is applied to data.frames without rownames.

最左边的数字列是R的标准数据。frame行编号,并应用于数据。没有rowname的帧。

#2


-1  

in case you need a solution for your data but doesn't give answer for why does force = T not working.

如果你需要一个数据的解决方案,但是没有给出为什么force = T不工作的答案。

#read data
d <- data.frame(a = c(x1 = 1, x2 = 2), b = c(x1 = 'a', x2 = 'b'))

删除使用unname的data.frame的dimnames无效

#convert to matrix
m1 <- as.matrix(d) 
#remove dimnames
m2 <- matrix(mm1, ncol = ncol(d), dimnames = NULL)
m2 #output the data

删除使用unname的data.frame的dimnames无效

Seems that unname() is not working for data.frame even if does say it works in documentation.

看起来unname()对data.frame不起作用,即使它在文档中工作。