Tag Archives: r programming

Linear Regression T-test: Formula, Example

Linear regression line slope 0

Last updated: 29th Nov, 2023 Linear regression is a popular statistical method used to model the relationship between a dependent variable and one or more independent variables. In linear regression, the t-test is a statistical hypothesis testing technique that is used to test the hypothesis related to linearity of the relationship between the response variable and different predictor variables. In this blog, we will discuss linear regression and t-test and related formulas and examples. For a detailed read on linear regression, check out my related blog – Linear regression explained with real-life examples. T-tests are used in linear regression to determine if a particular variable is statistically significant in the …

Continue reading

Posted in Data Science, Python, R, statistics. Tagged with , , , .

How to Add Rows to DataFrames in R Using dplyr: Examples

Add Row to R Dataframe using dplyr

Data manipulation is a fundamental aspect of data analysis, and R, with its dplyr package, offers an efficient and readable way to perform such tasks. In my experience working with various datasets, I have often encountered situations where I needed to add rows to an existing DataFrame. The dplyr package, part of the tidyverse collection, makes these tasks intuitive and efficient. In this blog post, I’ll share two common scenarios: adding a single row and adding multiple rows to a DataFrame using dplyr. If you would want to learn about how to add rows to Pandas Dataframe using Python, check out my related post – Pandas Dataframe: How to Add …

Continue reading

Posted in Data Science, R. Tagged with , .

Learn R – How to Define Function in R

This article represents code examples in relation with how to write function in R. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos. Following are the key points described later in this article: Function Definition Code Examples for Functions   How to Define Function Function in R looks like following: # Function definition funcName <- function( a, b, c ) { return(v) # or simply v } # This is how a function is invoked var1 <- funcName(x,y,x) In above code, funcName is the name of the function. a, b, c are parameters. v is return variable Code Examples …

Continue reading

Posted in Data Science. Tagged with .

Learn R – How to Create Multiple Density Plots using GGPlot

This article represents code samples which could be used to create multiple density curve or plots using ggplot2 package in R programming language. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos. Multiple Density Curves/Graphs with GGPlot The code samples given below works for “diamonds” dataset which is loaded as part of ggplot2 package. Following are two different types of plots shown below: Density plots with multiple fills Density plot with single fill Density Plots with Multiple Fills:Following code represents density plots with multiple fills. Pay attention to the “fill” parameter passed to “aes” method. # Create density plots for …

Continue reading

Posted in Data Science. Tagged with .

Learn R – How to Create Density Plot over Histogram

This article represents code examples for overlaying or creating density curve on Histogram using ggplot2 package in R programming. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos. Code Samples to Overlay Density Curve on Histogram In the code examples below, diamonds data set belonging to ggplot2 package is used. One must load the ggplot2 package (require(“ggplot2”)) before executing the code samples given below. # Most simplistic density curve ggplot(diamonds, aes(x=carat)) + geom_histogram(aes(y=..density..)) + geom_density() + labs(title=”Histogram & Density Curve”, x=”Carat”) Following diagram would get displayed by executing the above code. # Density curve with histogram painted using body …

Continue reading

Posted in Data Structure. Tagged with .

Learn R – 3 Commands to Generate Random Numbers

This article represents 3 different commands with code examples which could be used to generate random numbers in R programming language. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos. Following are the key points described later in this article: Runif command Sample command Rnorm command Difference between runif and rnorm command Runif: Generate Random Numbers based on Uniform Distribution “Runif” command can be used for generating random numbers based on uniform distribution. One can generate one or more random numbers within a range of numbers. One should note that the random numbers generated using runif commands are all …

Continue reading

Posted in Data Science. Tagged with .

Learn R – Extract Data Frame with One Column

This article represents code sample that could be used to create/extract data frame with one column from existing data frame. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos. Extract Data Frame with One Column In the code sample below, diamonds dataset from ggplot2 package is used. To work with the example below, one needs to load the ggplot2 library using command such as require(“ggplot2”). In the command below, method as.data.frame is used. Make a note of drop=false parameter passed to as.data.frame method. dfn1 <- as.data.frame(diamonds[,c(1)], drop=false)

Posted in Data Science. Tagged with .

Learn R – How to Create Histogram using GGPlot

This article represents techniques (commands samples) which could be used to create histogram using ggplot2 package in R programming. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos. Following is the summary of commands used to create histogram using ggplot: Using simplistic ggplot and geom_histogram method Using ggplot and geon_histogram command with attributes such as col, fill, alpha Using ggplot, geom_histogram and  scale_fill_gradient method Common Techniques to Create Histogram using ggplot2 In the code examples below, diamonds dataset from ggplot2 package is used. To work with examples below, load the ggplot2 library prior to executing the commands given below. …

Continue reading

Posted in Data Science. Tagged with .

Learn R – How to Create Data Frames using Existing Data Frame

This article represents commands that could be used to create data frames using existing data frame. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos. Following is a list of command summary for creating data frames by extracting multiple columns from existing data frame based on following criteria, whose sample is provided later in this article: Column indices Column names Subset command Data.frame command 6 Techniques for Extracting Data Frame from Existing Data Frames Following commands have been based on diamonds data frame which is loaded as part of loading ggplot2 library.   Following is how the diamonds data …

Continue reading

Posted in Data Science. Tagged with .

Learn R – 5 Techniques to Create Empty Data Frames with Column Names

This article represents techniques on how one could create an empty data frame with column names. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos. 5 Techniques to Create Empty Data Frames In each of the examples below, the data frame is created with three columns, namely, ‘name’, ‘rating’, ‘relyear’. It represents moview names, ratings, and the release year. # Command data.frame is used df1 <- data.frame(name=””, rating=””, relyear=””, stringsAsFactors=FALSE) # Command data.frame is used df2 <- data.frame(name=character(), rating=character(), relyear=character(), stringsAsFactors=FALSE) # Usage of read.table command to create empty data frame df3 <- read.table(text = “”, colClasses = c(“character”, …

Continue reading

Posted in Data Science. Tagged with .

Learn R – How to Get Data Frames Columns as Vectors

This article represents different ways in which one could get a data frame column as a vector. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos. 4 Techniques to Get Data Frame Column as Vector In the examples below, diamonds dataset from ggplot2 package is considered. This is how a diamond dataset looks like: Following are four different technique/method using which one could retrieve a data frame column as a vector. # In the data set shown above, carat represents column name and hence, [[‘carat’]] carat1 <- diamonds[[‘carat’]] # In the data set shown above, carat represents 1st column …

Continue reading

Posted in Big Data. Tagged with .

Learn R – How to Get Random Training and Test Data Set

This article represents sample source code which could be used to extract random training and test data set from a data frame using R programming language. The R code below could prove very handy while you are working to create a model using any machine learning algorithm. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos.   # Read the data from a file; The command below assumes that the working # directory has already been set. One could set working directory using # setwd() command. sample_df <- read.csv(“glass.data”, header=TRUE, stringsAsFactors=FALSE) # get a vector comprising of all indices …

Continue reading

Posted in Big Data. Tagged with , , .

Learn R – How to Extract Rows & Columns from Data Frame

This article represents command set in R programming language, which could be used to extract rows and columns from a given data frame. When working on data analytics or data science projects, these commands come very handy in data cleaning activities.  This article is meant for beginners/rookies getting started with R and wanting to know or see examples of extracting information from a data frame. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos. Following are the key points described later in this article: Commands to extract rows and columns Command to extract a column as data frame Command …

Continue reading

Posted in Big Data. Tagged with , .