Nizamuddin Siddiqui

Nizamuddin Siddiqui

1,958 Articles Published

Articles by Nizamuddin Siddiqui

Page 131 of 196

Convert a numeric column to binary factor based on a condition in R data frame

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 10-Nov-2021 4K+ Views

To convert a numeric column to binary factor based on a condition in R data frame, we can use factor function along with ifelse function.For Example, if we have a data frame called df that contains a numerical column say Num and we want to convert it to a binary factor if Num is less than 100 then it will be Minor otherwise Major then we can use the below given command −df$Num_Factor

Read More

How to standardize data.table object column by group in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 10-Nov-2021 579 Views

To standardize data.table object column by group, we can use scale function and provide the grouping column with by function.For Example, if we have a data.table object called DT that contains two columns say G and Num where G is a grouping column and Num is a numerical column then we can standardize Num by column G by using the below given command −DT[,"Num":=as.vector(scale(Num)),by=G]Example 1Consider the below data.table object −library(data.table) Grp

Read More

Replace each value in a column with the largest value based on a condition in R data frame.

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 10-Nov-2021 279 Views

Suppose we have three columns say X, Y, and Z in an R data frame called df and we want to replace values in columns X and Y with the same value if the values are greater than values in Z and if they are less than the values in Z then we can replace with Z values.Check out the below Examples to understand how it can be done.Example 1Following snippet creates a sample data frame −x1

Read More

How to randomly replace values in an R data frame column?

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

The random replacement values in an R data frame column can be done with the help of sample function along with nrow function and single square subsetting.For Example, if we have a data frame called df that contains a columns say X and we want to randomly replace 5 values in X with 1.5 then we can use the below given command −df$X[sample(nrow(df),5)]

Read More

How to multiply corresponding values from two data.table objects in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 10-Nov-2021 634 Views

To multiply corresponding values from two data.table objects in R, we can follow the below steps −First of all, create two data.table objects.Then, use mapply function to multiply corresponding values from those two data.table objects.ExampleCreate the first data.table objectLet’s create a data.table object as shown below −library(data.table) x1

Read More

R Programming how to display both axes' labels of a ggplot2 graph in italics?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 10-Nov-2021 324 Views

To display both axes’ labels of a ggplot2 graph in italics in R, we can use theme function where we can set the face of axis.text.x and axis.text.y argument to italic.For Example, if we have a data frame called df that contains two columns say X and Y then we can create a scatterplot of X and Y with axes labels in italics by using the below mentioned command −ggplot(df, aes(X, Y))+geom_point()+theme(axis.text.x=element_text(face="italic"), axis.text.y=element_text(face="italic"))ExampleFollowing snippet creates a sample data frame −x

Read More

How to change the color of a particular bar using geom_bar in R?

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

To change the color of a particular bar using geom_bar in R, we can provide the count corresponding to the value for which we want to change the color inside aes function.For Example, if we have a data frame called df that contains two columns say V and F where V is categorical and F is for frequency and we want to change the color of frequency 10 in bar plot then we can use the below mentioned command −ggplot(df, aes(V, F))+geom_bar(aes(fill=..F..==10), stat="identity")ExampleFollowing snippet creates a sample data frame −x

Read More

How to add a value to a particular matrix element in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 10-Nov-2021 4K+ Views

To add a value to a particular matrix element in R, we can use subsetting for the particular value with single square brackets.For Example, if we have a matrix called M and we want to add 10 to the fifth value in second column then can use the below mentioned command −M[5,2]

Read More

How to find the number of zeros in each column of an R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 10-Nov-2021 11K+ Views

To find the number of zeros in each column of an R data frame, we can follow the below steps −First of all, create a data frame.Then, use colSums function to find the number of zeros in each column.Example 1Create the data frameLet’s create a data frame as shown below −x

Read More

How to multiply corresponding row values in a matrix with single row matrix in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 10-Nov-2021 516 Views

To multiply row values in a matrix having multiple rows with single row matrix in R, we can follow the below steps −First of all, create a matrix with multiple rows and a matrix with single row.Then, use mapply function to multiply row values in those matrices.ExampleCreate the first matrixLet’s create a matrix as shown below −M1

Read More
Showing 1301–1310 of 1,958 articles
« Prev 1 129 130 131 132 133 196 Next »
Advertisements