Found 33676 Articles for Programming

How to add named vectors of different sizes based on names in R?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 12:54:57

236 Views

If we have named vectors but the names come from same family then they cannot be added to each other direct to get the sum of values based on names. To do this, we need to use tapply function. For example, if we have three vectors defined as x, y, and z then firstly they need to be combined as V

How to count special characters in an R vector?

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

975 Views

Special characters are generally treated as string values and they can be counted with the help of str_count function of stringr package. For example, if we have a vector x that contains $, #, %, ^, &, *, @, ! or any other special character then we can use str_count(x,"\$") to count the number of $ in the vector x, this can be done for all special characters separately.Example1x1

How to convert a vector into data frame in R?

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

9K+ Views

If we want to convert an object into a data frame then as.data.frame function can be used, we just need to read the object that we want to convert with as.data.frame. For example, if we have a vector x then it can be converted to data frame by using as.data.frame(x) and this can be done for a matrix as well.Example1 Live Demox

How to create a regression model in R with interaction between all combinations of two variables?

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

481 Views

The easiest way to create a regression model with interactions is inputting the variables with multiplication sign that is * but this will create many other combinations that are of higher order. If we want to create the interaction of two variables combinations then power operator can be used as shown in the below examples.Example1 Live Demox1

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

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

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

789 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

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

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

237 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

Advertisements