Nizamuddin Siddiqui

Nizamuddin Siddiqui

1,958 Articles Published

Articles by Nizamuddin Siddiqui

Page 188 of 196

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 703 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 593 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 281 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 593 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

How to find the correlation matrix in R using all variables of a data frame?

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

Correlation matrix helps us to determine the direction and strength of linear relationship among multiple variables at a time. Therefore, it becomes easy to decide which variables should be used in the linear model and which ones could be dropped. We can find the correlation matrix by simply using cor function with data frame name.ExampleConsider the below data frame of continuous variable −> set.seed(9) > x1 x2 x3 x4 x5 df df x1 x2 ...

Read More

How to change the order of columns in an R data frame?

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

Ordering columns might be required when we want to manipulate the data. Manipulation can have several reasons such as cross verification, visualisation, etc. We should also be careful when we change anything in the original data because that might affect our processing. To change the order of columns we can use the single square brackets.ExampleConsider the below data frame −> set.seed(1) > Class Grade Score df df   Class Grade Score 1   a     A     68 2   b     B     39 3   c     C      1 4   ...

Read More
Showing 1871–1880 of 1,958 articles
« Prev 1 186 187 188 189 190 196 Next »
Advertisements