R Programming Articles

Page 143 of 174

How to create a histogram with dots instead of bars in base R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 04-Mar-2021 775 Views

To create a histogram with dots instead of bars, we can use the plot function in base R. The x values will be based on the sorting of the vector values and the y values will be based on the sequence of the table for the vector values. Therefore, we would be needing sorting and table with sequence. Check out the below examples to understand how it can be done.Example1> x1 plot(sort(x1),sequence(table(x1)))OutputExample2> x2 plot(sort(x2),sequence(table(x2)))Output

Read More

How to highlight a bar in base R histogram?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 04-Mar-2021 632 Views

To highlight a bar in base R histogram, we need to understand the X-axis values and pass the col argument inside hist function appropriately. We just need to put a separate value for the bar that we want to highlight and set the colouring of the rest of the bars to 0 (that is default in base R). Check out the below examples to understand how it works.Example1> x hist(x,col = c(rep(0,5),4,rep(0,5)))OutputExample2> y hist(y,col = c(rep(0,3),4,rep(0,9)))Output

Read More

How to display upper and lower quartiles through different line in a boxplot in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 04-Mar-2021 221 Views

To display the upper and lower quartiles through different line in base R boxplot, we can use abline function but we need to find the quartiles inside abline using quantile for the respective quartiles. The lines created by using abline and quantiles and the boxplot function may not coincide because of the differences in calculation. The calculation method for boxplot is explained below −The two ‘hinges’ are versions of the first and third quartile. The hinges equal the quartiles for odd n (where n x boxplot(x)OutputExample> abline(h=quantile(x,c(0.25,0.75)),col="blue")Output

Read More

How to convert numeric columns to factor using dplyr package in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Feb-2021 4K+ Views

If we have a numeric column in an R data frame and the unique number of values in the column is low that means the numerical column can be treated as a factor. Therefore, we can convert numeric columns to factor. To do this using dplyr package, we can use mutate_if function of dplyr package.Loading dplyr package and converting numerical columns in BOD data set (available in base R) to factor columns −Examplelibrary(dplyr) str(BOD) 'data.frame': 6 obs. of 2 variables: $ Time : num 1 2 3 4 5 7 $ demand: num 8.3 10.3 19 16 15.6 19.8 - ...

Read More

How to sort a column of data.table object in ascending order using column name stored in a vector in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 10-Feb-2021 332 Views

Sorting a column of data.table object can be done easily with column number but sorting with column name is different. If a column name is stored in a vector and we want to sort a column of data.table object in ascending order using this name then order function will be used with single and double square brackets as shown in the below examples.Loading data.table package and creating a data.table object −Examplelibrary(data.table) x1

Read More

How to create an integer64 vector in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 10-Feb-2021 1K+ Views

The integer64 vector contains vector values that represents signed integers with values that range from negative 9,223,372,036,854,775,808 to positive 9,223,372,036,854,775,807. To create an integer64 vector, we can use as.integer64 function of bit64 package. The difference between integer64 vector and others is that a large number of values can be stored in the vector.Examplelibrary(bit64) x1

Read More

How to fix the lower value for X-axis in base R

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 10-Feb-2021 430 Views

To fix the lower value for X-axis in base R, we can use xlim argument in plot function. Mostly, we set the lower value to zero but it is not necessary it can be something else, either less than zero or greater than zero as well. If only lower value needs to be fixed then upper value will be set by using the max function as shown in the below example.Examplex

Read More

How to create a boxplot with log of the variable in base R?

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

To create a boxplot with log of the variable in base R, we need to use log argument within the boxplot function but we need to carefully pass the Y-axis inside the function because the values of the boxplot are plotted on the Y-axis. For example, if we have a vector called x then the boxplot of log of x will be created as boxplot(x,log="y").Examplex

Read More

How to check for duplicates in data.table object in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Feb-2021 589 Views

The checking for duplicates in a data.table object can be easily done with the help of delta ($) operator that is used to access the columns and the duplicated function. For example, if a data.table object DT contains a column x then to check the duplicates in x, we can use the command duplicated(DT$x).Example1Loading data.table object and creating an object −library(data.table) set.seed(141) x

Read More

How to change the first value for each group in data.table object in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Feb-2021 775 Views

To change the first value for each group in data.table object, we can use single square brackets for accessing and changing the value to desired value. For example, if we have a data.table object called DT that contains a group column defined by Class and a numerical column defined by Response then the first value of Response for each Class can be set to say 5 by using the command DT[,Response:=c(2,Response[-]),by=Class]Consider the below data.table object −Examplelibrary(data.table) Group

Read More
Showing 1421–1430 of 1,740 articles
« Prev 1 141 142 143 144 145 174 Next »
Advertisements