Sunday, August 13, 2023

Kick start your R journey Getting started with R

 

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. 

Let’s start with data( mtcars), which is usually an inbuilt dataset

R can read most of the datasets which are commonly used. Here I am going to explain how you could import a spreadsheet to R’s global environment. 

R has introduced specific options for importing spreadsheets of common formats. You could easily scroll the dropdown menu of button in title bar “import dataset”. By clicking on this you can get the options on extra plugins to download. I am going to tell a different method, ie,by using code. Most R users like this, because of its convenience in automations. In this one is an inbuilt function and another one  you have to download. Both are essential for exploring the R world .

car<- mtcars 
View(mtcars)

Browsing through columns 

dollar method

Dollar method

Browse through your columns by adding $ symbol

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.

data[rows,columns]

so here it is

car[,]

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.

Find columns using Square bracket and number/name method
Find rows using Square bracket and number/name method

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.

rownames(carpart)
[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")

In conclusion, the first class of an R programming journey is a foundational step towards mastering this versatile and powerful language. As you delve into R, you'll find yourself equipped with the essential knowledge of its syntax, data structures, and basic programming concepts. This initial exposure sets the stage for more advanced topics like data manipulation, visualization, and statistical analysis, which are at the heart of R's capabilities. Whether you're a data scientist, statistician, or just someone interested in data-driven insights, the skills you acquire in this first class will serve as the building blocks for your future exploration and expertise in the world of R programming. So, embrace this beginning with enthusiasm, for it opens the door to a realm of endless possibilities in data analysis and beyond. 

Python: A Guide to Customizing Themes in Jupyter

  Hey there, Folks! It's been a while, hasn't it? Today, I'm excited to share a neat trick that'll make your Jupyter Lab or...