কোনও বৈশ্বিক সামগ্রীর নীচে লুকিয়ে থাকলে কীভাবে ভেরিয়েবলের ধরণ পাবেন:
আপনার যা কিছু প্রয়োজন তা মূল ধরণের উপর আর ম্যানুয়ালটিতে রয়েছে: 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 টি লাইন আর কোড দিয়ে আমি করেছি এই দুর্দান্ত জিনিসটি দেখুন! কীভাবে এটি করা উচিত তা আমার কোনও ধারণা নেই তবে এটি কী করে!"
typeof()
কীভাবে বস্তুটি স্মৃতিতে সংরক্ষণ করা হয় সে সম্পর্কে একটি খুব জেনেরিক তথ্য দেয়। সবচেয়ে ব্যবহারের ক্ষেত্রে জন্য আপনি একটি পরিবর্তনশীল সম্পর্কে ভাল তথ্য জানতে চাইx
, আপনার কাছ থেকে আরও তথ্য পেতে করবclass(x)
,is(x)
অথবাstr(x)
(তারা কত বিস্তারিত প্রদান ক্রমানুসারে)। দেখুন নিচের এরিক এর উত্তর কি উদাহরণের জন্যtypeof()
আপনি বলে: কারণinteger
; তালিকাগুলি, ডেটা ফ্রেম, মডেল অবজেক্টস, এবং অন্যান্য উন্নত অবজেক্টগুলি কেবলlist
...