R Programming Articles

Page 158 of 174

How to cbind vectors of different length without repetition of elements of the smaller vector in R?

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

We can join vectors by columns using cbind and it does not matter whether these vectors are of same length or not. If the vectors are of same length then all the values of both the vectors are printed but if the length of these vectors are different then the values of the smaller vector gets repeated. But we might not want to repeat the values/elements of the smaller vector and it is possible by setting the length of the smaller vector to the length of larger vector, this will create NA values in the smaller at places where the ...

Read More

How to merge data frames by row names in R?

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

Mostly, we merge the data frames by columns because column names are considered prominent in data sets but it is also possible to merge two data frames by using rows. Merging by rows is likely to result in more uncleaned data as compared to the merging by columns. This can be done with the help of merge function and its by argument.ExampleConsider the below data frames −df1

Read More

How to select of data.table based on substring of row values for a column in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 21-Aug-2020 554 Views

We often create subsets of data in R to perform calculations based on smaller objectives of a whole objective in data analysis projects. Sometimes this subsetting is conditional on strings instead of numeric values. We can also create a subset of data.table on the basis of substring of row values for a column by using grep function.ExampleConsider the below data.table object −x1

Read More

How to write a common title for par(mfrow) plots in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 21-Aug-2020 2K+ Views

We can create multiple plots using par(mfrow) on a single plot window in R. It might be possible that all of these plots are different or same as well. Irrespective of the type of plots, we can give a common title to all the plots. This can be a situation where we want to show, say, scatterplots, and histograms of few variables but the object of our analysis is fixed. To write a common title between plots in the plot window we can use mtext and adjust the title position by changing line argument.Exampleset.seed(100) x1

Read More

How to rename the factor levels of a factor variable by using mutate of dplyr package in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 21-Aug-2020 2K+ Views

We know that a factor variable has many levels but it might be possible that the factor levels we have are not in the form as needed. For example, if we want to have capital letters as a factor level but the original data has small letters of English alphabets. In this situation we can rename those factor levels by using mutate of dplyr package.ExampleConsider the below data frame −City

Read More

How to add a new column at the front of an existing R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 21-Aug-2020 3K+ Views

Generally, when we add a new column to an existing R data frame that column is added at the end of the columns but we might need it at the front. This totally depends on our ease of use, familiarity with variables, and their need. We can add a new column at the front of an existing R data frame by using cbind function.ExampleConsider the below data frame −ID

Read More

How to replace the values in a matrix with another value based on a condition in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 21-Aug-2020 3K+ Views

A matrix has only numeric values and sometimes these values are either incorrectly entered or we might want to replace some of the values in a matrix based on some conditions. For example, if we have few fives in a matrix then we might want to replace all fives to an another number which is greater than 5 or less than 5.ExampleConsider the below matrix −set.seed(123) M

Read More

How to extract a single column of an R data frame as a data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 21-Aug-2020 423 Views

Generally, we extract columns as a vector from an R data frame but sometimes we might need a column as a data frame, therefore, we can use as.data.frame to extract columns that we want to extract as a data frame with single square brackets. The purpose behind this could be merging the column with another data frame.ExampleConsider the below data frame −set.seed(9) x1

Read More

How to stop par(mfrow) to create multiple plots in one plot window and create only one plot in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 21-Aug-2020 2K+ Views

When we use par(mfrow), we define the number of plots we want to draw on the plot window and when we draw all the necessary plots then starts again with the first plot. For example, if we set par(mfrow) to (2,2) then we will have four plots on the plot window but if we want to create one plot on the plot window then it does not work, it will show a small plot on the upper left side. To deal with the problem, we can set par(mfrow) to (1,1).Examplepar(mfrow=c(2,2)) x

Read More

How to convert a data frame to a matrix if the data frame contains factor variable as strings in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 21-Aug-2020 625 Views

A matrix contains only numeric values, therefore, if we will convert a data frame that has factor variables as strings then the factor levels will be converted to numbers. These numbering is based on the first character of the factor level, for example, if the string starts with an A then it will get 1, and so on. To convert a data frame to a matrix if the data frame contains factor variable as strings, we need to read the data frame as matrix.ExampleConsider the below data frame −x1

Read More
Showing 1571–1580 of 1,740 articles
« Prev 1 156 157 158 159 160 174 Next »
Advertisements