सभी प्रकार के चर प्राप्त करें


118

आर में, मैं अपनी स्क्रिप्ट के अंत में वैश्विक चर की एक सूची प्राप्त करना चाहूंगा और उन पर पुनरावृति करूंगा। यहाँ मेरा कोड है

#declare a few sample variables
a<-10
b<-"Hello world"
c<-data.frame()

#get all global variables in script and iterate over them
myGlobals<-objects()
for(i in myGlobals){
  print(typeof(i))     #prints 'character'
}

मेरी समस्या यह है कि typeof(i)हमेशा characterपरिवर्तनशील होते हुए भी लौटता है aऔर cवर्ण चर नहीं हैं। लूप के लिए मैं मूल प्रकार का चर कैसे प्राप्त कर सकता हूं?


इस प्रश्न को पढ़ने वाले लोगों पर ध्यान दें: typeof()स्मृति में ऑब्जेक्ट को कैसे संग्रहीत किया जाता है, इस बारे में बहुत सामान्य जानकारी देता है। सबसे उपयोग के मामलों के लिए, यदि आप एक चर बारे में अच्छी जानकारी जानना चाहते हैं x, तो आप से और अधिक उपयोगी जानकारी मिल जाएगा class(x), is(x)या str(x)(वे कितना विस्तार प्रदान के क्रम में)। एरिक के जवाब को नीचे देखें जो typeof()आपको बताता है कि उदाहरण : कारक हैं integer; सूची, डेटा फ़्रेम, मॉडल ऑब्जेक्ट, अन्य उन्नत ऑब्जेक्ट बस हैं list...
ग्रेगर थॉमस

जवाबों:


109

आपके getद्वारा दिए गए ऑब्जेक्ट के चरित्र नाम के बजाय मान प्राप्त करने के लिए उपयोग करने की आवश्यकता है ls:

x <- 1L
typeof(ls())
[1] "character"
typeof(get(ls()))
[1] "integer"

वैकल्पिक रूप से, समस्या के लिए प्रस्तुत के रूप में आप उपयोग करना चाहते हैं eapply:

eapply(.GlobalEnv,typeof)
$x
[1] "integer"

$a
[1] "double"

$b
[1] "character"

$c
[1] "list"

पूरी तरह से काम करें। क्या आप जानते हैं कि अगर कोई प्रदर्शन पेनल्टी मिलती है तो क्या () का उपयोग कई बड़े डेटा फ़्रेमों के प्रकार को खोजने के लिए किया जाता है जो वस्तुओं द्वारा लौटी चर सूची में मौजूद हो सकते हैं?

1
getइसके आलोचक हैं और मुझे लगता है कि eapplyयह एक व्याख्यायित पाश से तेज होगा। लेकिन इसका पता लगाने का केवल एक ही तरीका है ...
जेम्स

17

एक वैश्विक वस्तु के नीचे छिपे हुए चर का प्रकार कैसे प्राप्त करें:

आपको जो कुछ भी चाहिए वह बुनियादी प्रकारों पर आर मैनुअल में है: https://cran.r-project.org/doc/manuals/R-lang.html#Basic-types

आपका object()जरूरतों के साथ प्रवेश किया जाना है get(...)इससे पहले कि आप के अंदर देख सकते हैं। उदाहरण:

a <- 10
myGlobals <- objects()
for(i in myGlobals){
  typeof(i)         #prints character
  typeof(get(i))    #prints integer
}

आर में आपके पास किस प्रकार का चर है

उदाहरण के लिए R फ़ंक्शनtypeof में आपको अधिकतम गहराई पर टाइप देने का पूर्वाग्रह है।

library(tibble)

#expression              notes                                  type
#----------------------- -------------------------------------- ----------
typeof(TRUE)             #a single boolean:                     logical
typeof(1L)               #a single numeric with L postfixed:    integer
typeof("foobar")         #A single string in double quotes:     character
typeof(1)                #a single numeric:                     double
typeof(list(5,6,7))      #a list of numeric:                    list
typeof(2i)               #an imaginary number                   complex

#So far so good, but those who wish to keep their sanity go no further
typeof(5 + 5L)           #double + integer is coerced:          double
typeof(c())              #an empty vector has no type:          NULL
typeof(!5)               #a bang before a double:               logical
typeof(Inf)              #infinity has a type:                  double
typeof(c(5,6,7))         #a vector containing only doubles:     double
typeof(c(c(TRUE)))       #a vector of vector of logicals:       logical
typeof(matrix(1:10))     #a matrix of doubles has a type:       list

