R Variables का इस्तेमाल R data को store करने के लिए किया जाता है |
Rules of Variables
- Variable ये case-sensetive होता है | for eg int a और int A ये अलग-अलग variables है |
- Variable की शुरुआत किसी भी alphabet(a-z, A-Z) या dot(.) से होती है |
- Variable ये space को allow नहीं करता |
- Variable name कोई भी R Keywords नहीं होता है |
- Variables का नाम alphanumeric हो सकता है | For eg. a1 = 5, var1, var2
Examples for Valid Variables in R
> a = 5 > a1 = 6 > .a = 7 > .a. = 8 > a_b = 9 > a_b_ = 10
Examples for Invalid Variables in R
> 1a = 5 Error: unexpected symbol in "1a" > _a = 6 Error: unexpected input in "_" > @a = 7 Error: unexpected '@' in "@" > a@ = 8 Error: unexpected '=' in "a@ =" > TRUE = 9 Error in TRUE = 9 : invalid (do_set) left-hand side to assignment > if = 10 Error: unexpected '=' in "if ="
Assigning Value to Variable in R
R Variables पर value को assign करने के लिए assignment(=), leftward(<-) या rightward(->) operators का इस्तेमाल किया जाता है |
> a = 1 # assign value using assignment operator > b <- 2 # assign value using leftward operator > 3 -> c # #assign value using rightward operator
Display Variable's value using print(), cat() and paste() Function in R
RGui के अंतर्गत जब R scipt file को save किया जाता है तब R console पर उस file को आसानी से run करने के लिए source() function का इस्तेमाल इस्तेमाल किया जाता है |
print() Function ये single argument के लिए होता है |
cat() Function ये multiple argument के लिए होता है |
paste() Function print() के अंतर्गत होता है | ये function arguments को concatenate करके character data type में result को display करता है |
sample.ROutput :a = 1 b <- 2 3 -> c print(a) print(paste("Value of a is ",a)) cat("Value of b is ",b,"\n") cat("Value of c is ",c,"\n")
> source("E:\\UD\\Documents\\hello.R") [1] 1 [1] "Value of a is 1" Value of b is 2 Value of c is 3
Finding Variables in R
R workspace पर store हुए सभी variables को display करने के लिए ls() function का इस्तेमाल किया जाता है |
निचे दिया गया output ये मेरे workspace पर stored हुए सभी variables है | ये output; workspace पर store हुए variables के हिसाब से आता है |
> ls() [1] "a" "A" "add.result" "add.vec3" "b" [6] "c" "cnt" "dec" "inc" "max.temp" [11] "P" "v" "var" "vec" "vec1" [16] "vec2" "vec3"
ls() function में pattern ये parameter दिया गया है | pattern की value ये variable name पर दिए गए dot(.) के left side का हिस्सा होता है |
> ls(pattern="add") [1] "add.result" "add.vec3"
R में dot(.) से शुरू होने वाले variables hidden होते है लेकिन is() function के all या all.name इस parameter से show हो जाते है |
> print(ls(all = TRUE)) [1] ".Random.seed" "a" "A" "add.result" "add.vec3" [6] "b" "c" "cnt" "dec" "inc" [11] "max.temp" "P" "v" "var" "vec" [16] "vec1" "vec2" "vec3"
Removing Variable from Workspace in R
Workspace से variable को remove करने के लिए rm() function का इस्तेमाल किया जाता है |
> a = 1 > a [1] 1 > rm(a) > a Error: object 'a' not found
निचे Workspace से सभी variables को remove किया गया है |
> rm(list = ls())
Data Type of Variable
R में variables को declare नहीं किया जाता है | जब R Variable पर value को assign किया जाता है तब dynamically उसे value के हिसाब से data type दिया जाता है |
Variable का data type देखने के लिए class() function का इस्तेमाल किया जाता है |
> var1 = 5 > var2 = 5L > var3 = 'Hello' > var4 = 2+3i > class(var1) [1] "numeric" > class(var2) [1] "integer" > class(var3) [1] "character" > class(var4) [1] "complex"