Create Sequence Increasing by 1 in R

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

338 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

Sort Large Number of CSV Files in Ascending Order in R

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

305 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

Sort Data Table Column in Ascending Order Using R

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

302 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

Extract Closest Value to a Certain Value in Each Category in R Data Frame

Nizamuddin Siddiqui
Updated on 10-Feb-2021 10:47:38

2K+ Views

In Data Analysis, we often deal with the comparison of values and this comparison could be also done after finding the closest value to a certain value that might be threshold. For this purpose, we can use filter function of dplyr package along with abs and min function, the abs and min function are required to create the formula for finding the closest value.Consider the below data frame −Example Live DemoCategory

Perform Shapiro Test for All Columns in an R Data Frame

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

5K+ 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

Access Table Values in R

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

6K+ 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

Create Boxplot Using ggplot2 for Single Variable in R

Nizamuddin Siddiqui
Updated on 10-Feb-2021 07:18:03

4K+ Views

The important part of a boxplot is Y−axis because it helps to understand the variability in the data and hence, we can remove X−axis labels if we know the data description. To create a boxplot using ggplot2 for single variable without X−axis labels, we can use theme function and set the X−axis labels to blank as shown in the below example.Example Live DemoConsider the below data frame −y

Create Random Sample by Defining Probabilities for Each Unit in R

Nizamuddin Siddiqui
Updated on 10-Feb-2021 07:16:52

190 Views

The random sample can be created by using sample function, this random sample gives equal chance for each unit to be selected in the sample, hence it is called simple random sample. If we want to have a sample where each unit has different chance of being selected in the sample then we need to use the argument prob as shown in the below examples.Example1 Live Demox1

Remove Dot and Number at the End of String in R Vector

Nizamuddin Siddiqui
Updated on 10-Feb-2021 07:15:02

6K+ Views

To remove dot and number at the end of the string, we can use gsub function. It will search for the pattern of dot and number at the end of the string in the vector then removal of the pattern can be done by using double quotes without space. After that the vector will be passed as shown in the below examples.Example1 Live Demox1

Create Correlation Matrix Plot in R

Nizamuddin Siddiqui
Updated on 10-Feb-2021 07:14:45

381 Views

To create a correlation matrix plot, we can use ggpairs function of GGally package. For example, if we have a data frame called df that contains five columns then the correlation matrix plot can be created as ggpairs(df). A correlation matrix plot using ggpairs display correlation value as well as scatterplot and the distribution of variable on diagonal.Example Live DemoConsider the below data frame −set.seed(212) x

Advertisements