Nizamuddin Siddiqui

Nizamuddin Siddiqui

1,958 Articles Published

Articles by Nizamuddin Siddiqui

Page 125 of 196

How to impute missing values by random value for a single column in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Nov-2021 804 Views

To impute missing values by random value for a single column in R, we can use impute function from Hmisc package.For example, if we have a data frame called that contains a column say C which has some missing values then we can use the below given command to fill those missing values randomly −df$C

Read More

How to create scatterplot using data frame columns in R?

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

To create scatterplot using data frame columns, we need to convert the data frame columns into a variable and the value for each column will be read in a new column against each column name. This can be done with the help of melt function in reshape2 package.After that we can use ggplot function to create the scatterplot with new data frame as shown in the below example.ExampleFollowing snippet creates a sample data frame −x1

Read More

How to multiply each element of a larger vector with a smaller vector in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Nov-2021 262 Views

To multiply each element of a larger vector with a smaller vector, we can perform outer product calculation with the help of %o% operator.For example, if we have two vectors say x and y where x is of shorter length than y then we can multiply each element of y with each element of x by using the command given below −x%o%yCheck out the below examples to understand how it works.Example 1To multiply each element of a larger vector with a smaller vector, use the code given below −x1

Read More

How to extract unique rows by categorical column of a data.table object in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Nov-2021 420 Views

If we have categorical data in a data.table object and some values are duplicate then we might want to extract unique rows from that object.To extract unique rows by categorical column of a data.table object, we can use unique function and define the columns with by argument as shown in the below examples. To understand how the extraction is done check out the below examples.Example 1Following snippet creates a data.table object −library(data.table) grp

Read More

How to extract diagonal elements of a matrix in R without using diag function?

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

The diagonal elements of a matrix occur at the position where column and row indices are same hence, we can make use of those indices to extract diagonal elements of a matrix if we do not want to use diag function.For example, if we have a matrix called M then diagonal elements of M can be extracted by using the command given below − M[row(M)==col(M)]Check out the below examples to understand how it works.Example 1Following snippet creates a matrix −M1

Read More

How to create a data frame column with equally spaced values between 0 and 1 in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Nov-2021 266 Views

To create a data frame column with equally spaced values between 0 and 1, we can use ppoints function. For example, if we want to create a data frame df with hundred equally spaced values between 0 and 1 then we can use the below mentioned command −df

Read More

How to save the str output as a string in R?

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

To save the str output as a string in R, we can use capture.output function along with str function.For example, if we have a data frame called df and we want to store the str output of df as a string then we can use the command given below −capture.output(str(df))It would be better if we save it in an object as str_df

Read More

How to find the moving standard deviation in an R data frame?

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

To find the moving standard deviation in an R data frame, we can make use of rollapply function of zoo package.For example, if we have a data frame called df and we want to find the 2 moving standard deviations then we can use the below given command −rollapply(df,width=2,FUN=sd,fill=0,align="r")Example 1Following snippet creates a sample data frame −x1

Read More

How to change the background color of base R plot region?

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

To change the background color of base R plot region, we can follow the below steps −First of all, create a plot in base R to understand the difference.Then, use par function with bg argument where you can put color name and then create the same plot.ExampleCreate the plotLet’s create a barplot in base R as shown below −x

Read More

How to remove rows in R data frame that contains a specific number?

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

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

Read More
Showing 1241–1250 of 1,958 articles
« Prev 1 123 124 125 126 127 196 Next »
Advertisements