Found 2038 Articles for R Programming

How to standardize matrix elements in R?

Nizamuddin Siddiqui
Updated on 14-Oct-2020 12:00:25

987 Views

The standardization is the process of converting a value to another value so that the mean of the set of values from which the original value was taken becomes zero and the standard deviation becomes one. To standardize matrix elements, we can use data.Normalization function of clusterSim package but we need to make sure that we set the type argument to n1 because that corresponds to standardization with mean zero and standard deviation 1.Loading clusterSim package −library("clusterSim")Example Live DemoM1

How to change a data frame with comma separated values in columns to multiple columns in R?

Nizamuddin Siddiqui
Updated on 14-Oct-2020 11:44:18

739 Views

Mostly, we need to import the data from an outside source in R environment for analysis and these data can be recorded as comma separated values that represent rows. If we want to create the columns for the comma separated values then cSplit function of splitstackshape package can be used. In the below example, we have created a data frame with comma separated values then splitting those values as single value in each column.Consider the below data frame −Example Live Demodf=data.frame(x=apply(matrix(rpois(200, 10), 20, 10), 1, paste, collapse=", ")) dfoutputx 1 8, 12, 7, 12, 10, 8, 11, 6, 8, 7 2 ... Read More

How to create a boxplot with outliers of larger size in R?

Nizamuddin Siddiqui
Updated on 14-Oct-2020 11:32:18

3K+ Views

When we create a boxplot for a column of an R data frame that contains outlying values, the points for those values are smaller in size by default. If we want to increase the size for those outlying points then outlier.size argument can be used inside geom_boxplot function of ggplto2 package.Consider the below data frame −Example Live Demoset.seed(1231) x

How to add a mathematical expression in axis label in a plot created by using plot function in R?

Nizamuddin Siddiqui
Updated on 14-Oct-2020 11:30:02

421 Views

When we create a plot using plot function in R, the axes titles are either chosen by R automatically based on the vectors passed through the function or we can use ylab or xlab for particular axes. To add a mathematical expression in an axis label, we can use title function with expression function to define the mathematical expression.Consider the below vectors and create scatterplot between the two −Exampleset.seed(111) x

How to create a black and white word cloud in R?

Nizamuddin Siddiqui
Updated on 14-Oct-2020 11:28:18

131 Views

According to Google, a word cloud is an image composed of words used in a particular text or subject, in which the size of each word indicates its frequency or importance. In R, we can create word cloud by using wordcloud function of wordcloud package. So, we have the same name of the function as the package, thus we should not get confused by it.Loading wordcloud package and creating a wordcloud −library("wordcloud") x

How to convert the repeated elements of strings in a vector to unique elements in R?

Nizamuddin Siddiqui
Updated on 14-Oct-2020 11:25:20

115 Views

When we have repeated elements of strings and we want to use them as factor levels then it is okay but if we want to treat them individually then it is better to make each value a unique element. To do this, we can use make.unique function. For example, if we have a vector x that contains repeated string values then to make them unique, we can use make.unique(x).Example Live Demox1

How to create a stacked bar plot with vertical bars in R using ggplot2?

Nizamuddin Siddiqui
Updated on 14-Oct-2020 11:17:31

413 Views

Traditionally, the stacked bar plot has multiple bars for each level of categories lying upon each other. But this visual can be changed by creating vertical bars for each level of categories, this will help us to read the stacked bar easily as compared to traditional stacked bar plot because people have a habit to read vertical bars.Consider the below data frame −Example Live Demoset.seed(999) Class

How to subtract one data frame from another in R?

Nizamuddin Siddiqui
Updated on 14-Oct-2020 11:12:46

9K+ Views

If we have two data frames with same number of columns of same data type and equal number of rows then we might want to find the difference between the corresponding values of the data frames. To do this, we simply need to use minus sign. For example, if we have data-frames df1 and df2 then the subtraction can be found as df1-df2.Consider the below data frame −Example Live Demox1

How to create a list of an unordered combination of elements in a string vector in R?

Nizamuddin Siddiqui
Updated on 14-Oct-2020 10:05:56

116 Views

An unordered combination of elements means that the combination of the values in a way that does not make any particular arrangement. For example, if we have three values one, two, and three then they can be arranged in the following way which is unordered −"one" "two" "three" "one" "two" "one" three" "two" "three" "one" "two" "three"Example Live Demox

How to check whether a year or a vector of years is leap year or not in R?

Nizamuddin Siddiqui
Updated on 14-Oct-2020 10:04:26

321 Views

Almost everyone knows that a leap has 366 instead of 365 days and it occurs once in four years. If we want to check whether a particular year is a leap year or in a range of years which years correspond to leap year then we can use leap_year function of leap year. The length function can be used with the year value and if the output is 1 then it will be a leap year otherwise the output will be 0 which refers to the non-leap year.Loading lubridate package −Examplelibrary("lubridate") year1

Advertisements