Server Side Programming Articles - Page 1509 of 2650

How to concatenate two or more vectors in R?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 13:11:55

5K+ Views

The concatenation of vectors can be done by using combination function c. For example, if we have three vectors x, y, z then the concatenation of these vectors can be done as c(x,y,z). Also, we can concatenate different types of vectors at the same time using the same same function.Example1 Live Demoset.seed(999) x1

How to find total of an integer column based on two different character columns in R?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 13:09:45

125 Views

The calculation of total for integer column based on two different character columns simply means that we need to create a contingency table for the available data. For this purpose, we can use with and tapply function. For example, if we have a data frame df that contains two categorical columns defined as gender and ethnicity and an integer column defined as Package then the contingency table can be created as:with(df,tapply(Package,list(gender,ethnicity),sum))ExampleConsider the below data frame − Live Demoset.seed(777) Class

How to convert a list of lists into a single list in R?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 13:02:35

2K+ Views

A list can contain multiple lists of different type as well as similar type elements such as data frames, vectors, matrices etcetera but accessing those elements become a little difficult task. Therefore, it is better to convert a list that contain multiple lists into a single list and it can be done using unlist function.ExampleConsider the below vectors −set.seed(871) x1

How to calculate two period moving average for vector elements in R?

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

203 Views

If we want to find the two−period moving average easily then it cannot be done in base R. We need to use rollmean function of zoo package that solves this problem in a single line of code. For example, if we have a vector x that contains hundred from starting from 1 to 100 then the two−period moving for x can be found by using rollmean(x,2)Loading zoo package −library(zoo)Examples Live Demox1

How to italicize boxplot label in R using ggplot2?

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

2K+ Views

Like every other tool for statistical analysis R does not display the labels of a boxplot in italics, thus if we want to do this, we need to do it manually. In ggplot2, we have a function scale_x_discrete that can be used to change the default font to italic using expression function.Example Live DemoConsider the below data frame −set.seed(121) x

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

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

249 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

989 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

496 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

Advertisements