Find Groupwise Mean and Sum in R Data Frame

Nizamuddin Siddiqui
Updated on 09-Nov-2021 07:33:44

215 Views

To find the groupwise mean and groupwise sum at the same time, we can first convert the data frame into a data.table object and then apply the sum function and mean function for data.table object groups as shown in the below examples.Example 1Following snippet creates a sample data frame −Group

Minus Every Element of a Vector with Another Vector in R

Nizamuddin Siddiqui
Updated on 09-Nov-2021 07:30:34

571 Views

To minus every element of a vector with every element of another vector, we can use outer function by defining the subtraction sign.For example, if we have two vectors say x and y and we want to minus every element in x from every element in y then we can use the below mentioned command −outer(x,y,`-`)Example 1Following snippet creates a sample vector −x1

Create Total Column in Data Frames Stored in R List

Nizamuddin Siddiqui
Updated on 09-Nov-2021 07:27:08

165 Views

To create a column of total in data frames stored in R list, we can follow the below steps −First of all, create a list of data frames.Then, use lapply function to create a column of total in data frames stored in the list.ExampleCreate the list of data framesUsing data.frame function to create data frames and list function to create the list of those data frames −df1

Change Name of Data Frames in an R List

Nizamuddin Siddiqui
Updated on 09-Nov-2021 07:21:33

2K+ Views

To change the name of data frames stored in an R list, we can follow the below steps −First of all, create a list of data frames.Then, use names function to change the name of data frames.ExampleCreate the list of data framesUsing data.frame function to create data frames and list function to create the list of those data frames −df1

Find All Combinations of Vector Elements Without Space in R

Nizamuddin Siddiqui
Updated on 09-Nov-2021 07:21:02

197 Views

When we want to find the combinations of a vector elements, we can use combn function with size of the combination say 2 for pairs but the result gives space between the values. If we do not want to get the combinations with space then combn function will be used with paste function by collapsing the space as shown in the below examples.Check out the below examples to understand how it can be done.Example 1To find all combinations of a vector elements without space, use the snippet given below −x1

Find Cumulative Sum for Each Row in an R Data Frame

Nizamuddin Siddiqui
Updated on 09-Nov-2021 07:20:02

1K+ Views

To find the cumulative sum for each row in an R data frame, we would need to read the data frame as a data.table object and then Reduce function will be used with accumulate argument.For Example, if we have a data frame called df and we want to find the cumulative sum for each row in df then we can use the below mentioned command −setDT(df)[,names(df):=Reduce("+",df,accumulate=TRUE)]Example 1Following snippet creates a sample data frame −x1

Find Frequency of Particular Value in R Data Frame Rows

Nizamuddin Siddiqui
Updated on 09-Nov-2021 07:18:16

1K+ Views

To find the frequency of particular value in rows of an R data frame, we can use mutate function of dplyr package along with rowSums function.For example, if we have a data frame called df then we can find the number of 5’s in each row of df by using the below command −df%>%mutate(Number_of_Fives=rowSums(.==1))Example 1Following snippet creates a sample data frame −x1

Find Column Maximum for Categorical Columns in R Data Frame

Nizamuddin Siddiqui
Updated on 09-Nov-2021 07:15:32

228 Views

To find the column maximum if some columns are categorical in R data frame, we can follow the below steps −First of all, create a data frame.Then, use numcolwise function from plyr package to find the column maximum if some columns are categorical.Example 1Create the data frameLet’s create a data frame as shown below −Group

Replace Numbers Written in Words with Numbers in R Data Frame Column

Nizamuddin Siddiqui
Updated on 09-Nov-2021 07:12:44

929 Views

To replace numbers written in words with numbers in an R data frame column, we can use within function.For example, if we have a data frame called df that contains a column say X having numbers from 1 to 5 written in words then we can convert them into numbers by using the below command −within(df,X

Replace Words in a Dichotomous Column for an R Data Frame with Numbers

Nizamuddin Siddiqui
Updated on 09-Nov-2021 07:10:31

464 Views

A dichotomous column can be represented by words such as Yes/No, Good/Bad, Right/Wrong etc. To replace such words with number, say 1 and 0 we can use ifelse function.For example, if we have a data frame called df that contains a column Binary with Yes and No values then we can replace Yes with 1 and No with 0 by using the below command −df$Binary

Advertisements