R Programming Articles

Page 115 of 174

How to change the name of data frames stored in an R list?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 09-Nov-2021 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

Read More

How to find all combinations of a vector elements without space in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 09-Nov-2021 227 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

Read More

How to find the cumulative sum for each row in an R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 09-Nov-2021 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

Read More

How to find the frequency of particular value in rows of an R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 09-Nov-2021 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

Read More

How to find the column maximum if some columns are categorical in R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 09-Nov-2021 241 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

Read More

How to replace numbers written in words with numbers in an R data frame column?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 09-Nov-2021 957 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

Read More

How to replace words in a dichotomous column for an R data frame with numbers?\\n

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 09-Nov-2021 478 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

Read More

How to create a histogram for uniform data in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 09-Nov-2021 400 Views

To create a histogram for uniform data in R, we can simply use hist function but the bars size may vary even if the frequency is same. Therefore, for this purpose, we need to define the breaks as shown in the below example.ExampleTo create a histogram for uniform data in R, use the code given below −hist(1:6)OutputIf you execute the above given code, it generates the following output −To create a histogram for uniform data in R, use the code given below −hist(1:8) OutputIf you execute the above given code, it generates the following output −To create a histogram for ...

Read More

How to find the smallest number in an R data frame column excluding values zero or less?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 09-Nov-2021 3K+ Views

To find the smallest number in an R data frame column excluding values zero or less, we can use min function along with subsetting of values greater than 0 through single square brackets.For example, if we have a data frame called df that contains a column say X then the smallest number excluding values zero or less can be found by using the below command −min(df[df$X>0,1])Example 1Following snippet creates a sample data frame −x

Read More

How to find the dot product of two matrices in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 09-Nov-2021 2K+ Views

To find the dot product of two matrices in R, we can use dot function of geometry package.For Example, if we have two matrices say matrix1 and matrix2 then we can use the dot product of these two matrices by using the below given command −dot(matrix1,matrix2)Example 1Following snippet creates a sample matrix −M1

Read More
Showing 1141–1150 of 1,740 articles
« Prev 1 113 114 115 116 117 174 Next »
Advertisements