R spells

Getting started with R

I hope you install the R and Rstudio successfully in your PC. Data manipulation in R is different from contemporary softwares for statistical – graphical analysis. Be in mind that R is not a software, rather it is a numerical computation program like MatLab or Octave. A detailed review on the capabilities of  R were explained the post. Here we are going to start with basic data manipulation on R for extreme beginners. Let’s start with data( mtcars), which is usually an inbuilt dataset. To make you know how to import a data from your PC, just go to the link here.

car<- mtcars  


Browsing through columns 

1) Dollar method

Browse through your columns by adding $ symbol



2) Square bracket and number/name method

Other methods are by using [] square brackets, or by using the exact column names(if you know) or by tab <=> button. However the result will be the same. Like maths, in R you can solve the same problems in different ways. Use the codes according to your convenience, and your data structure. Inside the [] , the data is represented by rows and columns.The basic structure is


so here it is


You could select desired columns by using “tab” button, when the cursor is in the square bracket[ ]. Similarly, you can shift the cursor left to the coma for rows.


you can use numbers instead of names of columns. i e, for the first column use 1 and for the second column use 2.


car[,1] # For first column
car[2,]# For second column

Get first four columns from the data[car], you have to select multiple columns by using the command “c”.

car[,c(1,2,3,4)] # first four columns by using no.s.
car[,c("mpg","cyl","disp","hp")] #first four columns by using names.

In the same way you can use rows too. Lets check the first 4 rows.

car[c(1,2,3,4),] # first four rows by number
car[c("Mazda RX4","Mazda RX4 Wag","Datsun 710","Hornet 4 Drive"),]#first four rows by name

Everything in the R needs a name, as in real life scenario. Otherwise R will forgot, and won’t seen on global environment.

 carpart<-  car[,c(1,2,3,4)]


This way you can define the subsets, i e you made your own dataset from the previous one.

Point data manipulation

You can do point data editing in R, before you doing this you should have a better understanding on datasets you are dealing with. For this you have to  specify both rows and columns.

just type

car[3,3] # 3rd row and 3rd column

check the output

[1] 108


Suppose you could change the value from 108 to 50

car[3,3]<- 50


Change row names and column names

Row names can be changed by function rownames(), lets the check the subset data “carpart” with rownames.


check result

 [1] "Mazda RX4"           "Mazda RX4 Wag"       "Datsun 710"          "Hornet 4 Drive"      "Hornet Sportabout"  
 [6] "Valiant"             "Duster 360"          "Merc 240D"           "Merc 230"            "Merc 280"           
[11] "Merc 280C"           "Merc 450SE"          "Merc 450SL"          "Merc 450SLC"         "Cadillac Fleetwood" 
[16] "Lincoln Continental" "Chrysler Imperial"   "Fiat 128"            "Honda Civic"         "Toyota Corolla"     
[21] "Toyota Corona"       "Dodge Challenger"    "AMC Javelin"         "Camaro Z28"          "Pontiac Firebird"   
[26] "Fiat X1-9"           "Porsche 914-2"       "Lotus Europa"        "Ford Pantera L"      "Ferrari Dino"       
[31] "Maserati Bora"       "Volvo 142E"

If you want to know column names, you can use two commands

> colnames(carpart) # colnames is a specific function
[1] "mpg"  "cyl"  "disp" "hp"  
> names(carpart)# names only work work for dataframes
[1] "mpg"  "cyl"  "disp" "hp"

You can change the column names/row names by assign the new names in order, lets check the column names.

  > names(carpart)
[1] "mpg" "cyl" "disp" "hp"

Now assign with new name.

names(carpart)<- c("miles/gal","cylinder","displacement","horsepower")
# or
colnames(carpart)<- c("miles/gal","cylinder","displacement","horsepower")

let’s check the new column names


I hope this will you make you start the R program from scratch, More advanced topics are coming soon. Please follow the category R-spells.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s