Posts

Add a new column or variable to a table in R

Add a new column or variable to a table in R #  In R, rows are called observations and columns are called variables We have a table below called tabletrains with four variables and we want to add another variable/column called "distance" tabletrains TrainNo From To DateofJourney 12863 HWH YPR 21-Nov-15 12245 HWH YPR 21-Nov-15 12378 NJP SDAH 20-Nov-15 52544 DJ KGN 18-Nov-15 13149 BWN NJP 15-Nov-15 12864 KJM HWH 13-Nov-15 12864 KJM HWH 10-Nov-15 12640 KJM MAS 4-Nov-15 12840 MAS HWH 4-Nov-15 12246 YPR HWH 16-Aug-15 Steps: install.packages("dplyr") # hit Ctrl + Enter library(dplyr)                   # hit Ctrl + Enter # once the package in loaded, tabletrains <- tabletrains %>%                       mutate("distance") # Select the code and hit Ctrl

Mean, Median, Mode in R - Central Tendency

Mean, Median, Mode in R - Central Tendency x <- c(1,2,3,4,5,6,7,7,7) # calculate mean mean(x)  4.666667 # calculate median median(x) 5 # calculate mode tbl(x) 1 2 3 4 5 6 7   1 1 1 1 1 1 3   #mode == 7 as it is present 3 times Remember 1. # is used to comment in R and will not execute 2. Ctrl + Enter will executed the code in line where the cursor is. No need to select the code. 3. Mean == average 4. Median == Middle value in a sorted data set 5. Mode == Most common observation # calculate standard deviation # post your results if you found this useful sd(x) # calculate variance # post your result if you found this useful var(x) # calculate IQR (interquartile range) IQR(x)

filter data in R

How to Filter Data from a Table in R Imagine you have a table below called "trains" and you want to filter Train to HWH KJM <- filter(trains, To == "HWH") TrainNo From To DateofJourney 12863 HWH YPR 21-Nov-15 12245 HWH YPR 21-Nov-15 12378 NJP SDAH 20-Nov-15 52544 DJ KGN 18-Nov-15 13149 BWN NJP 15-Nov-15 12864 KJM HWH 13-Nov-15 12864 KJM HWH 10-Nov-15 12640 KJM MAS 4-Nov-15 12840 MAS HWH 4-Nov-15 12246 YPR HWH 16-Aug-15 Things to remember; 1. R is case sensitive HWH & hwh are not the same 2. results from using filter is stored in KJM 3. for equal we use double ==