Reverse a Vector in R

Nizamuddin Siddiqui
Updated on 21-Aug-2020 06:34:16

765 Views

Sometimes the vector values are recorded in the reverse order in R, therefore, we need to again reverse those vectors to get the actual order we want. For example, a sequence of numbers might be recorded as 1 to 20 but we wanted it to be from 20 to 1. The reversing of order of the vector values can be easily done with the help of rev function.Examplesx1

Split a Continuous Variable into Multiple Groups in R

Nizamuddin Siddiqui
Updated on 21-Aug-2020 06:32:27

782 Views

Splitting a continuous variable is required when we want to compare different levels of a categorical variable based on some characteristics of the continuous variable. For example, creating the salary groups from salary and then comparing those groups using analysis of variance or Kruskal-Wallis test. To split a continuous variable into multiple groups we can use cut2 function of Hmisc package −Example Live DemoConsider the below data frame −set.seed(2) ID

Split Long String into Vector of Substrings of Equal Sizes in R

Nizamuddin Siddiqui
Updated on 21-Aug-2020 06:29:39

434 Views

If a vector is recorded as a single string by mistake or the file that contains the data did not separated the string in an appropriate way then we might need to split in the correct form so that we can proceed with the further analysis. This might happen when the levels of a factor variable that have equal name length are not separated. In this case, we can split the string into a vector that contain substring of equal sizes by using substring function.ExamplesJust look at these examples to understand how substring function can help us to split the ... Read More

Visualize the Normality of a Column in an R Data Frame

Nizamuddin Siddiqui
Updated on 21-Aug-2020 06:28:32

172 Views

The first step to analyze a variable is checking whether it is normally distributed or not and to visually do this, we create a histogram. If the histogram depicts a bell then we consider that the variable is normally distributed otherwise, it is not. We can create a histogram for any column of an R data frame by using hist function.ExampleConsider the below data frame −set.seed(9) df

Create Bar Plot in R with Labels on Top Using ggplot2

Nizamuddin Siddiqui
Updated on 21-Aug-2020 06:25:03

459 Views

There are multiple ways to represent a chart, specifically a bar plot is represented with so many variations. We can also include bar labels in a bar plot so that the viewer can easily understand the frequency of the categories for bars. To put the labels on top of the bars in a bar plot we can use vjust = 0 with geom_text in ggplot2.Example Live DemoConsider the below data frame −df

Create New Data Frame for Mean of Rows in R

Nizamuddin Siddiqui
Updated on 21-Aug-2020 06:22:16

889 Views

Finding row means help us to identity the average performance of a case if all the variables are of same nature and it is also an easy job. But if some of the columns have different type of data then we have to extract columns for which we want to find the row means. Therefore, we can create a new data frame with row means of the required columns using rowMeans function.Example Live DemoConsider the below data frame −set.seed(88) Group

Add or Multiply Elements of Two Matrices in R

Nizamuddin Siddiqui
Updated on 21-Aug-2020 06:20:14

153 Views

Basic mathematical operations such as addition, subtraction, multiplication, and division are common for matrices and we often do that but if the matrices are stored as a list in R then these basic calculations are done differently as they are not direct objects. To add or multiply the matrices in a list, we can use Reduce function with the plus (+) or multiply (*) sign and the list name.Example Live DemoConsider the below list of matrices −Matrices_List

Create Line Chart for Subset of Data Frame Using ggplot2 in R

Nizamuddin Siddiqui
Updated on 21-Aug-2020 06:17:33

618 Views

Subsetting is not a difficult thing in R but if we make our code short then it is a little tedious task because we will have to introduce code between codes and that creates confusion. Therefore, we must be very careful while writing a code inside another code. To create a line with subsetting the data frame using ggplot function of ggplot2 can be done by using subset function.Example Live DemoConsider the below data frame −set.seed(99) x1

Create a Venn Diagram in R

Nizamuddin Siddiqui
Updated on 21-Aug-2020 06:11:50

442 Views

A Venn diagram helps to identify the common and uncommon elements between two or more than two sets of elements. This is also used in probability theory to visually represent the relationship between two or more events. To create a Venn diagram in R, we can make use of venn function of gplots package.ExampleConsider the below vectorsx

Find Monthly Sales Using Date Variable in R

Nizamuddin Siddiqui
Updated on 21-Aug-2020 06:05:05

452 Views

Sales analysis requires to find monthly sales mean, total, range, and often standard deviation. This is mostly required by FMCG (Fast-Moving Consumer Goods) companies because they want to track their sales on daily basis as well as monthly basis. If we have daily sales data then we need to create another column for months in an R data frame to find the monthly sales and this can be done with the help of strftime and aggregate function.ExampleConsider the below data frame −date

Advertisements