Found 2038 Articles for R Programming

What is the difference between ordered factors and unordered factors in R?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 12:38:37

773 Views

To understand the difference ordered factors and unordered factors, it is better to understand them by creating the factor vectors by using ordered argument with TRUE and FALSE options. For example, if we have a vector x then it can be ordered or unordered as factor(x,ordered=TRUE) and factor(x,ordered=FALSE).Example1 Live Demox1

How to combine array of vectors in R?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 12:36:46

488 Views

To combine array of vectors we can use rbind function. For example, if we have multiple vectors say x, y, z of same size or different sizes but the total number of elements are even then we can use rbind(x,y,z) to combine those vectors. Check out the examples to understand how it works.Example1 Live Demox1

How to convert factor levels into character in R?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 12:28:19

5K+ Views

To convert factor levels into character then we can use as.character function by accessing the column of the data frame that contain factor values. For example, if we have a data frame df which contains a factor column named as Gender then this column can be converted into character column as as.character(df$Gender).Example Live DemoConsider the below data frame −set.seed(121) x1

How to create boxplot in base R without axes labels?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 12:25:48

1K+ Views

The boxplot can be created by using boxplot function in base R but the Y−axis labels are generated based on the vector we pass through the function. If we want to remove the axis labels then axes = FALSE argument can be used. For example, if we have a vector x then the boxplot for x without axes labels can be created by using boxplot(x,axes=FALSE).Example Live DemoConsider the below vector x and creating boxplot −set.seed(777) x

How to find p-value for correlation coefficient in R?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 12:22:12

10K+ Views

The t test is used to find the p−value for the correlation coefficient and on the basis of that we decide whether there exists a statistically significant relationship between two variables or not. In R, we can perform this test by using function cor.test. For example, if we have a vector x and y then we can find the p−value using cor.test(x,y).Example1 Live Demoset.seed(444) x1

How to create boxplot for multiple categories with long names in base R?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 12:20:45

169 Views

In base R, we use boxplot function to create the boxplots but if we have categorical vector and the corresponding numerical vector then the boxplot can be easily created. For this purpose, we should save those vectors in a data frame and use the $ operator and las = 2 argument to create the boxplot as shown in the below example.ExampleConsider the below vectors:Countries Rate

How to generate a power sequence of two in R?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 12:15:49

6K+ Views

In R, for the calculation of power we can simply use power operator ^ and this will be also used in case of generating a power sequence. For example, if we want to generate a power sequence from 1 to 5 of 2 then we can use the code 2^(1:5) this will result 2 4 8 16 32.Example Live Demo2^(0:2)Output[1] 1 2 4Example Live Demo2^(0:10)Output[1] 1 2 4 8 16 32 64 128 256 512 1024 Example Live Demo2^(0:50)Output[1] 1.000000e+00 2.000000e+00 4.000000e+00 8.000000e+00 1.600000e+01 [6] 3.200000e+01 6.400000e+01 1.280000e+02 2.560000e+02 5.120000e+02 [11] 1.024000e+03 2.048000e+03 4.096000e+03 8.192000e+03 1.638400e+04 [16] 3.276800e+04 6.553600e+04 1.310720e+05 2.621440e+05 5.242880e+05 [21] ... Read More

How to convert more than one column in R data frame to from integer to numeric in a single line code?

Nizamuddin Siddiqui
Updated on 08-Sep-2023 23:06:52

42K+ Views

To convert columns of an R data frame from integer to numeric we can use lapply() function. For example, if we have a data frame df that contains all integer columns then we can use the code lapply(df,as.numeric) to convert all of the columns data type into numeric data type.Example1Consider the below data frame − Live Demoset.seed(871) x1

How to create a point chart with empty points using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 12:09:22

2K+ Views

The point chart can be created by using geom_point function and if we want to create a point chart for single vector then we should pass the vector in both the places inside aes function. Also, by default the points are complete black circles and if we want to change the points to empty points then shape argument can be used.ExampleConsider the below data frame − Live Demoset.seed(171) x

How to generate random samples rounded to 4 decimal places in R?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 09:49:47

809 Views

Random samples can be generated in many ways such as using discrete and continuous distributions, using integer vectors, using numerical vectors, using character vectors and/or factor vectors, also with columns of a data set. If we have the sample that is continuous in nature then the values are likely to contain many values after decimal point and we can limit those values to 4 or use any other limit using round function.Example Live Demox1

Advertisements