R Programming Articles

Page 154 of 174

How to extract a string that lies between two strings in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 24-Aug-2020 620 Views

If we have a long string then we might want to extract a part of string that lies between two strings. For example, if we have a string “E-learning changing the education system in the world” and we want to extract the string “the education system” brave then we must be very careful about passing the strings in string function, you get to know this in examples. The extraction is not difficult with gsub function but we have to make sure that we are using the correct syntax, otherwise, the result will become obnoxious.Examplesx1

Read More

How to find the number of runs in a sequence in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 24-Aug-2020 681 Views

Sometimes data is recorded as a sequence of numerical values or strings and we might to find the frequency for each of the sequences. This helps us to check the variation in the runs but we must make sure the total frequency is equal to the total number values, otherwise our calculation of frequency would be incorrect. To find the number of runs, we can use rle function in R that stands for Run Length Encoding.Examplesx1

Read More

How to add title at the top of multi-plots created by using gridExtra in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 24-Aug-2020 8K+ Views

The gridExtra package works as an alternative of par(mfrow) with ggplot2, therefore, we can create multiple plots using ggplot2 and gridExtra on a single plot window. Now, if we want to give a title to all of the plots or we can say if want to give a main title to multi-plots, the top argument will be used to make the title lie on the top of the title. Similarly, we can use bottom, left, and right on the basis of our requirement but we would also need grid package for this purpose.ExampleConsider the below data frame −set.seed(123) x1

Read More

How to get the US states name abbreviation in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 24-Aug-2020 541 Views

There are fifty states in United States, few of them have short names but most of the states have a lengthy name. Therefore, if we are dealing with data that has states name of United States then it will be a little complicated to access the states by using their name, hence it is preferred to use abbreviation. We can get the state name abbreviation with the help of state.abb function.Examplesstate.abb[which(state.name=="New York")] [1] "NY" state.abb[which(state.name=="California")] [1] "CA" state.abb[which(state.name=="Texas")] [1] "TX" state.abb[which(state.name=="Florida")] [1] "FL" state.abb[which(state.name=="Washington")] [1] "WA" state.abb[which(state.name=="Michigan")] [1] "MI" state.abb[which(state.name=="New Jersey")] [1] "NJ" state.abb[which(state.name=="Arizona")] [1] "AZ" state.abb[which(state.name=="Pennsylvania")] [1] "PA" state.abb[which(state.name=="Alaska")] ...

Read More

What are the restrictions on creating a vector in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 24-Aug-2020 215 Views

There are four main restrictions on creating a vector in R. We must remember these restrictions while creating any type of vector −A vector name cannot have % sign.A vector name cannot start with a number.A vector can start with a dot but it should not have a number after it.A vector cannot start with underscore.ExamplesVectors with % sign −x1%

Read More

How to find the correlation matrix by considering only numerical columns in an R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 24-Aug-2020 896 Views

While we calculate correlation matrix for a data frame, all the columns must be numerical, if that is not the case then we get an error Error in cor(“data_frame_name”) : 'x' must be numeric. To solve this problem, either we can find the correlations among variables one by one or use apply function.ExampleConsider the below data frame −set.seed(99) x1

Read More

How to change the title size of a graph using ggplot2 in R?

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

The size of a graph title mattes a lot for the visibility because it is the first thing people look at after plot area. Its size must not be very large nor very small but is should be different from the axis titles and axes labels so that there exists a clarity in the graph. This can be done by using theme function.ExampleConsider the below data frame −set.seed(1) x

Read More

How to combine the levels of a factor variable in an R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 24-Aug-2020 4K+ Views

An R data frame can have numeric as well as factor variables. It has been seen that, factor levels in the raw data are recorded as synonyms even in different language versions but it is rare. For example, a factor variable can have hot and cold as levels but it is possible that hot is recorded as garam by a Hindi native speaker because garam is Hindi form of hot. Therefore, we need to combine the similar levels into one so that we do not have unnecessary factor levels for a variable.ExampleConsider the below data frame −set.seed(109) x1

Read More

How to find the difference in number of days between two date columns of an R data frame?

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

When dealing with date data, we often want to find the difference between dates if the data contains two or more date values. Same thing can be done for the two columns of an R data frame that contains dates but first we need to read those date columns in date format in case they are not recorded as date in R. The finding of difference in number of days can be done by using difftime function.ExampleConsider the below data −date1

Read More

How to transform numbers between 1 and 12 to abbreviated month in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 24-Aug-2020 415 Views

Sometimes date vector for months is recorded in numeric form and it becomes difficult to treat or visualize it as a date vector. For example, if a vector for months has numbers 1 that represents January, 2 that represents February and so on then it is considered as a numeric vector instead of the vector to represent the month. To transform such type of vectors into abbreviated month as Jan, Feb, etc. we can use month.abb function.ExamplesMonth1

Read More
Showing 1531–1540 of 1,740 articles
« Prev 1 152 153 154 155 156 174 Next »
Advertisements