Programming Articles - Page 1423 of 3363

How to create a sequence increasing by 1 in R?

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

371 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

332 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

325 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

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

How to access the 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

How to create a boxplot using ggplot2 for single variable without X-axis labels in R?

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

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

How to create the random sample by defining the probabilities for each unit in R?

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

230 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

How to remove dot and number at the end of the string in an R vector?

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

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

How to create correlation matrix plot in R?

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

429 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

How to convert list elements into a single string in R?

Nizamuddin Siddiqui
Updated on 10-Feb-2021 07:13:37

4K+ Views

To convert list elements into a single string, we can use paste function but firstly we would need to unlist the elements. Also, since we want to create a single string double quotes will be required at the end of the output. For example, if we have a list that contains 5 elements say 1, 2, 3, 4, 5 then conversion of these elements into a single string would be "12345".Example1 Live DemoList1

Advertisements