Found 2038 Articles for R Programming

How to extract statistical summary from boxplot in R?

Nizamuddin Siddiqui
Updated on 10-Feb-2021 11:12:07

7K+ Views

To extract statistical summary from boxplot we can use stats function with delta operator. For example, if we have a data frame called df that contains 5 columns then the boxplot for each column can be created by using the command boxplot(df) and if we want to extract the statistical summary from this boxplot then boxplot(df)$stats can be used.Consider the below data frame −Example Live Demodf

How to extract strings that contains a particular substring in an R vector?

Nizamuddin Siddiqui
Updated on 10-Feb-2021 11:06:39

940 Views

Suppose we have a vector that contains multiple string elements and we want to find out which string element has a particular substring. This can be done with the help of grep function. For example, if we have a vector called x that contains five string elements each of varying length then finding which of the elements has a substring say programming then it can be done by using the command grep("programming",x,fixed=TRUE)Example Live Demox1

How to put space between words that start with uppercase letter in string vector in R?

Nizamuddin Siddiqui
Updated on 10-Feb-2021 11:02:06

1K+ Views

To put space between words that start with uppercase letter in string vector in R, we can use gsub function. Since we can have uppercase letters as well as lowercase letters in the string vector hence, we need to properly specify both type of characters for creating the space between words. Check out the below examples to understand how it works.Example Live Demox1

How to truncate a string vector after a character in R?

Nizamuddin Siddiqui
Updated on 10-Feb-2021 11:00:30

940 Views

The most difficult problem in data analysis is cleaning a dirty data. Most of the times the data is available in dirty form and one such dirtiness is a string vector having unnecessary values after a particular character. Therefore, to truncate a string vector after a character we can use str_split from stringr package along with the sapply function as shown in the below examples.library(stringr)Example Live Demox1

How to display Y-axis with Euro sign using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 10-Feb-2021 10:59:12

823 Views

When we have a Euro currency column in an R data frame as a response variable then we might to display the Euro sign in the plot created by using ggplot2 package. For this purpose, we can use scales package and the scale for Y axis will be changed by using the command scale_y_continuous(labels=dollar_format(suffix="€",prefix="")) to the plot command.Consider the below data frame −Example Live Demox

How to create a sequence increasing by 1 in R?

Nizamuddin Siddiqui
Updated on 10-Feb-2021 10:56:22

240 Views

If a sequence is increasing by 1 that means for every value the total number of values increases by that much. For example, the values 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 are creating a sequence of values starting from 1 to 5. To create such type of sequences in R, we can simply use sequence function and pass the range as sequence(1:5).Example Live Demox1

How to sort a large number of csv files in ascending order in R?

Nizamuddin Siddiqui
Updated on 10-Feb-2021 10:54:20

186 Views

To sort a large number of csv files in ascending order, we can use mixedsort function from gtools package. For example, if we have a list of csv files that are randomly arranged in a vector called FILES then the files can be sorted in ascending order using the command mixedsort(sort(FILES))Example Live DemoFiles1

How to sort a column of data.table object in ascending order using column name stored in a vector in R?

Nizamuddin Siddiqui
Updated on 10-Feb-2021 10:53:02

236 Views

Sorting a column of data.table object can be done easily with column number but sorting with column name is different. If a column name is stored in a vector and we want to sort a column of data.table object in ascending order using this name then order function will be used with single and double square brackets as shown in the below examples.Loading data.table package and creating a data.table object −Examplelibrary(data.table) x1

How to perform shapiro test for all columns in an R data frame?

Nizamuddin Siddiqui
Updated on 10-Feb-2021 07:20:33

4K+ Views

The shapiro test is used to test for the normality of variables and the null hypothesis for this test is the variable is normally distributed. If we have numerical columns in an R data frame then we might to check the normality of all the variables. This can be done with the help of apply function and shapiro.test as shown in the below example.Example Live DemoConsider the below data frame −set.seed(321) x1

How to access the table values in R?

Nizamuddin Siddiqui
Updated on 10-Feb-2021 07:20:17

5K+ Views

Sometimes we want to extract table values, especially in cases when we have a big table. This helps us to understand the frequency for a particular item in the table. To access the table values, we can use single square brackets. For example, if we have a table called TABLE then the first element of the table can accessed by using TABLE[1].Example1 Live Demox1

Advertisements