Found 2038 Articles for R Programming

How to find the two factor interaction variables in an R data frame?

Nizamuddin Siddiqui
Updated on 06-Feb-2021 10:36:44

189 Views

If we have a data frame called df that contains four columns say x, y, z, and a then the two factor interaction columns will be xy, xz, xa, yz, ya, za. To find how many two factor interaction variables can be created using data frame columns, we can make use of combn function as shown in the below examples.Consider the below data frame −Example Live Demox1

How to replace 0 in list of matrices to NA in R?

Nizamuddin Siddiqui
Updated on 06-Feb-2021 10:29:55

214 Views

Sometimes missing values are being read as 0 which is not a correct way to represent them, therefore, we must convert the 0’s in the data to NA so that R can understand the difference between missing value and 0’s. For this replacement, we can use lapply function and apply the replacement for all matrices using a function as shown in the below examples.Example Live DemoM1

How to save an xtable file locally using R?

Nizamuddin Siddiqui
Updated on 06-Feb-2021 10:25:13

525 Views

To save an xtable file locally, obviously we first need to create the xtable and then use the print function for saving the file. Therefore, we require xtable package loaded in R environment and the data set that we want to save as an xtable file. In the below example, we have used iris data in base R for this purpose. Take a look at the example to understand how it works.Loading xtable package −library(xtable)Considering iris data −Example Live Demodata(iris) head(iris, 20)OutputSepal.Length Sepal.Width Petal.Length Petal.Width   Species 1  5.1           3.5         1.4   ... Read More

How to sort matrix columns independently in increasing order in R?

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

321 Views

To sort a matrix column independently in increasing order means that ordering each column of the data frame in increasing order, therefore, sorting of values in one column does not affect the sorting in other columns. This can be done with the help of apply function along with the sort function as shown in the below examples.Example Live DemoM1

How to remove Y-axis labels in R?

Nizamuddin Siddiqui
Updated on 06-Feb-2021 09:16:32

12K+ Views

When we create a plot in R, the Y-axis labels are automatically generated and if we want to remove those labels, the plot function can help us. For this purpose, we need to set ylab argument of plot function to blank as ylab="" and yaxt="n" to remove the axis title. This is a method of base R only, not with ggplot2 package.Examplex

How to remove single quote from string column in an R data frame?

Nizamuddin Siddiqui
Updated on 06-Feb-2021 09:13:50

4K+ Views

Sometimes column values in an R data frame have single quote associated with them and to perform the analysis we need to remove that quote. Therefore, to remove single quote from string column, we can use gsub function by defining the single quote and replacing it with blank(not space) as shown in the below examples.Consider the below data frame −Example Live Demox1

How to extract first value from a list in R?

Nizamuddin Siddiqui
Updated on 06-Feb-2021 09:09:46

4K+ Views

To extract first value from a list, we first need to access the element using double square brackets then the sub-element of each element will be accessed using single square brackets. For example, if we have a list called LIST containing five elements each having 10 elements then the first sub-element of the LIST will be selected by using LIST[[1]][1].Example Live DemoList1

How to put the plot title inside the plot using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 06-Feb-2021 09:08:10

615 Views

Generally, we put the plot title on top side of the plot but we can put it inside the plot as well. Of course, this will change the display of the chart but it will also get attraction of viewers. To do this, we can use the theme function of ggplot2 package where margin argument for plot title will change the position of the title.Consider the below data frame −Example Live Demox

How to find the column index in an R data frame that matches a condition?

Nizamuddin Siddiqui
Updated on 06-Feb-2021 09:05:21

851 Views

To find the column index that matches a condition, we can use apply function. This condition could be values in columns greater than something, less than something, equal to something, or any other condition for numerical variables. For example, if we want to check which columns of data df contains value in rows greater than 5 then we can use the command apply(df,1, function(x) which(x>5)).Consider the below data frame −Example Live Demox1

How to sort an R data frame column without losing row names?

Nizamuddin Siddiqui
Updated on 06-Feb-2021 09:03:03

1K+ Views

When we sort a data frame column in R, the row names are lost but we might need them. Therefore, sorting without losing row names is required and it can be done with the help of order function. For example, if we have a data frame called df that has a column x then sorting of x without losing row names can be done by using the below command −df[order(df$x),,drop=FALSE]Consider the below data frame −Example Live Demox1

Advertisements