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 16 of 2545
How 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 MorePHP acosh() Function
Definition and UsageThe acosh() function returns the inverse hyperbolic cosine ratio of given angle in of given parameter. In other words, the return value of asinh() is hyperbolic sine of given parameter. A hyperbolic inverse hyperbolic cosine function is defined as −.acosh(x) = log(x+sqrt(pow(x, 2)-1))This function returns a float value.Syntaxacosh ( float $arg ) : floatParametersSr.NoParameter & Description1argA floating point value whose inverse hyperbolic cosine is to be calculatedReturn ValuesPHP acosh() function returns inverse hyperbolic cosine ratio of given parameter.PHP VersionThis function is available in PHP versions 4.x, PHP 5.x as well as PHP 7.x.ExampleFollowing example calculates acosh(pi/2) and returns ...
Read MoreWrite a program to reverse digits of a number in C++
A program to reverse digits of a number will interchange the position of the digits and reverse there order.Let’s suppose be a number abcde the reverse will be edcba.Let’s take an example to understand the problem, Inputn = 786521Output125687To reverse digits of the number, we will take each digit of the number from MSB(unit digit) and add it to reverse number variable, after this divide the original number by 10 and the reverse_number is multiplied by 10. This will be done until the number becomes 0.This repetitive process can be accomplished by two methods, iteration, and recursion, we will create ...
Read MoreHow to print a one-month calendar of user choice using for loop in C?
The logic to print a one-month calendar is as follows −for(i=1;i
Read MoreHow to find the total of frequency based on the values of a factor column in R?
Often, we have duplicate values in a factor column that means a factor column has many levels and each of these levels occur many times. In this situation, if we have a frequency column then we want to find the total of that frequency based on the values of a factor column and this can be done by using aggregate function.Example> Metal Quantity df2 head(df2, 10) Metal Quantity 1 Iron 43 2 Nickel 33 3 Lead 25 4 Zinc 24 5 Tin 27 6 Sodium 34 7 Silver ...
Read More