2016-06-21 14 views
12

आर, जब आप सांख्यिक को बाइनरी से एक सदिश विवश में, नाम दूर छीन रहे हैं।आर में, 'as.numeric` का उपयोग करते समय मैं वेक्टर के नाम कैसे संरक्षित कर सकता हूं?

कुछ संभव समाधान है, जो मैंने पहले रेखांकित किया है रहे हैं। यह सभी मूल्यों को 0 जोड़कर अंतर्निहित रूपांतरण पर भरोसा करने के खतरनाक लगता है, और sapply() मेरी संचालन के लिए एक अतिरिक्त पाश (जो अक्षम है) कहते हैं। as.numeric का उपयोग कर वेक्टर को परिवर्तित करते समय नामों को संरक्षित करने का कोई और तरीका है?

# Set the seed 
set.seed(1045) 

# Create a small sample vector and give it names 
example_vec <- sample(x = c(TRUE,FALSE),size = 10,replace = TRUE) 
names(example_vec) <- sample(x = LETTERS,size = 10,replace = FALSE) 

example_vec 
#  Y  N  M  P  L  J  H  O  F  D 
# FALSE TRUE FALSE FALSE TRUE TRUE TRUE TRUE TRUE TRUE 

as.numeric(x = example_vec) 
# [1] 0 1 0 0 1 1 1 1 1 1 

example_vec + 0 
# Y N M P L J H O F D 
# 0 1 0 0 1 1 1 1 1 1 

sapply(X = example_vec,FUN = as.numeric) 
# Y N M P L J H O F D 
# 0 1 0 0 1 1 1 1 1 1 
+1

सांख्यिक को 'वर्ग()' बदलने भंडारण मोड को बदलने के रूप में ही प्रभाव है लगता है। – joran

+1

बल्कि बदसूरत, लेकिन आप 'चला सकते हैं setNames (as.numeric (example_vec), नाम (example_vec))' –

+3

मैं नहीं अस्पष्ट या स्पष्ट रूपांतरण में किसी भी जोखिम दिख रहा है। यदि आप पूर्णांक पसंद करते हैं और कम वर्ण टाइप करते हैं, तो '+ example_vec' का उपयोग करें।साथ ही, मुझे डेटाटेबल के साथ टैग करने का कोई कारण नहीं दिखता है जबतक कि आप 'setattr' या कुछ का उपयोग करने के बारे में सोच रहे हों। – Frank

उत्तर

10

बस वहाँ एक और विकल्प फेंकने के लिए, के बाद से अपने इनपुट एक तार्किक वेक्टर है, तो आप ifelse() उपयोग कर सकते हैं। और एक बहस कर सकते इस दृष्टिकोण अधिक स्पष्ट और सीधा है:

ifelse(example_vec,1L,0L); 
## Y N M P L J H O F D 
## 0 1 0 0 1 1 1 1 1 1 

बेंचमार्किंग

library(microbenchmark); 

ifelse. <- function(x) ifelse(x,1L,0L); 
sapply. <- function(x) sapply(x,as.integer); 
setstoragemode <- function(x) { storage.mode(x) <- 'integer'; x; }; 
setmode <- function(x) { mode(x) <- 'integer'; x; }; 
setclass <- function(x) { class(x) <- 'integer'; x; }; 
as.and.setnames <- function(x) setNames(as.integer(x),names(x)); 
plus <- function(x) +x; 
addzero <- function(x) x+0L; 

## small scale (OP's example input) 
set.seed(1045L); 
x <- sample(c(T,F),10L,T); 
names(x) <- sample(LETTERS,10L); 

ex <- ifelse.(x); 
identical(ex,sapply.(x)); 
## [1] TRUE 
identical(ex,setstoragemode(x)); 
## [1] TRUE 
identical(ex,setmode(x)); 
## [1] TRUE 
identical(ex,setclass(x)); 
## [1] TRUE 
identical(ex,as.and.setnames(x)); 
## [1] TRUE 
identical(ex,plus(x)); 
## [1] TRUE 
identical(ex,addzero(x)); 
## [1] TRUE 

microbenchmark(ifelse.(x),sapply.(x),setstoragemode(x),setmode(x),setclass(x),as.and.setnames(x),plus(x),addzero(x)); 
## Unit: nanoseconds 
##    expr min  lq  mean median  uq max neval 
##   ifelse.(x) 6843 8126.0 9627.13 8981 9837.0 21810 100 
##   sapply.(x) 18817 20100.5 23234.93 21383 22666.5 71418 100 
## setstoragemode(x) 856 1283.0 1745.54 1284 1711.0 15396 100 
##   setmode(x) 7270 8126.0 9862.36 8982 10264.0 32074 100 
##   setclass(x) 429 1283.0 2138.97 1284 1712.0 32075 100 
## as.and.setnames(x) 1283 1711.0 1997.78 1712 2139.0 7271 100 
##    plus(x)  0 428.0 492.39 428 428.5 9837 100 
##   addzero(x)  0 428.0 539.39 428 856.0 2566 100 