#Strangeness ahead, there be dragons: step carefully:
typeof(substr("abc",2,2))#a string at index 2 which is 'b' is:  character
typeof(c(5L,6L,7L))      #a vector containing only integers:    integer
typeof(c(NA,NA,NA))      #a vector containing only NA:          logical
typeof(data.frame())     #a data.frame with nothing in it:      list
typeof(data.frame(c(3))) #a data.frame with a double in it:     list
typeof(c("foobar"))      #a vector containing only strings:     character
typeof(pi)               #builtin expression for pi:            double

#OK, I'm starting to get irritated, however, I am also longsuffering:
typeof(1.66)             #a single numeric with mantissa:       double
typeof(1.66L)            #a double with L postfixed             double
typeof(c("foobar"))      #a vector containing only strings:     character
typeof(c(5L, 6L))        #a vector containing only integers:    integer
typeof(c(1.5, 2.5))      #a vector containing only doubles:     double
typeof(c(1.5, 2.5))      #a vector containing only doubles:     double
typeof(c(TRUE, FALSE))   #a vector containing only logicals:    logical

#R is really cramping my style, killing my high, irritation is increasing:
typeof(factor())         #an empty factor has default type:     integer
typeof(factor(3.14))     #a factor containing doubles:          integer
typeof(factor(T, F))     #a factor containing logicals:         integer
typeof(Sys.Date())       #builtin R dates:                      double
typeof(hms::hms(3600))   #hour minute second timestamp          double
typeof(c(T, F))          #T and F are builtins:                 logical
typeof(1:10)             #a builtin sequence of numerics:       integer
typeof(NA)               #The builtin value not available:      logical

#The R coolaid punchbowl has been spiked: stay frosty and keep your head low:
typeof(c(list(T)))       #a vector of lists of logical:         list
typeof(list(c(T)))       #a list of vectors of logical:         list
typeof(c(T, 3.14))       #a vector of logicals and doubles:     double
typeof(c(3.14, "foo"))   #a vector of doubles and characters:   character
typeof(c("foo",list(T))) #a vector of strings and lists:        list
typeof(list("foo",c(T))) #a list of strings and vectors:        list
typeof(TRUE + 5L)        #a logical plus an integer:            integer
typeof(c(TRUE, 5L)[1])   #The true is coerced to 1              integer
typeof(c(c(2i), TRUE)[1])#logical coerced to complex:           complex
typeof(c(NaN, 'batman')) #NaN's in a vector don't dominate:     character
typeof(5 && 4)           #doubles are coerced by order of &&    logical
typeof(8 < 'foobar')     #string and double is coerced          logical
typeof(list(4, T)[[1]])  #a list retains type at every index:   double
typeof(list(4, T)[[2]])  #a list retains type at every index:   logical
typeof(2 ** 5)           #result of exponentiation              double
typeof(0E0)              #exponential lol notation              double
typeof(0x3fade)          #hexidecimal                           double
typeof(paste(3, '3'))    #paste promotes types to string        character
typeof(3 +)           #R pukes on unicode                    error
typeof(iconv("a", "latin1", "UTF-8")) #UTF-8 characters         character
typeof(5 == 5)           #result of a comparison:               logical

आर में आपके पास एक चर की श्रेणी कैसे प्राप्त करें

द आर classउदाहरण के लिए फ़ंक्शन में आपके प्रकारों को घेरने वाले कंटेनर या संरचना देने के लिए एक पूर्वाग्रह है।

library(tibble)

#expression            notes                                    class
#--------------------- ---------------------------------------- ---------
class(matrix(1:10))     #a matrix of doubles has a class:       matrix
class(factor("hi"))     #factor of items is:                    factor
class(TRUE)             #a single boolean:                      logical
class(1L)               #a single numeric with L postfixed:     integer
class("foobar")         #A single string in double quotes:      character
class(1)                #a single numeric:                      numeric
class(list(5,6,7))      #a list of numeric:                     list
class(2i)               #an imaginary                           complex
class(data.frame())     #a data.frame with nothing in it:       data.frame
class(Sys.Date())       #builtin R dates:                       Date
class(sapply)           #a function is                          function
class(charToRaw("hi"))  #convert string to raw:                 raw
class(array("hi"))      #array of items is:                     array

