How to extract numerical values โ€‹โ€‹from a structure object in R

I need to extract numerical values โ€‹โ€‹from a variable, which is a structure combined with numerical values โ€‹โ€‹and names

structure(c(-1.14332132657709, -1.1433213265771, -1.20580568266868, -1.75735158849487, -1.35614113300058), .Names = c("carbon", "nanotubes", "potential", "neuron", "cell", "adhesion")) 

In the end I would like to have a vector with this information

 c(-1.14332132657709, -1.1433213265771, -1.20580568266868, -1.75735158849487, -1.35614113300058) 

How can i do this? many thanks

+6
source share
3 answers

Both as.numeric() and unname() do this:

 R> structure(c(-1.14332132657709, -1.1433213265771, -1.20580568266868, + -1.75735158849487, -1.35614113300058, NA), + .Names = c("carbon", "nanotubes", "potential", + "neuron", "cell", "adhesion")) carbon nanotubes potential neuron cell adhesion -1.14332 -1.14332 -1.20581 -1.75735 -1.35614 NA R> foo carbon nanotubes potential neuron cell adhesion -1.14332 -1.14332 -1.20581 -1.75735 -1.35614 NA R> R> as.numeric(foo) ## still my 'default' approach [1] -1.14332 -1.14332 -1.20581 -1.75735 -1.35614 NA R> R> unname(foo) ## maybe preferable though [1] -1.14332 -1.14332 -1.20581 -1.75735 -1.35614 NA R> 
+8
source
 myVec <- structure(c(-1.14332132657709, -1.1433213265771, -1.20580568266868, -1.75735158849487, -1.35614113300058), .Names = c("carbon", "nanotubes", "potential", "neuron", "cell")) as.numeric(myVec) # [1] -1.143321 -1.143321 -1.205806 -1.757352 -1.356141 

or

 names(myVec) <- NULL 

EDIT:

unname for an atomic vector is just names(obj) <- NULL with some redundant code.

+2
source

What about unname ?

 > myVec <- structure(c(-1.14332132657709, -1.1433213265771, -1.20580568266868, -1.75735158849487, -1.35614113300058), .Names = c("carbon", "nanotubes", "potential", "neuron", "cell")) + + > > unname(myVec) [1] -1.143321 -1.143321 -1.205806 -1.757352 -1.356141 
+2
source

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


All Articles