6

数値と名前を組み合わせた構造体である変数から数値を抽出する必要があります

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

最後に、この情報だけを含むベクトルが必要です

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

どうすればできますか?どうもありがとう

4

3 に答える 3

8

両方as.numeric()とこれをunname()行います:

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> 
于 2012-09-24T13:19:33.363 に答える
2
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

または

names(myVec) <- NULL

編集:

unnameアトミックなベクトルはnames(obj) <- NULL、余分なコードが含まれているだけです。

于 2012-09-24T13:16:46.223 に答える
2

どう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
于 2012-09-24T13:20:04.663 に答える