#So far so good, but those who wish to keep their sanity go no further
class(5 + 5L)           #double + integer is coerced:          numeric
class(c())              #an empty vector has no class:         NULL
class(!5)               #a bang before a double:               logical
class(Inf)              #infinity has a class:                 numeric
class(c(5,6,7))         #a vector containing only doubles:     numeric
class(c(c(TRUE)))       #a vector of vector of logicals:       logical

#Strangeness ahead, there be dragons: step carefully:
class(substr("abc",2,2))#a string at index 2 which is 'b' is:  character
class(c(5L,6L,7L))      #a vector containing only integers:    integer
class(c(NA,NA,NA))      #a vector containing only NA:          logical
class(data.frame(c(3))) #a data.frame with a double in it:     data.frame
class(c("foobar"))      #a vector containing only strings:     character
class(pi)               #builtin expression for pi:            numeric

#OK, I'm starting to get irritated, however, I am also longsuffering:
class(1.66)             #a single numeric with mantissa:       numeric
class(1.66L)            #a double with L postfixed             numeric
class(c("foobar"))      #a vector containing only strings:     character
class(c(5L, 6L))        #a vector containing only integers:    integer
class(c(1.5, 2.5))      #a vector containing only doubles:     numeric
class(c(TRUE, FALSE))   #a vector containing only logicals:    logical

#R is really cramping my style, killing my high, irritation is increasing:
class(factor())       #an empty factor has default class:      factor
class(factor(3.14))   #a factor containing doubles:            factor
class(factor(T, F))   #a factor containing logicals:           factor
class(hms::hms(3600)) #hour minute second timestamp            hms difftime
class(c(T, F))        #T and F are builtins:                   logical
class(1:10)           #a builtin sequence of numerics:         integer
class(NA)             #The builtin value not available:        logical

#The R coolaid punchbowl has been spiked: stay frosty and keep your head low:
class(c(list(T)))       #a vector of lists of logical:         list
class(list(c(T)))       #a list of vectors of logical:         list
class(c(T, 3.14))       #a vector of logicals and doubles:     numeric
class(c(3.14, "foo"))   #a vector of doubles and characters:   character
class(c("foo",list(T))) #a vector of strings and lists:        list
class(list("foo",c(T))) #a list of strings and vectors:        list
class(TRUE + 5L)        #a logical plus an integer:            integer
class(c(TRUE, 5L)[1])   #The true is coerced to 1              integer
class(c(c(2i), TRUE)[1])#logical coerced to complex:           complex
class(c(NaN, 'batman')) #NaN's in a vector don't dominate:     character
class(5 && 4)           #doubles are coerced by order of &&    logical
class(8 < 'foobar')     #string and double is coerced          logical
class(list(4, T)[[1]])  #a list retains class at every index:  numeric
class(list(4, T)[[2]])  #a list retains class at every index:  logical
class(2 ** 5)           #result of exponentiation              numeric
class(0E0)              #exponential lol notation              numeric
class(0x3fade)          #hexidecimal                           numeric
class(paste(3, '3'))     #paste promotes class to string       character
class(3 +)           #R pukes on unicode                   error
class(iconv("a", "latin1", "UTF-8")) #UTF-8 characters         character
class(5 == 5)           #result of a comparison:               logical

storage.modeअपने चर का डेटा प्राप्त करें

जब R वेरिएबल को डिस्क पर लिखा जाता है, तो डेटा लेआउट फिर से बदल जाता है, और इसे डेटाstorage.mode कहा जाता है । फ़ंक्शन storage.mode(...)इस निम्न स्तर की जानकारी को प्रकट करता है: देखें मोड, क्लास और प्रकार की आर ऑब्जेक्ट देखें । आपको R के संग्रहण के बारे में चिंता करने की आवश्यकता नहीं है। जब तक कि आप राउंड ट्रिप कास्ट / कॉर्किंस के कारण होने वाली देरी को समझने की कोशिश नहीं कर रहे हैं, जब डिस्क से और इसके लिए डेटा असाइन करना और पढ़ना होता है।

R के त्रिगुण टाइपिंग सिस्टम के आसपास की विचारधारा:

