Dot-joined names automatically when you assign a value to a named array with a named variable

A simple example:

a <- quantile(1:10, 0.1) a 

output:

 10% 1.9 

Use to assign a value to a named array:

 b = c(index=0, value=a) b 

output:

 index value.10% 0.0 1.9 

Why are "value" and "10%" automatically joined by a dot? How to avoid this, because I just want to call it "value"?

+5
source share
2 answers

The object b created in the question is a vector named R, not an array R.

an array

An array R is a vector R with size (or dimensions) and, possibly, the dimnames attribute. It is created as follows:

 ar <- array(c(0, a), dimnames = list(c("index", "value"))); ar ## index value ## 0.0 1.9 

named vector

To create a named vector with the specified names, rather than create an array, use setNames to overwrite any existing names:

 v <- setNames(c(0, a), c("index", "value")); v ## index value ## 0.0 1.9 

or use names<- as follows:

 v <- c(0, a) names(v) <- c("index", "value") 

or as mentioned in the comments, create b from unname(a) , not from a , to avoid unified names.

attributes

Note the ar attributes:

 attributes(ar) ## List of 2 ## $ dim : int 2 ## $ dimnames:List of 1 ## ..$ : chr [1:2] "index" "value" 

and attributes v :

 attributes(v) ## $names ## [1] "index" "value" 
+3
source

The answers in the comments will help you in solving this problem, but I think you need to understand that a already a named vector. Therefore, using c ( c for concat ), you are trying to execute two objects of more than one type. That's why R is trying to make it work by combining the names, here is ordinary ( 10% ) and the one you want to give.

In this context, it would be more convenient for me to write c(c(index=0), a) , and then change the names or c(index=0, value=unname(a))

0
source

Source: https://habr.com/ru/post/1273683/


All Articles