Vectors Aur Assignment
R ka conventional assignment operator <- hai, = nahi. x <- 5 ko "5, into x" ki tarah padha jaata hai aur yeh wo form hai jo aapko practically har R code aur documentation mein milega (= bhi top level par assignment ke liye kaam karta hai, lekin <- hi idiom hai). Koi declaration keyword nahi hota kisi bhi kism ka — kisi naam ko di gayi first assignment hi use existence mein laati hai, wahi minimal style jo Python aur Ruby use karte hain.
c() ("combine" ka short form) ek se zyada elements ka vector banane ka tareeka hai. Indexing 1-based hoti hai — x[1] first element hai, second nahi — jo Python, JavaScript, Rust, Go, Java, C, C++, C#, Kotlin, aur Swift se genuinely alag hai, kyunki ye sab 0 se counting start karte hain.
x <- c(10, 20, 30) print(x) print(x[1]) print(length(x)) y <- 5 print(y[1]) print(length(y))
[1] 10 20 30 [1] 10 [1] 3 [1] 5 [1] 1
x[1] correctly 10 return karta hai, first element — 20 nahi, jo 0-based indexing deti. Lekin y ko dekho: ise ek single number, 5, assign kiya gaya tha, c() ko bilkul call kiye bina — aur y[1] phir bhi kaam karta hai, 5 return karta hai, aur length(y) 1 report karta hai. y kabhi bhi "sirf ek number" nahi tha jise indexing tolerate kar rahi ho; yeh create hone ke pehle second se hi length 1 ka numeric vector tha. Aage: data types — numeric, character, logical, integer, aur NA, plus yeh ki jab ek value us type mein coerce nahi ho sakti jo aapne maanga tha to kya hota hai.
Practice exercise
<- use karke x <- c(5, 10, 15) create karo, phir x[2] print karo (yaad rakho R indexing 1 se start hoti hai, 0 se nahi) aur length(x) print karo.