Found 2038 Articles for R Programming

How to create a data frame column with letters of both size in R?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 13:57:25

303 Views

To create a data frame column with letters of both sizes, we can simply use the letters function and LETTERS function, the first one corresponds to lowercase letters and the latter corresponds to uppercase letters with single square brackets as shown in the below examples.Example1 Live Demodf1

How to find the index of the nearest smallest number in an R data frame column?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 13:55:20

151 Views

To find the index of the nearest smallest number in an R data frame column, we can use which function along with subsetting for the value for which we want to find the index of the nearest smallest number. To understand how it can be done check out the below examples.Example1Consider the below data frame − Live DemoID

How to convert a matrix to binary matrix in R?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 13:55:48

1K+ Views

To convert a matrix to binary matrix, we can use as.matrix function and converting the matrix to logical matrix then adding 0 to the values greater than 0. For example, if we have a matrix called M then it can be converted to a binary matrix using the command −as.matrix((M

How to create a date vector with randomization in R?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 13:49:43

155 Views

To create a date vector with randomization, we can use sample function but the dates need to be read with as.Date function. For example, if we have 2 dates say 2021-01-01 and 2021-02-02 then a vector with randomization of these three dates can be created by using the command −sample(c(as.Date("2021-01-01"),as.Date("2021-02-02")),100,replace=TRUE)Example1 Live DemoDate1

How to multiply large numbers with all digits in the output in R?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 13:48:11

163 Views

To multiply large numbers with all digits in the output, we can use mul.bigz function of gmp package. For example, if we have two vectors say x and y each containing numbers of large size then the multiplication of these numbers that will return all the digits of the multiplication can be done by using the command mul.bigz(x,y).Example1Loading gmp package and multiplying vectors containing large number values −library(gmp) x1

How to show values in boxplot in R?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 13:46:44

4K+ Views

The main values in a boxplot are minimum, first quartile, median, third quartile, and the maximum, and this group of values is also called five-number summary. Therefore, if we want to show values in boxplot then we can use text function and provide the five-number summary and labels with fivenum function as shown in the below examples.Example1x

How to create a horizontal boxplot in base R?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 13:46:26

2K+ Views

To create a horizontal boxplot in base R, we can set the horizontal argument inside boxplot function to TRUE. For example, if we have a vector called x then the horizontal histogram of that vector can be created by using the command boxplot(x,horizontal=TRUE).Example1x

How to set the text position using geom_text in R?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 13:43:16

355 Views

To set the text position using geom_text, we can use the value for the X-axis and Y-axis with appropriate positions. We need to make sure that the values we set for both the axes do not lie within the data otherwise the text will be printed on the plot we want to draw and it will become less attractiveExampleConsider the below data frame − Live Demox

How to create normal random variables with specific correlation between them in R?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 13:44:07

555 Views

To create normal random variables with specific correlation between them, we can use mvrnorm function of MASS package. For example, if we want to create two variables of size 10 with means equal to 2 and 4 and standard deviation of 0.5 then it can be done by using the command −mvrnorm(10,mu=c(2,4),Sigma=matrix(c(1,0.5,0.5,1),ncol=2),empirical=TRUE)Example1library(MASS) X

How to find the percentage for frequencies stored in a vector with two decimal places in R?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 13:41:25

550 Views

To find the percentage for frequencies stored in a vector with two decimal places can be done with the help of sum function and round function. For example, if we have a vector of frequencies say x then the percentage of these frequencies can be found by using the command round((x/sum(x))*100,2). Check out the below examples to understand how it works.Example1 Live DemoFrequency1

Advertisements