সমস্ত ভেরিয়েবলের প্রকার পান


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/manouts/R-lang.html# বেসিক- টাইপস

আপনার অভ্যন্তরটি দেখার আগে আপনার object()প্রয়োজনীয়তা প্রবেশ করা দরকার get(...)। উদাহরণ:

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

আর এ থাকা ভেরিয়েবলের ধরণটি কীভাবে পাবেন

উদাহরণস্বরূপ, সর্বাধিক গভীরতায় টাইপ দেওয়ার জন্য আর ফাংশনটির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আপনার ভেরিয়েবলের ডেটা পান

যখন কোনও আর ভেরিয়েবলটি ডিস্কে লেখা হয়, তখন ডেটা বিন্যাসটি আবার পরিবর্তন হয় এবং তাকে ডেটাstorage.mode বলা হয় । ফাংশনটি storage.mode(...)এই নিম্ন স্তরের তথ্য প্রকাশ করে: দেখুন মোড, শ্রেণি এবং আর অবজেক্টের ধরণ । আর-এর স্টোরেজ.মোড সম্পর্কে আপনাকে চিন্তা করার দরকার নেই যতক্ষণ না আপনি ডিস্কে এবং ডেটা অ্যাসাইজ করার সময় এবং পাঠানোর সময় ঘটে যাওয়া রাউন্ড ট্রিপ কাস্ট / জবরদস্তিগুলির কারণে সৃষ্ট বিলম্ব বুঝতে চেষ্টা না করে।

আর এর ট্রাইড টাইপিং সিস্টেমের চারপাশে মতাদর্শ:

আর এর হাঁসের টাইপিং সিস্টেমের মধ্যে অনিশ্চয়তা রয়েছে। সাদৃশ্য হিসাবে, একটি সিরামিক কাপ বিবেচনা করুন, এটি একটি তরল ধরে রাখতে ব্যবহার করা যেতে পারে, বা বেসবলের মতো প্রক্ষিপ্ত হিসাবে ব্যবহার করা যেতে পারে। কাপটির উদ্দেশ্য এটি উপলভ্য বৈশিষ্ট্য এবং এটির উপর নির্ভর করে ফাংশন নির্ভর করে। এই ধরণের ফ্লুয়লটিটি প্রোগ্রামারদের জন্য কোনও ফাংশন থেকে অন্য ফাংশনে যে কোনও ধরণের আউটপুট পুনঃনির্দেশ করতে দেয় এবং আর আপনার মন পড়ার চেষ্টা করতে এবং যুক্তিসঙ্গত কিছু করার চেষ্টা করে আর বেশি পরিমাণে যায়।

ধারণা যে যখন নবাগত প্রোগ্রামারদের ব্রোমিন মাধ্যমে আর প্রোগ্রাম লিখতে, যেমন তারা, তারা একটি পাস প্রচেষ্টা googah.blimflargএকটি মধ্যে vehicle.subspaceresponder(...)। কোনও টাইপ-ত্রুটি বাড়ানোর পরিবর্তে, আর প্রোগ্রামটি জিমন্যাস্টিকগুলি ধরণের রূপান্তর করতে এবং তারপরে আশ্চর্যজনকভাবে কার্যকর কিছু করে। নবাগত প্রোগ্রামারটি তার ব্লগে কোড পোস্ট করে এবং বলে যে "এই কোডটি 3 টি লাইন আর কোড দিয়ে আমি করেছি এই দুর্দান্ত জিনিসটি দেখুন! কীভাবে এটি করা উচিত তা আমার কোনও ধারণা নেই তবে এটি কী করে!"


উদাহরণস্বরূপ ডিএস <- সি (3,4,5,5,3) উদাহরণস্বরূপ কীভাবে চিহ্নিত করবেন - যে "ডিএস" হ'ল সংখ্যার প্রকারযুক্ত ভেক্টর হ'ল?
সর্বোচ্চ উসানিন

1
আপনার নিজস্ব কাস্টম আর ফাংশন তৈরি করুন যা আপনি আপনার সরঞ্জাম বাক্সে রেখেছেন যা একটি প্যারামিটার x নেয়। টাইপফ (এক্স) সংখ্যাসূচক কিনা এবং শ্রেণি (এক্স) ভেক্টর কিনা তা পরীক্ষা করতে বিবৃতি ব্যবহার করে ফাংশনের অভ্যন্তরে ব্যবহার করুন। যদি তা হয় তবে স্ট্রিংটি মুদ্রণ করুন: "x হ'ল একটি সংখ্যার প্রকারযুক্ত ভেক্টর হ'ল"। আর এই বিভাগে আপনাকে সাহায্য করতে যাচ্ছে না কারণ এই ত্রিদেশীয় টাইপিং সিস্টেমে অসীম জটিলতা রয়েছে, প্রকার বিশ্লেষণ অসম্ভব, আপনি সমস্ত প্রকারের সংজ্ঞা দেওয়ার সাথে সাথেই কেউ একটি নতুনকে সংজ্ঞায়িত করে। আর টাইপিং সিস্টেমটি সবচেয়ে বেশি খারাপ যে কোনও ভাষা আমি দেখেছি। এটি ল্যান্ডফিল আগুন।
এরিক লেসচিনস্কি

6

পরিবর্তনশীল প্রকারটি পরীক্ষা করতে আপনি ক্লাস (এক্স) ব্যবহার করতে পারেন। যদি প্রয়োজন হয় যে সমস্ত ভেরিয়েবলের ধরণের ডেটা ফ্রেম পরীক্ষা করা দরকার তবে স্যাপ্লি (এক্স, ক্লাস) ব্যবহার করা যেতে পারে।


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

ল্যাপলি (আপনার_ডাটাফ্রেম, ক্লাস) আপনাকে এরকম কিছু দেয়:

ik টিকর [1] "ফ্যাক্টর"

$ তারিখ [1] "তারিখ"

$ [1] "সংখ্যাসূচক" খুলুন

$ উচ্চ [1] "সংখ্যাসূচক"

... ইত্যাদি

আমাদের সাইট ব্যবহার করে, আপনি স্বীকার করেছেন যে আপনি আমাদের কুকি নীতি এবং গোপনীয়তা নীতিটি পড়েছেন এবং বুঝতে পেরেছেন ।
Licensed under cc by-sa 3.0 with attribution required.