Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove dimnames of data.frame using unname doesn't work

Tags:

dataframe

r

names

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:

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

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
like image 669
mt1022 Avatar asked Oct 18 '22 00:10

mt1022


1 Answers

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

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.

like image 173
Jason Avatar answered Oct 21 '22 00:10

Jason