Nizamuddin Siddiqui

Nizamuddin Siddiqui

1,958 Articles Published

Articles by Nizamuddin Siddiqui

Page 157 of 196

How to concatenate numerical vectors and a string to return a string in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 07-Sep-2020 2K+ Views

In general, the concatenation of numerical vectors and string results in a vector of strings in R. For example, if we want to concatenate 1, 2, 3 with Tutorialspoint using paste function then it will result in a vector as: "Tutorialspoint 1" "Tutorialspoint 2" "Tutorialspoint 3". But if we want it to return as "Tutorialspoint 1 2 3" then we need to use collapse argument with paste function.Example1> x1Output[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"> y1Output[1] 1 9 6 3 8 1 8 5 9 1 2 8 3 8 4 5 10 1 6 3 ...

Read More

How to create an image of matrix of pixels in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 07-Sep-2020 1K+ Views

A matrix can be converted into a pixel’s image of matrix. It is defined as the area of the matrix which contains pixels with equal or different sizes of left, right, bottom and upper.We can create this by using image function and its argument useRaster with the matrix data.Example Live Demo> M par(mar=c(5,5,5,5)) > image(M,useRaster=TRUE,axes=FALSE)Output> par(mar=c(10,10,10,10)) > image(M,useRaster=TRUE,axes=FALSE)Output> par(mar=c(2,2,2,2)) > image(M,useRaster=TRUE,axes=FALSE)OutputPixel matrix with grey color −> image(M,axes=FALSE,col=grey(seq(0,1,length=180)))Output

Read More

How to show all X-axis labels in a bar graph created by using barplot function in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 07-Sep-2020 737 Views

In base R, the barplot function easily creates a barplot but if the number of bars is large or we can say that if the categories we have for X-axis are large then some of the X-axis labels are not shown in the plot. Therefore, if we want them in the plot then we need to use las and cex.names.ExampleConsider the below data and bar graph − Live Demo> x names(x) barplot(x)OutputShowing all the X-axis labels −> barplot(x,las=2,cex.names=0.5)Output

Read More

How to generate a sequence of a date in each month for a fixed number of months using R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 04-Sep-2020 969 Views

Every month have common dates except few such as February do not have 30 or 31 and even 29 in some years and there are months that contain 30 days while some contains 31 days. Therefore, finding a date say the first date, a middle date, or a last date is not an easy task but it can be done with the help of seq function in base R.Examples Live Demo> seq(as.Date("2020-01-01"), length=12, by="1 month")Output[1] "2020-01-01" "2020-02-01" "2020-03-01" "2020-04-01" "2020-05-01" [6] "2020-06-01" "2020-07-01" "2020-08-01" "2020-09-01" "2020-10-01" [11] "2020-11-01" "2020-12-01"Example Live Demo> seq(as.Date("2020-01-01"), length=36, by="1 month") Output[1] "2020-01-01" "2020-02-01" "2020-03-01" "2020-04-01" "2020-05-01" [6] ...

Read More

How to match two string vectors if the strings case is different in both the vectors in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 04-Sep-2020 227 Views

We know that, R is a case sensitive programming language, hence matching strings of different case is not simple. For example, if a vector contains tutorialspoint and the other contains TUTORIALSPOINT then to check whether the strings match or not, we cannot use match function directly. To do this, we have to convert the lowercase string to uppercase or uppercase to lowercase with the match function.Examples Live Demo> x1 x1Output[1] "z" "v" "r" "y" "z" "l" "v" "t" "f" "p" "p" "z" "e" "b" "a" "o" "m" "d" [19] "e" "l" "y" "y" "u" "u" "w" "b" "a" "j" "n" "v" ...

Read More

Why the t.test returns a smallest p-value of 2.2e – 16 in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 04-Sep-2020 3K+ Views

When we perform a t test in R and the difference between two groups is very large then the p-value of the test is printed as 2.2e – 16 which is a printing behaviour of R for hypothesis testing procedures. The actual p-value can be extracted by using the t test function as t.test(“Var1”, ”Var2”, var.equal=FALSE)$p.value. This p-value is not likely to be the same as 2.2e – 16.Example1 Live Demo> x1 y1 t.test(x1, y1, var.equal=FALSE)Output   Welch Two Sample t-test data: x1 and y1 t = -3617.2, df = 10098, p-value < 2.2e-16 alternative hypothesis: true difference in means is not ...

Read More

How to concatenate column values and create a new column in an R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 04-Sep-2020 1K+ Views

Sometimes we want to combine column values of two columns to create a new column. This is mostly used when we have a unique column that maybe combined with a numerical or any other type of column. Also, we can do this by separating the column values that is going to be created with difference characters. And it can be done with the help of apply function.ExampleConsider the below data frame − Live Demo> ID Country df1 df1Output ID Country 1 1 UK 2 2 UK 3 3 India 4 4 USA 5 5 USA 6 6 UK 7 7 Nepal 8 ...

Read More

How to draw gridlines in a graph with abline function in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 04-Sep-2020 226 Views

Gridlines are the horizontal and vertical dotted lines, and they help to organize the chart so that the values on the labels becomes better readable to viewers. This is helpful specially in situations where we plot a large number of data points. A graph drawn by plot function can have gridlines by defining the vertical and horizontal lines using abline.ExampleConsider the below data and scatterplot − Live Demo> x y plot(x,y)OutputAdding gridlines using abline function −> abline(h=seq(0,5,0.5),lty=5) > abline(v=seq(-2,2,0.5),lty=5)Output

Read More

How to select rows based on range of values of a column in an R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 04-Sep-2020 2K+ Views

Extraction or selection of data can be done in many ways such as based on an individual value, range of values, etc. This is mostly required when we want to either compare the subsets of the data set or use the subset for analysis. The selection of rows based on range of value may be done for testing as well. We can do this by subset function.ExampleConsider the below data frame − Live Demo> x1 x2 x3 df dfOutput x1 x2 x3 1 3 2 6 2 3 4 9 3 4 4 12 4 4 8 12 5 3 5 11 ...

Read More

How to change the color and size of the axes labels of a plot created by using plot function in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 04-Sep-2020 364 Views

The default size of axes labels created by using plot function does not seem to be large enough and also it does not look appealing. Therefore, we might want to change their size and color because the appearance of a plot matters a lot. This can be done by setting colors with col.lab and size with cex.lab.Example Live Demo> x y plot(x,y)OutputChanging the color of axes labels and the size of those axes labels −> plot(x,y,col.lab="blue",cex.lab=2)Output> plot(x,y,col.lab="dark blue",cex.lab=3)Output

Read More
Showing 1561–1570 of 1,958 articles
« Prev 1 155 156 157 158 159 196 Next »
Advertisements