Programming Articles - Page 922 of 3363

How to find the median of frequency data in R?

Nizamuddin Siddiqui
Updated on 02-Nov-2021 06:22:03

1K+ Views

If we have frequency data then we first need to find the total data or complete data by repeating the values up to the frequency corresponding to each value after that we can apply median function on this complete data.For Example, if we have a data frame called df that contains two columns say X and Frequency then we can find the total data by using the following command −Total_data

C++ program to rearrange an array in maximum minimum form

Sunidhi Bansal
Updated on 02-Nov-2021 06:11:54

2K+ Views

We are given an integer array which can be arranged in sorted/unsorted manner. The task is to first sort the array if the values are unsorted then arrange the array in such a manner that the first element of array will be the maximum value, second will be the minimum value, third will be the second largest value, fourth will be the second minimest value and so on.Let us see various input output scenarios for this −Input − int arr[] = {7, 5, 2, 3, 4, 9, 10, 5 }Output − Array before Arrangement: 2 3 4 5 5 7 9 10 ... Read More

How to concatenate elements of equal size vectors alternatively in R?

Nizamuddin Siddiqui
Updated on 02-Nov-2021 06:10:10

201 Views

If we have multiple vectors and we want to combine the vector elements alternatively then we can use rbind function along with c function.For Example, if we have three vectors say X, Y, and Z as −X = 1, 2, 3 Y = 4, 5, 6 Z = 7, 8, 9Then, we can combine the elements in these vectors alternatively by using the below command −c(rbind(X,Y,Z,)) Example 1To concatenate elements of equal size vectors add the following code to the above snippet −x1

How to find the sample size for t test in R?

Nizamuddin Siddiqui
Updated on 02-Nov-2021 06:14:48

4K+ Views

To find the sample size for t test, we can use pwr.t.test function of pwr package, wherever we can pass the arguments for alternative hypothesis such as one-sided or two-sided, significance level, power of the test and difference for two samples.Check out the below examples to understand how it works.Example 1Consider the following code to find sample size for t test −library("pwr") pwr.t.test(power=0.80, d=1, sig.level=0.05, alternative="two.sided")OutputIf you execute the above given code, it generates the following Output for the two-sample t test power calculation −   n = 16.71472    d = 1 sig.level = 0.05    power = 0.8 alternative ... Read More

C++ program to rearrange all elements of array which are multiples of x in increasing order

Sunidhi Bansal
Updated on 02-Nov-2021 06:07:45

461 Views

We are given an integer type array as ‘int arr[]’ and an integer type variable as ‘x’. The task is to rearrange all the elements of an array in such a manner that they will be divisible by a given integer value ‘x’ and the arrangement should be in an increasing order.Let us see various input output scenarios for this −Input − int arr[] = {4, 24, 3, 5, 7, 22, 12, 10}, int x = 2Output − Rearrangement of all elements of array which are multiples of x 2 in decreasing order is: 4 10 3 5 7 12 22 24Explanation −we ... Read More

How to make duplicate factor levels unique in an R data frame?

Nizamuddin Siddiqui
Updated on 02-Nov-2021 06:06:58

2K+ Views

The factor with duplicate levels represents grouping data but if we want to convert the grouping data into nominal data then duplicate values must be removed or converted into unique values. To make duplicate factor levels unique in an R data frame, we can use make.unique function.Check out the below Examples to understand how it works.Example 1Following snippet creates a sample data frame −Factor

How to divide each value in an R data frame by 100?

Nizamuddin Siddiqui
Updated on 02-Nov-2021 06:06:50

4K+ Views

We sometimes need to perform mathematical operations on all values in the data set. One such operation could be dividing each value by 100.For example, if we have a data frame called df then we can divide each value in df by 100 by using the below command −df[,1:ncol(df)]/100ExampleFollowing snippet creates a sample data frame −x1

Recursive sum of digit in n^x, where n and x are very large in C++

Sunidhi Bansal
Updated on 02-Nov-2021 06:04:24

210 Views

We are given positive integer variables as ‘num’ and ‘x’. The task is to recursively calculate the num ^ x then add the digits of a resultant number till the single digit isn’t achieved and the resultant single digit will be the output.Let us see various input output scenarios for this −Input − int num = 2345, int x = 3Output − Recursive sum of digit in n^x, where n and x are very large are: 8Explanation − we are given positive integer values as num and x with the values as 2345 and power as 3. Firstly, calculate 2345 ^ 3 i.e. ... Read More

How to find the groupwise common value for a data.table object?

Nizamuddin Siddiqui
Updated on 02-Nov-2021 05:53:54

202 Views

To find the groupwise common value for a data.table object, we can use Reduce function with intersect function.For example, if we have a data.table object called DT that contains a numerical column say Num and a categorical column say C where C exists at the first position then the groupwise common value can be found by using the command given below −Reduce(intersect,DT[,.(list(unique(Num))),C]$V1)ExampleConsider the below data.table object −Group

Create ggplot2 graph with reversed Y-axis and X-axis on top in R.

Nizamuddin Siddiqui
Updated on 12-Nov-2021 04:08:58

1K+ Views

To create ggplot2 graph with reversed Y-axis and X-axis on top, we can use scale_y_reverse and scale_x_continuous function of ggplot2 package.For Example, if we have a data frame called df that contains two columns say X and Y and we want to create the scatterplot between X and Y with reversed Y-axis and X-axis on top then we can use the below command −ggplot(df,aes(X,Y))+geom_point()+scale_y_reverse()+scale_x_continuous(position="top")ExampleFollowing snippet creates a sample data frame −x

Advertisements