## large scale 
set.seed(1L); 
N <- 1e5L; 
x <- sample(c(T,F),N,T); 
names(x) <- make.unique(rep_len(LETTERS,N)); 

ex <- ifelse.(x); 
identical(ex,sapply.(x)); 
## [1] TRUE 
identical(ex,setstoragemode(x)); 
## [1] TRUE 
identical(ex,setmode(x)); 
## [1] TRUE 
identical(ex,setclass(x)); 
## [1] TRUE 
identical(ex,as.and.setnames(x)); 
## [1] TRUE 
identical(ex,plus(x)); 
## [1] TRUE 
identical(ex,addzero(x)); 
## [1] TRUE 

microbenchmark(ifelse.(x),sapply.(x),setstoragemode(x),setmode(x),setclass(x),as.and.setnames(x),plus(x),addzero(x)); 
## Unit: microseconds 
##    expr  min   lq   mean  median   uq  max neval 
##   ifelse.(x) 7633.598 7757.1900 16615.71251 7897.4600 29401.112 96503.642 100 
##   sapply.(x) 86353.737 102576.0945 125547.32957 123909.1120 137900.406 264442.788 100 
## setstoragemode(x) 84.676  92.8015 343.46124  98.3605 113.543 23939.133 100 
##   setmode(x) 124.020 155.0245 603.15744 167.2125 181.111 22395.736 100 
##   setclass(x) 85.104  92.3740 328.25393 100.2850 118.460 21807.713 100 
## as.and.setnames(x) 70.991  78.2610 656.98177  82.3235  88.953 35710.697 100 
##    plus(x) 40.200  42.9795  48.68026  44.9040  49.608  88.953 100 
##   addzero(x) 181.326 186.4580 196.34882 189.6650 201.211 282.679 100 

## very large scale 
set.seed(1L); 
N <- 1e7L; 
x <- sample(c(T,F),N,T); 
names(x) <- make.unique(rep_len(LETTERS,N)); 

ex <- ifelse.(x); 
identical(ex,sapply.(x)); 
## [1] TRUE 
identical(ex,setstoragemode(x)); 
## [1] TRUE 
identical(ex,setmode(x)); 
## [1] TRUE 
identical(ex,setclass(x)); 
## [1] TRUE 
identical(ex,as.and.setnames(x)); 
## [1] TRUE 
identical(ex,plus(x)); 
## [1] TRUE 
identical(ex,addzero(x)); 
## [1] TRUE 

microbenchmark(ifelse.(x),sapply.(x),setstoragemode(x),setmode(x),setclass(x),as.and.setnames(x),plus(x),addzero(x),times=5L); 
## Unit: milliseconds 
##    expr   min   lq   mean  median   uq   max neval 
##   ifelse.(x) 1082.220903 1308.106967 3452.639836 1473.723533 6306.320235 7092.82754  5 
##   sapply.(x) 16766.199371 17431.458634 18401.672635 18398.345499 18843.890150 20568.46952  5 
## setstoragemode(x) 13.298283 13.648103 173.574496 19.661753 24.736278 796.52806  5 
##   setmode(x) 19.043796 19.878573 75.669779 19.969235 39.683589 279.77370  5 
##   setclass(x) 14.025292 14.119804 259.627934 14.414457 26.838618 1228.74150  5 
## as.and.setnames(x) 12.889875 24.241484 178.243948 24.962934 25.103631 804.02182  5 
##    plus(x)  7.577576  7.676364  9.047674  8.245142  8.253266 13.48602  5 
##   addzero(x) 18.861615 18.960403 71.284716 26.622226 26.950662 265.02867  5 

लगता एकल प्लस केक ले जाता है की तरह। (और मेरे ifelse() विचार थोड़े बेकार है।)

13

एक संभावना यह mode<- प्रतिस्थापन समारोह का उपयोग करने के वस्तु के आंतरिक भंडारण मोड (प्रकार) को बदलने के लिए है। इसके अलावा, तार्किक जबरदस्ती के इस मामले के लिए पूर्णांक युगल (यानी संख्यात्मक) से अधिक उपयुक्त होते हैं।

mode(example_vec) <- "integer" 
example_vec 
# Y N M P L J H O F D 
# 0 1 0 0 1 1 1 1 1 1 

help(mode) से -

mode(x) <- "newmode"newmode करने के लिए वस्तु x के मोड बदल जाता है। यह तभी एक उपयुक्त as.newmode समारोह है, उदाहरण के "logical", "integer", "double", "complex", "raw", "character", "list", "expression", "name", "symbol" और "function" लिए समर्थित है। विशेषताएँ संरक्षित हैं।

प्रलेखन भी लिखते हैं कि storage.mode<-mode<- के एक अधिक कुशल आदिम संस्करण है। तो निम्नलिखित भी इस्तेमाल किया जा सकता है।

storage.mode(example_vec) <- "integer" 

लेकिन जैसे @joran टिप्पणी में कहा, ऐसा लगता है class<- की तरह भी एक ही बात करता है।

संबंधित मुद्दे

 संबंधित मुद्दे