Find Sum of Column Values Up to Another Column Value in R

Nizamuddin Siddiqui
Updated on 05-Nov-2021 07:55:35

2K+ Views

To find the sum of a column values up to a particular value in another column, we can use cumsum function with sum function.For example, if we have a data frame called df that contains two columns say x and y and we want to find the sum of x values until y is equal to 2 then we can use the following command −sum(df$x[cumsum(df$y==2)==0])Example 1Following snippet creates a sample data frame −x1

Create Sample of Rows Using ID Column in R

Nizamuddin Siddiqui
Updated on 05-Nov-2021 07:52:14

553 Views

To create sample of rows using ID column, we can use sample function. We would need to apply the sample function on ID column and take the subset of rows with the help of single square brackets.For example, if we have a data frame called df that contains an ID column say ID then we can sample 4 rows of df using ID column as follows −df[sample(df$ID,4),]Example 1Following snippet creates a sample data frame −Emp_ID

Create Quartile Column for Each Value in an R Data Frame Column

Nizamuddin Siddiqui
Updated on 05-Nov-2021 07:51:41

3K+ Views

Any numerical data can be divided into four parts by using three quartiles, first quartile at 25%, second quartile at 50% and third quartile at 75% hence there will be four quarters to represent first 25%, second 25%, third 25% and the last 25% in a set of data.If we want to create a quartile (1 to 4) column for each value in an R data frame column then we can use the quantile function and cut function as shown in the below Examples.Example 1Following snippet creates a sample data frame −x

Find Combination of Matrix Values in R

Nizamuddin Siddiqui
Updated on 05-Nov-2021 07:49:21

557 Views

To find the combination of matrix values in R, we can use expand.grid function with split function.For example, if we have a matrix called M then to create the combination of matrix values we can use the code mentioned below −do.call(expand.grid,split(M,rep(1:nrow(M),ncol(M))))Check out the examples given below to understand how it works.Example 1Following snippet creates a matrix −M1

Round Values in Proportion Table in R

Nizamuddin Siddiqui
Updated on 05-Nov-2021 07:45:35

2K+ Views

To round values in proportion table in R, we can first save the proportion table in an object and then use the round function.For example, if we have a vector say X then we can create a proportion table for data in X using prop.table(table(X)) and store it in an object called Tab and then round the values to two decimal places by using the below given command −round(Tab,2)Example 1Following snippet creates a sample data frame −x1

Convert Matrices Stored in a List into Data Frames in R

Nizamuddin Siddiqui
Updated on 05-Nov-2021 07:42:11

664 Views

A list may contain vectors, data frames, matrices, lists etc. If a list contains matrices and we want to convert those matrices into data frames then lapply function can be used along with as.data.frame function.For example, if we have a list called LIST that contains matrices then we can convert those matrices into data frames by using the below command −lapply(LIST,function(x) as.data.frame(x))ExampleFollowing snippet creates a list of matrices −M1

Find Mean of Column Grouped by Date in R

Nizamuddin Siddiqui
Updated on 05-Nov-2021 07:37:06

1K+ Views

To find the mean of a column grouped by date, we can simply use aggregate function. For Example, if we have a data frame called df that contains a date column say Date and numerical column say Num then we can find the mean of Num by dates in the Date column by using the below command −aggregate(Num~Date,df,mean)Example 1Following snippet creates a sample data frame −Date

Rotate a ggplot2 Graph in R

Nizamuddin Siddiqui
Updated on 05-Nov-2021 07:36:23

1K+ Views

To rotate a ggplot2 graph, we can save it in an object and then use the print function by defining the angle with viewport.For example, if we have a graph saved in an object called PLOT then we can rotate it to 180 degrees by using the below mentioned command −print(PLOT,vp=viewport(angle=180))ExampleFollowing snippet creates a sample data frame −x

Find Frequency for All Columns Based on a Condition in R

Nizamuddin Siddiqui
Updated on 05-Nov-2021 07:34:13

496 Views

To find the conditional frequency for all columns based on a condition, we can use for loop where we will define the length of each column with condition for which we want to find the frequency.For example, if we have a data frame called df and we want to find the number of values in each column that are greater than 5 then we can use the below given command − Columns

Add Row Percentages to Contingency Table in R

Nizamuddin Siddiqui
Updated on 05-Nov-2021 07:31:44

2K+ Views

To add row percentage to contingency table in R, we can use rowSums and sum function with table values and combine them with cbind function.For Example, if we have a table called TAB then we can add row percentages to TAB by using the below command −cbind(TAB,rowSums(TAB),rowSums(TAB)/sum(TAB))Example 1Following snippet creates a sample data frame −Grp1

Advertisements