Scale Columns in Data Table Object in R

Nizamuddin Siddiqui
Updated on 11-Nov-2021 05:49:08

468 Views

To scale some columns in data.table object in R, we can follow the below steps −First of all, create a data.table object.hen, subset the columns with single square brackets and use lapply, list and scale function to scale those columns.ExampleCreate the data.table objectLet’s create a data.table object as shown below −library(data.table) var1

Unsplit a Split in R Data Frame

Nizamuddin Siddiqui
Updated on 11-Nov-2021 05:44:11

932 Views

To unsplit a split in R data frame, we can follow the below steps −First of all, create a data frame.Then, use split function to split the data frame.After that, use do.call function along with rbind function unsplit the data frame.ExampleCreate the data frameLet’s create a data frame as shown below −x

Reorder Row Indices in an R Data Frame

Nizamuddin Siddiqui
Updated on 11-Nov-2021 05:43:37

913 Views

If we have a data frame where row indices are not in correct sequence then we can set them to NULL to get the correct sequence of row numbers starting from 1.For example, if we have a data frame called df which has row ordered in an inappropriate way then we can reorder the row indices in correct order by using the command given below −row.names(df)

Print Exact Values of All Elements in a Column of an R Data Frame

Nizamuddin Siddiqui
Updated on 11-Nov-2021 05:38:53

2K+ Views

To print the exact values of all elements in a column of an R data frame, we can follow the below stops −First of all, create a data frame.Then, use options function and provide the required number of digits for printing.ExampleCreate the data frameLet’s create a data frame as shown below −x

Change Color Code of Corrplot in R

Nizamuddin Siddiqui
Updated on 11-Nov-2021 05:37:52

7K+ Views

To change the color code of corrplot, we can use colorRampPalette function inside corrplot function. We can provide different colors in colorRampPalette that we want to display in the corrplot.Check out the below given example to understand how it can be done.ExampleFollowing snippet creates a sample data frame −x

Convert Values Greater Than Threshold to 1 in Matrix Column in R

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

465 Views

To convert values greater than a threshold into 1 in matrix in R data frame, we can follow the below steps −First of all, create a matrix.Then, use ifelse function to convert values greater than a threshold into 1.ExampleCreate the matrixLet’s create a matrix as shown below −M

Check If Two Vectors Are Exactly Same in R

Nizamuddin Siddiqui
Updated on 11-Nov-2021 05:33:24

7K+ Views

To check if two vectors are exactly same, we can use identical function.For example, if we have two vectors say x and y then we can find whether both of them are exactly same or not by using the command given below −identical(x,y)Check out the below examples to understand the result of identical function for two vectors.Example 1To check if two vectors are exactly same, use the command given below −x1

Change Outline Color for Histogram Bars Using ggplot2 in R

Nizamuddin Siddiqui
Updated on 11-Nov-2021 05:32:56

4K+ Views

To change the outlines color of histogram bars using ggplot2, we can use col argument inside geom_histogram function of ggplot2 package.For Example, if we have a data frame called df that contains a column say X then we can create the histogram of X with different outline color of bars using the below command −ggplot(df,aes(X))+geom_histogram(bins=30,col=I("red"))ExampleFollowing snippet creates a sample data frame −x

Find ID-wise Frequency in an R Data Frame

Nizamuddin Siddiqui
Updated on 11-Nov-2021 05:32:22

562 Views

To find the ID wise frequency in an R data frame, we can use summarise function of dplyr package after defining the ID with group_by function, also the column for which we want to find the frequency will be placed inside group_by function.Check out the below examples to understand how it can be done.Example 1Following snippet creates a sample data frame −ID

Find Standard Deviation for Observations in R Data Frame Column

Nizamuddin Siddiqui
Updated on 11-Nov-2021 05:30:47

396 Views

To find the standard deviation for every n number of observations in an R data frame, we can use rollapply function of zoo package.For Example, if we have a data frame called df that contains a column say X containing 100 values then we can create a column with standard deviation of every 10 values by using the below command −df$SD_10

Advertisements