Multiply Corresponding Values from Two Data Table Objects in R

Nizamuddin Siddiqui
Updated on 10-Nov-2021 07:33:11

585 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

Display Both Axes Labels of a ggplot2 Graph in Italics

Nizamuddin Siddiqui
Updated on 10-Nov-2021 07:30:12

281 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 −xRead More

Find Percentage of Zeros in Each Column of a Matrix in R

Nizamuddin Siddiqui
Updated on 10-Nov-2021 07:24:15

234 Views

To find the percentage of zeros in each column of a matrix in R, we can follow the below steps −First of all, create a matrix.Then, use colSums function along with nrow function to find the percentage of zeros in each column.Example 1Create the matrixLet’s create a matrix as shown below −M1

Change Color of a Particular Bar Using geom_bar in R

Nizamuddin Siddiqui
Updated on 10-Nov-2021 07:22:47

1K+ 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 −xRead More

Round Values in Columns of R Data Frame with Categorical Columns

Nizamuddin Siddiqui
Updated on 10-Nov-2021 07:19:28

149 Views

To round each value in columns 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 round each value in columns if some columns are categorical.ExampleCreate the data frameLet’s create a data frame as shown below −Level

Find Log2 of Each Value in R Data Frame Columns

Nizamuddin Siddiqui
Updated on 10-Nov-2021 07:15:50

419 Views

To find the log2 of each value 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 log2 if some columns are categorical.ExampleCreate the data frameLet’s create a data frame as shown below −Level

Add Value to Matrix Element in R

Nizamuddin Siddiqui
Updated on 10-Nov-2021 07:14:38

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]

Find Number of Zeros in Each Column of an R Data Frame

Nizamuddin Siddiqui
Updated on 10-Nov-2021 07:12:28

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

Remove Rows in Data Table Object in R Containing a Specific Number

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

312 Views

To remove rows in data.table object in R that contains a specific number, we can follow the below steps −First of all, create a data.table object.Then, use single square subsetting with apply function to remove rows that contains a specific number.ExampleCreate the data.table objectLet’s create a data.table object as shown below: −library(data.table) x

Change Sign of Even Number Rows in Single Column Matrix in R

Nizamuddin Siddiqui
Updated on 10-Nov-2021 07:06:54

196 Views

To change the sign of even number rows in R matrix, we can follow the below steps −First of all, create a matrix.Then, use vector multiplication with 1 and minus 1 to change the sign of even number rows.ExampleCreate the matrixLet’s create a matrix as shown below −M

Advertisements