Create Point Chart for Cumulative Sums Using ggplot2 in R

Nizamuddin Siddiqui
Updated on 17-Oct-2020 13:47:12

1K+ Views

To create a point chart for cumulative sums using ggplot2, we need to use cumsum function for the dependent variable inside the aes function for aesthetic mapping that describes how the variable will be plotted. For example, if we have a data frame df that contain columns x and y where y is the dependent variable then the point chart for cumulative sums can be created as ggplot(df,aes(1:20,y=cumsum(y)))+geom_point().ExampleConsider the below data frame − Live Demoset.seed(666) x

Combine Two Lists of Same Size to Make a Data Frame in R

Nizamuddin Siddiqui
Updated on 17-Oct-2020 13:32:11

1K+ Views

If we have two lists of same size then we can create a data frame using those lists and this can be easily done with the help of expand.grid function. The expand.grid function create a data frame from all combinations of the provided lists or vectors or factors. For example, if we have two lists defined as List1 and List2 then we can create a data frame using the code expand.grid(List1,List2).Example Live DemoConsider the below lists −List1

Multiply Two Vectors in R as in Mathematics

Nizamuddin Siddiqui
Updated on 17-Oct-2020 13:29:28

7K+ Views

In mathematics, when two vectors are multiplied the output is a scalar quantity which is the sum of the product of the values. For example, if we have two vectors x and y each containing 1 and 2 then the multiplication of the two vectors will be 5. In R, we can do it by using t(x)%*%y.Example1 Live Demox1

Find the Mean of List Elements in R

Nizamuddin Siddiqui
Updated on 17-Oct-2020 13:21:52

6K+ Views

To find the mean of list elements we need to unlist those elements. For example, if we have a list named as List that contains three elements of equal or different sizes such element1, element2, and element3 then we can find the mean of all the list elements by using mean(unlist(List)).Example1List1

Replace Blanks in a Vector with Previous Element in R

Nizamuddin Siddiqui
Updated on 17-Oct-2020 13:18:27

219 Views

Filling of blanks is not an easy task in data analysis, especially if the vector contains numerical or integer values. Suppose we have a vector x that contains 1, , 2, 3, 4, 5 and we want to put 1 in place of blank after first value then cummax function along with seq_along function can be used as x[cummax(seq_along(x)*(x!=""))].Example1 Live Demox1

Remove Rows with Missing Values in R Data Frame

Nizamuddin Siddiqui
Updated on 17-Oct-2020 13:15:49

782 Views

If we want to remove rows containing missing values based on a particular column then we should select that column by ignoring the missing values. This can be done by using is.na function. For example, if we have a data frame df that contains column x, y, z and each of the columns have some missing values then rows of x without missing values can be selected as df[!is.na(df$x),].ExampleConsider the below data frame − Live Demox1

Concatenate Two or More Vectors in R

Nizamuddin Siddiqui
Updated on 17-Oct-2020 13:11:55

5K+ Views

The concatenation of vectors can be done by using combination function c. For example, if we have three vectors x, y, z then the concatenation of these vectors can be done as c(x,y,z). Also, we can concatenate different types of vectors at the same time using the same same function.Example1 Live Demoset.seed(999) x1

Find Total of Integer Column Based on Two Character Columns in R

Nizamuddin Siddiqui
Updated on 17-Oct-2020 13:09:45

133 Views

The calculation of total for integer column based on two different character columns simply means that we need to create a contingency table for the available data. For this purpose, we can use with and tapply function. For example, if we have a data frame df that contains two categorical columns defined as gender and ethnicity and an integer column defined as Package then the contingency table can be created as:with(df,tapply(Package,list(gender,ethnicity),sum))ExampleConsider the below data frame − Live Demoset.seed(777) Class

Convert List of Lists into a Single List in R

Nizamuddin Siddiqui
Updated on 17-Oct-2020 13:02:35

2K+ Views

A list can contain multiple lists of different type as well as similar type elements such as data frames, vectors, matrices etcetera but accessing those elements become a little difficult task. Therefore, it is better to convert a list that contain multiple lists into a single list and it can be done using unlist function.ExampleConsider the below vectors −set.seed(871) x1

Calculate Two-Period Moving Average for Vector Elements in R

Nizamuddin Siddiqui
Updated on 17-Oct-2020 12:59:59

223 Views

If we want to find the two−period moving average easily then it cannot be done in base R. We need to use rollmean function of zoo package that solves this problem in a single line of code. For example, if we have a vector x that contains hundred from starting from 1 to 100 then the two−period moving for x can be found by using rollmean(x,2)Loading zoo package −library(zoo)Examples Live Demox1

Advertisements