Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Programming Articles
Page 14 of 2545
How to change the font size of legend in base R plot?
In base R, we can use legend function to add a legend to the plot. For example, if we want to create a histogram with legend on top-right position then we can use legend("topright",legend="Normal Distribution") and if we want to change the font size then we need to as cex argument as shown below:legend("topright",legend="Normal Distribution",cex=2)Examplelegend("topleft",legend="Histogram of",cex=1.5)Output
Read MoreHow to replace missing values with median in an R data frame column?
To replace missing values with median, we can use the same trick that is used to replace missing values with mean. For example, if we have a data frame df that contain columns x and y where both of the columns contains some missing values then the missing values can be replaced with median as df$x[is.na(df$x)]
Read MoreHow to create scatterplot for factor levels in an R data frame?
To create a scatterplot for factor levels, we can use facet_grid function of ggplot2 package. For example, suppose we have a factor column in a data frame df defined as F and numerical columns defined as x and y then the scatterplot for the factor levels can be created as −ggplot(df,aes(x,y))+geom_point()+facet_grid(~Factor)Examplelibrary(ggplot2) ggplot(df,aes(x,y))+geom_point()+facet_grid(~Factor)Output
Read MoreHow to change the Y axis limit for boxplot created by using ggplot2 in R?
One of the most important aspects of a boxplot is Y-axis labels because these labels help us to understand the limit of the variable. Since R generate these labels automatically in a good way, we stick with that but we can change that using coord_cartesian function with ylim as shown in the below example.Exampleggplot(df,aes(x,y))+geom_boxplot()+coord_cartesian(ylim=c(290,400))Output
Read MoreHow to create a sample from an R data frame if weights are assigned to the row values?
To create a random sample in R, we can use sample function but if the weight of the values is provided then we need to assign the probability of the values based on the weights. For example, if we have a data frame df that contains a column X with some values and another column Weight with the corresponding weights then a random sample of size 10 can be generated as follows −df[sample(seq_len(nrow(df)), 10, prob=df$Weight_x), ]Exampledf[sample(seq_len(nrow(df)), 5, prob=df$weight_x), ] Output x weight_x 11 5.257177 10 19 5.401021 9 13 5.334041 10 10 4.416107 6 5 6.593158 2Exampledf[sample(seq_len(nrow(df)), 3, prob=df$weight_x), ...
Read MoreHow to replicate whole data frame and add it in the original one in R?
The replicates of a data frame in R can be created with the help of sapply function, to set the number of times we want to repeat the data frame we can use rep.int,times argument. For example, if we have a data frame df and we want to create 5 replicates of df and add them in the original then sapply(df,rep.int,times=5) can be used.Examplesapply(df,rep.int,times=5)Output x1 x2 x3 x4 [1,] 20.84538 9.486324 2.961236 967.9296 [2,] 23.29721 5.344792 3.044849 960.2204 [3,] 20.55978 6.064207 3.005293 1086.9639 [4,] 20.66044 8.436004 2.892010 1029.8222 [5,] 19.81347 9.277129 2.980567 1018.0453 [6,] 20.84538 9.486324 2.961236 967.9296 [7,] 23.29721 5.344792 3.044849 960.2204 [8,] 20.55978 6.064207 3.005293 1086.9639 [9,] 20.66044 8.436004 2.892010 1029.8222 [10,] 19.81347 9.277129 2.980567 1018.0453 [11,] 20.84538 9.486324 2.961236 967.9296 [12,] 23.29721 5.344792 3.044849 960.2204 [13,] 20.55978 6.064207 3.005293 1086.9639 [14,] 20.66044 8.436004 2.892010 1029.8222 [15,] 19.81347 9.277129 2.980567 1018.0453 [16,] 20.84538 9.486324 2.961236 967.9296 [17,] 23.29721 5.344792 3.044849 960.2204 [18,] 20.55978 6.064207 3.005293 1086.9639 [19,] 20.66044 8.436004 2.892010 1029.8222 [20,] 19.81347 9.277129 2.980567 1018.0453 [21,] 20.84538 9.486324 2.961236 967.9296 [22,] 23.29721 5.344792 3.044849 960.2204 [23,] 20.55978 6.064207 3.005293 1086.9639 [24,] 20.66044 8.436004 2.892010 1029.8222 [25,] 19.81347 9.277129 2.980567 1018.0453
Read MoreHow to align the bars of a barplot with the X-axis using ggplot2 in R?
The bar plot is created with geom_bar function but there always exist some space between the bars and the X-axis labels. If we want to reduce that space or completely remove it we need to use scale_y_continuous function by defining expand argument for former and scale_y_continuous(expand=c(0,0)) for latter.Exampleggplot(df,aes(x,y))+geom_bar(stat="identity")+scale_y_continuous(expand=c(0,0))Output
Read MoreHow to find the minimum value of an array in JavaScript?
We can find the minimum value of an array using Math.min() and sort()1) Math.min() Math.min() function usually takes all the elements in an array and scrutinize each and every value to get the minimum value.It's methodology is discussed in the below example. var numbers = [6, 39, 55, 1, 44]; document.getElementById("min").innerHTML = numbers; function myFunction() { numbers.sort(function(a, b){return b - a}); document.getElementById("min").innerHTML = numbers; document.write(numbers[numbers.length-1]); } After running the program the out put will be shown as in the following imageOutputOn clicking the click me button above ...
Read MoreHow to perform group-wise linear regression for a data frame in R?
The group−wise linear regression means creating regression model for group levels. For example, if we have a dependent variable y and the independent variable x also a grouping variable G that divides the combination of x and y into multiple groups then we can create a linear regression model for each of the group. In R, we can convert data frame to data.table object, this will help us to create the regression models easily.Exampledf2[,as.list(coef(lm(Salary~Ratings))),by=Class]OutputClass (Intercept) Ratings 1: I 31894.13 194.9152 2: III 35270.10 663.4089 3: II 40405.42 -1087.9103
Read MorePHP abs() Function
Definition and UsageThe abs() function is an in-built function in PHP iterpreter. This function accepts any number as argument and returns a positive value, disregarding its sign. Absolute value of any number is always positive.This function always returns positive number.Syntaxabs( mixed $num)ParametersSr.NoParameter & Description1numThis parameter stores a value whose absolute value is to be obtained.Return ValuesPHP abs() function returns absolute value of num. If data type of num is float, return type is also float.For integer parameter, return type is integer.PHP VersionThis function is available in PHP versions 4.x, PHP 5.x as well as PHP 7.x.ExampleOutputThis will produce the following ...
Read More