Programming Articles - Page 876 of 3363

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

Nizamuddin Siddiqui
Updated on 11-Nov-2021 07:50:32

245 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

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

Nizamuddin Siddiqui
Updated on 11-Nov-2021 07:49:01

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

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

Nizamuddin Siddiqui
Updated on 11-Nov-2021 07:41:47

1K+ 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

How to separate first text value and the remaining text in an R vector?

Nizamuddin Siddiqui
Updated on 11-Nov-2021 07:21:45

134 Views

To separate first text value and the remaining text in an R, we can follow the below steps −First of all, create a vector.Then, use str_split function from stringr package to separate first text value and the remaining text.ExampleCreate the vectorLet’s create a vector as shown below −x

How to create sequential index value for binary column assigning 0 to FALSE values in R data frame?

Nizamuddin Siddiqui
Updated on 11-Nov-2021 07:19:13

195 Views

To create sequential index value for binary column assigning 0 to FALSE values in R data frame, we can follow the below steps −First of all, create a data frame with binary column.Then, use rle function along with sequence and lengths function to create sequential index column.ExampleCreate the data frameLet’s create a data frame as shown below −x

How to convert first letter of multiple string columns into capital in data.table object in R?

Nizamuddin Siddiqui
Updated on 11-Nov-2021 07:17:45

210 Views

To convert first letter of multiple string columns into capital in data.table object in R, we can follow the below steps −First of all, create a data.table object with string columns.Then, use sub function along with mutate_each function of dplyr package to convert first letter into capital in string columns.ExampleCreate the data.table objectLet’s create a data.table object as shown below −library(data.table) Names

How to generate a repeated values vector with each value in output selected randomly in R?

Nizamuddin Siddiqui
Updated on 11-Nov-2021 07:14:37

2K+ Views

To generate a repeated values vector with each value in output selected randomly in R, we can use replicate function. The replicate function will repeat the vector values up to the number of times we want and each value will be randomly selected. To understand how it works, check out the below examples.Example 1Using replicate function to create a vector of exponential random variable of size 10 −x1

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

Nizamuddin Siddiqui
Updated on 11-Nov-2021 07:12:26

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

How to convert values greater than a threshold into 1 in column of a data.table object in R?

Nizamuddin Siddiqui
Updated on 11-Nov-2021 07:09:35

193 Views

To convert values greater than a threshold into 1 in data.table object in R data frame, we can follow the below steps −First of all, create a data.table object.Then, use ifelse function to convert values greater than a threshold into 1.ExampleCreate the data.table objectLet’s create a data.table object as shown below −library(data.table) x

How to find the percent of NA’s in data.table object rows in R?

Nizamuddin Siddiqui
Updated on 11-Nov-2021 07:08:11

226 Views

To find the percent of NAs in each row of a data.table object in R, we can follow the below steps −First of all, create a data.table object.Then, use rowSums function and ncol function along with apply function to find the percent of NAs in each row of the data.table object.ExampleCreate the data.table objectLet’s create a data.table object as shown below −library(data.table) x1

Advertisements