R Programming Articles

Page 167 of 174

How to count the number of rows for a combination of categorical variables in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Aug-2020 544 Views

When we have two categorical variables then each of them is likely to have different number of rows for the other variable. This helps us to understand the combinatorial values of those two categorical variables. We can find such type of rows using count function of dplyr package.ExampleConsider the CO2 data in base R −> head(CO2, 20) > head(CO2, 20)       Plant    Type    Treatment    conc     uptake 1     Qn1     Quebec   nonchilled     95      16.0 2     Qn1     Quebec   nonchilled    175   ...

Read More

How to randomize an already created vector in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Aug-2020 977 Views

Some vectors are randomly created and some are not randomly created in R but we can do randomization for both of these types of vectors. Randomization ensures unbiasedness therefore it is necessary especially when the vector is created with an objective that tends to change the result of the analysis. The randomization in R can be simply done with the help of sample function.Randomization of vectors that are not randomly created −> x1 x1 [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ...

Read More

How to replace NA’s to a value of selected columns in an R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Aug-2020 697 Views

In data analysis, finding some NA values in a data frame is very common but all the NA values do not create problems if the column that contain NA values is not useful for the analysis. We can replace all NA values to 0 or to any other for the columns that are useful.ExampleConsider the below data frame −> set.seed(99) > x1 x2 x3 x4 x5 df df   x1   x2   x3   x4    x5 1  NA   NA   25    NA 2  5     2   24    f    2 3  NA   ...

Read More

How to count the number of words in a string in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Aug-2020 588 Views

The number of words in a sentence could be used for text analysis, therefore, we are required to count them. This can be for a single sentence or for multiple sentences. We can find the number of words in a sentence or in multiple sentences using strsplit with sapply.ExampleConsider the below sentences read as vectors −> x1 x1 [1] "Data Science is actually the Statistical analysis" > sapply(strsplit(x1, " "), length) [1] 7 > x2 x2 [1] "China faced trouble even after controlling COVID-19" > sapply(strsplit(x2, " "), length) [1] 7 > x3 x3 [1] "Corona virus has changed everything ...

Read More

How to change plot area margins using ggplot2 in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Aug-2020 1K+ Views

While creating plots using ggplot2, the plot area is of square shape but we can change our plot area by setting plot.margin in theme function. This is helpful when we want to decrease the plot area and also when the data points are less.ExampleConsider the below data frame −> set.seed(1) > x y df library(ggplot2)Creating the scatterplot without changing the plot area margins −> ggplot(df,aes(x,y))+ + geom_point()> ggplot(df,aes(x,y))+ + geom_point()+ + theme(plot.margin = unit(c(1,1,1,1), "cm"))> ggplot(df,aes(x,y))+ + geom_point()+ + theme(plot.margin = unit(c(2,2,2,2), "cm"))

Read More

How to select multiple elements of a list in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Aug-2020 1K+ Views

Generally, a list in R contains a large number of elements and each element can be of different type which is a great thing about lists. Since we can store type of data as a list element therefore storage and selection to different type of data becomes easier. And we can also select single or multiple elements of the list at a time. This can be done with the help of single square brackets.ExampleConsider the below list −> list_data list_data [[1]] [1] "India" [[2]] [1] "China" [[3]] [1] 21 32 11 [[4]] [1] "a" "b" "c" "d" "e" [[5]] ...

Read More

How to split a big data frame into smaller ones in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 10-Aug-2020 1K+ Views

Dealing with big data frames is not an easy task therefore we might want to split that into some smaller data frames. These smaller data frames can be extracted from the big one based on some criteria such as for levels of a factor variable or with some other conditions. This can be done by using split function.ExampleConsider the below data frame −> set.seed(1) > Grades Age Category df head(df, 20) Grades Age Category 1 A 25 6 2 B 4 ...

Read More

How to add a column between columns or after last column in an R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 10-Aug-2020 274 Views

Since no one is perfect, people might forget to add all columns that are necessary for the analysis but this problem can be solved. If a column is missing in our data frame and we came to know about it later then it can be added easily with the help of reordering the columns.ExampleConsider the below data frame −> x1 x2 x3 df df x1 x2 x3 1 1 a 1 2 2 b 2 3 3 c 1 4 4 d 2 5 5 e 1 ...

Read More

How to delete a row from an R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 10-Aug-2020 588 Views

While doing the analysis, we might come across with data that is not required and we want to delete it. This data can be a whole row or multiple rows. For example, if a row contains values greater than, less than or equal to a certain threshold then it might not be needed, therefore we can delete it. In R, we achieve this with the help of subsetting through single square brackets.ExampleConsider the below data frame −> set.seed(99) > x1 x2 x3 x4 x5 df df ...

Read More

How to replace missing values recorded with blank spaces in R with NA or any other value?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 10-Aug-2020 1K+ Views

Sometimes when we read data in R, the missing values are recorded as blank spaces and it is difficult to replace them with any value. The reason behind this is we need to know how many spaces we have used in place of missing values. If we know that then assigning any value becomes easy.ExampleConsider the below data frame of vectors x and y.> x y df df x y 1 1 2 3 2 3 2 4 1 43 5 2 2 6 3 7 2 3 ...

Read More
Showing 1661–1670 of 1,740 articles
« Prev 1 165 166 167 168 169 174 Next »
Advertisements