0

I got an error and after some search I realized, that the "name of a character" causes the problem. However, I still don't understand how a character can be transferred to a function and I don't know how to solve it. The best I got is the name with extra "" - see below. (I could use other hacks like "mydim" everywhere, but that is not what I am looking for.)

So how can I make the following really work without an error?

a <- 5
special <- list(mydim = c(100L, 9L),
                mydimnames = list(c("1", "2"), c("3", "4", "5")))
names(special)[[1]]
# [1] "mydim"
special[[1]]
# [1] 100   9
attr(x = a, which = names(special)[[1]]) <- special[[1]]
attributes(a)
# $hurz
# [1] "hurz"
# $mydim
# [1] 100   9

special2 <- list(dim = c(100L, 9L),
                 dimnames = list(c("1", "2"), c("3", "4", "5")))
names(special2)[[1]]
# [1] "dim" # ............................ of class character only!!
special2[[1]]
# [1] 100   9

# The following commands all result in the same error:
attr(x = a, which = as.character(names(special2)[[1]])) <- special2[[1]]
# Error in attr(x = a, which = names(special2)[[1]]) <- special2[[1]] : 
#   dims [product 900] do not match the length of object [1]

# The best I got with extra '':
attr(x = a, which = deparse(names(special2)[[1]])) <- special2[[1]]
attributes(a)
# $hurz
# [1] "hurz"
# $mydim
# [1] 100   9
# $`"dim"`
# [1] 100   9

For the error message in general see here.
It seems although "dim" is a charcater, it uses dim() and calculates 9*100 = 900...

Christoph
  • 6,841
  • 4
  • 37
  • 89
  • 3
    This is documented in `?attr`. The attribute names "class, comment, dim, dimnames, names, row.names and tsp) are treated specially and have restrictions on the values which can be set." – user2554330 May 03 '23 at 11:59
  • Note that a `matrix` naturally has an attribute named `dim` (and perhaps `dimnames`), likely why (among many classes) `attr` has that restriction. – r2evans May 03 '23 at 12:01
  • @user2554330 Do you have a solution? the "values" are fine - just the name is changed. To me the documentation is misleading in this case(?) – Christoph May 03 '23 at 12:02
  • I don't see what you find to be misleading. Where was the name changed? – user2554330 May 03 '23 at 12:07
  • @user2554330 OK I understand, it is not that the chacrater is changed but a restriction is put on attribute-names. So what I wanted (my personal "dim" attribute is not possible? – Christoph May 03 '23 at 12:22
  • Right, it would only be allowed if it obeyed the same rules as the standard one. – user2554330 May 03 '23 at 12:27

1 Answers1

4

This is much simpler than you make out. The problem isn't "the name of a character"; the problem is the attribute name you have chosen. As documented in ?attr,

Note that some attributes (namely class, comment, dim, dimnames, names, row.names and tsp) are treated specially and have restrictions on the values which can be set. (Note that this is not true of levels which should be set for factors via the levels replacement function.)

You are trying to set the "dim" attribute of a, and the rule is that it needs to be a numeric vector whose product is the length of a. The reason for this rule is that "dim" is used by R internal functions, and if you were allowed to put something else there, they would need a lot more error checking, so would be a lot slower.

So your "mydim" solution is really the only way to go.

user2554330
  • 37,248
  • 4
  • 43
  • 90