Found 26504 Articles for Server Side Programming

How to create smooth density curves without filling densities in R?

Nizamuddin Siddiqui
Updated on 21-Nov-2020 05:36:48

490 Views

The density curves can be created by using stat_density function of ggplot2 package but it fills the curve with density hence it becomes difficult to recognize the curves. We can remove these densities by using geom="line" inside the stat_density function so that only the density curves will be plotted.ExampleConsider the below data frame:Live Demo> G Response df dfOutputG Response 1 C 1.0229016 2 C 1.0058160 3 B 0.8831558 4 B 0.7729167 5 C 0.9130468 6 D 0.8431893 7 B 1.5003581 8 A 0.9687335 9 B 1.1139661 10 A 0.9211660 11 A 1.1790619 12 D 0.6349671 13 A 1.2616918 14 A ... Read More

How to check if some specific columns of an R data frame are equal to a column or not?

Nizamuddin Siddiqui
Updated on 21-Nov-2020 05:35:11

407 Views

If we have a large amount of data in a data frame and we suspect that some of the data columns are repeated or some of them are equal to a particular column then we can use sapply function in base R to figure it out. In this way, we can remove duplicated columns that does not suppose to help in our data analysis objective.Example1Consider the below data frame:Live Demo> set.seed(354) > x1 x2 x3 x4 x5 df1 df1Outputx1 x2 x3 x4 x5 1 4 5 4 4 6 2 6 4 8 7 5 3 5 6 4 7 ... Read More

How to convert a date or date vector to POSIXct in R?

Nizamuddin Siddiqui
Updated on 21-Nov-2020 05:33:09

5K+ Views

To convert a date or date vector to POSIXct, we can use as.POSIXct function but we also need to pass the appropriate date format inside the function. For example, if we have a date "2020-11-14" then it can be converted to POSIXct by using as.POSIXct("2020-11-14", format="%Y-%m-%d").Example1Live Demo> date1 as.POSIXct(date1, format="%Y-%m-%d")Output[1] "2020-04-01 IST"Example2Live Demo> date2 date2Output[1] "2020-02-12" "2020-06-01" "2020-04-01" "2020-05-01" "2020-01-21" [6] "2020-01-21" "2020-06-01" "2020-04-27" "2020-05-11" "2020-06-01" [11] "2020-01-21" "2020-03-31" "2020-05-01" "2020-02-12" "2020-01-21" [16] "2020-05-01" "2020-03-31" "2020-04-01" "2020-05-01" "2020-01-21" [21] "2020-05-01" "2020-04-11" "2020-05-11" "2020-04-01" "2020-03-31" [26] "2020-04-11" "2020-04-01" "2020-03-31" "2020-04-01" "2020-04-11" [31] "2020-05-11" "2020-06-01" "2020-03-31" "2020-04-27" "2020-01-21" [36] "2020-01-21" "2020-04-01" "2020-06-01" ... Read More

How to create a horizontal bar plot using barplot function in R?

Nizamuddin Siddiqui
Updated on 21-Nov-2020 05:31:01

240 Views

To create a bar plot in base R, we can directly use barplot function but the table of frequencies should be passed inside this function. If we want to create the barplot in horizontal manner then horiz=TRUE argument must be added. For example, if we have a vector x that contains repeating values then the horizontal bar plot of x can be created by using barplot(table(x),horiz=TRUE).Example1> x barplot(table(x),horiz=TRUE)OutputExample2> y barplot(table(y),horiz=TRUE)OutputExample3> z barplot(table(z),horiz=TRUE)Output

How to remove rows that contains NA values in certain columns of an R data frame?

Nizamuddin Siddiqui
Updated on 21-Nov-2020 05:29:15

2K+ Views

If we have missing data in our data frame then some of them can be replaced if we have enough information about the characteristic of the case for which the information is missing. But if that information is not available and we do not find any suitable way to replace the missing values then complete.cases function can be used with the columns that has missing values.ExampleConsider the below data frame:Live Demo> set.seed(19991) > x1 x2 x3 x4 x5 df1 df1Output x1 x2 x3 ... Read More

How to create a replicated list of a list in R?

Nizamuddin Siddiqui
Updated on 21-Nov-2020 05:26:32

3K+ Views

Sometimes we want to created repeated values, this is helpful in different scenarios such as measuring an effect of a constant on multiple variables. The list values can be also replicated for similar purpose of analysis. The replication of list of a list can be created by using rep function. For example, if we have a list called x and we want to create five times replicated list of this list then we can use the code rep(list(x), 5).Example1Live Demo> List1 List1Output$x1 [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ... Read More

How to create a subset of a data frame in R without using column names?

Nizamuddin Siddiqui
Updated on 21-Nov-2020 05:23:41

627 Views

The subsetting of a data frame can be done by using column names as well as column number. Also, we can subset by subsequent as well as non-subsequent column numbers. For example, if we have a data frame df that contains column x, y, z then we can make a subset of x and z by using df[, c(1, 3)].ExampleConsider the below data frame:> set.seed(191) > x1 x2 x3 x4 df1 df1Output x1 x2 x3 ... Read More

How to create a line that passes through specified points in an R plot?

Nizamuddin Siddiqui
Updated on 21-Nov-2020 05:20:43

740 Views

To create a line that passes through specified points, we first need to create the plot then using plot function then xspline function can be used to join the points with straight lines. The xspline function is specifically designed to draw curves and hence it can be also used to join points in a plot as shown in the below examples.Example1> plot(rpois(10,5)) > xspline(c(4,3,1),c(7,5,2))Output:Example2> plot(rnorm(10)) > xspline(c(4,3,1),c(0.3,-0.5,-1.5))Output:

How to create an empty plot using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 21-Nov-2020 05:19:05

2K+ Views

The two most easy ways to create an empty plot using ggplot2 are using geom_blank function and also adding the theme_bw along with the geom_blank. The geom_blank will create an empty plot with white gridlines and grey background, on the other hand, addition of theme_bw will create the empty plot with grey gridlines and white background.ExampleConsider the below data frame:Live Demo> set.seed(151) > x y df dfOutput      x         y 1  -0.05153895 0.3139643 2   0.76573738 0.1816184 3  -0.14673959 0.8201743 4  -0.11318581 1.6005576 5  -0.39551140 0.6770630 6  0.78227595 0.7446956 7  -1.39747811 0.7004385 8  -1.01883832 1.2728014 ... Read More

How to perform post hoc test for Kruskal-Wallis in R?

Nizamuddin Siddiqui
Updated on 21-Nov-2020 05:16:30

7K+ Views

The Kruskal-Wallis test is the non-parametric analogue of one-way analysis of variance. The non-parametric tests are used in situations when the assumptions of parametric tests are not met. If we find significant difference in Kruskal-Wallis then post hoc tests are done to find where the difference exists. For this purpose, we can perform dunn test. The function of dunn test can be accessed through FSA package.Example1Loading FSA package:> library(FSA)Consider the below data frame:Live Demo> x1 y1 df1 df1Output  x1 y1 1 E 1.1191117 2 D 1.1276032 3 D 1.5610692 4 E 1.1585054 5 E 1.0239322 6 C ... Read More

Advertisements