R के duck टाइपिंग सिस्टम में अनिश्चितता है। एक सादृश्य के रूप में, एक सिरेमिक कप पर विचार करें, इसका उपयोग तरल रखने के लिए किया जा सकता है, या एक बेसबॉल की तरह प्रक्षेप्य के रूप में उपयोग किया जा सकता है। कप का उद्देश्य यह उपलब्ध गुणों और उस पर कार्य करने वाले कार्य पर निर्भर करता है। प्रकार की यह तरलता प्रोग्रामर के लिए किसी भी प्रकार के आउटपुट को एक फ़ंक्शन से दूसरे फ़ंक्शन में पुनर्निर्देशित करने के लिए अधिक से अधिक लेवे की अनुमति देती है, और आर आपके दिमाग को पढ़ने और कुछ उचित करने की कोशिश करने के लिए बड़ी लंबाई तक जाएगा।

यह विचार है कि जब नौसिखिया प्रोग्रामर ब्राउन प्रोग्राम के माध्यम से आर प्रोग्राम लिखते हैं, जैसा कि वे करेंगे, तो वे एक googah.blimflargमें पास होने का प्रयास करते हैं vehicle.subspaceresponder(...)। एक टाइप-एरर को पुक करने के बजाय, आर प्रोग्राम जिम्नास्टिक को टाइप बदलने के लिए करता है और फिर आश्चर्यजनक रूप से उपयोगी कुछ करता है। नौसिखिया प्रोग्रामर अपने ब्लॉग पर कोड पोस्ट करता है और कहता है "इस जबरदस्त चीज को देखो जो मैंने आर कोड की 3 लाइनों के साथ किया था! मुझे नहीं पता कि यह कैसे करना है, लेकिन यह जानता है!"


उदाहरण के लिए पहचान कैसे करें ds <- c (3,4,5,5,3) - कि "ds" वास्तव में वेक्टर है जिसमें संख्यात्मक प्रकार होते हैं?
मैक्स उसानिन

1
अपना खुद का कस्टम आर फ़ंक्शन बनाएं जो आप अपने टूल बॉक्स में चारों ओर रखते हैं जो एक पैरामीटर x लेता है। फ़ंक्शन के अंदर यदि टाइपोफ़ (x) संख्यात्मक है और यदि वर्ग (x) एक सदिश है, तो जाँचने के लिए कथन का उपयोग करें। यदि ऐसा है, तो स्ट्रिंग को प्रिंट करें: "x एक वेक्टर है जो एक संख्यात्मक प्रकार के साथ है"। आर आपको इस विभाग में मदद करने के लिए नहीं जा रहा है क्योंकि इस त्रय टंकण प्रणाली में अनंत जटिलता है, टाइप विश्लेषण असंभव है, जैसे ही आप सभी प्रकारों को परिभाषित करते हैं, कोई नया परिभाषित करता है। टाइपिंग सिस्टम अब तक सबसे खराब है जो मैंने किसी भी भाषा में देखा है। यह एक लैंडफिल फायर है।
एरिक लेसिंसकी

6

आप चर प्रकार की जांच करने के लिए वर्ग (x) का उपयोग कर सकते हैं। यदि आवश्यकता किसी डेटा फ्रेम के सभी चर प्रकार की जांच करने की है तो sapply (x, class) का उपयोग किया जा सकता है।


4
> mtcars %>% 
+     summarise_all(typeof) %>% 
+     gather
    key  value
1   mpg double
2   cyl double
3  disp double
4    hp double
5  drat double
6    wt double
7  qsec double
8    vs double
9    am double
10 gear double
11 carb double

मैं कोशिश करता हूं classऔर typeofकार्य करता हूं , लेकिन सब विफल रहता है।


1

मूल रूप से आप जो चाहते थे उसका उलटा करने के लिए डिज़ाइन किया गया है, यहाँ मेरा टूलकिट खिलौने में से एक है:

 lstype<-function(type='closure'){
inlist<-ls(.GlobalEnv)
if (type=='function') type <-'closure'
typelist<-sapply(sapply(inlist,get),typeof)
return(names(typelist[typelist==type]))
}

0

lapply (your_dataframe, class) आपको कुछ ऐसा देता है:

$ tikr [1] "कारक"

$ तारीख [1] "तारीख"

$ ओपन [1] "न्यूमेरिक"

$ उच्च [1] "संख्यात्मक"

... आदि।

हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.