Found 26504 Articles for Server Side Programming

How to find the row-wise mode of a matrix in R?

Nizamuddin Siddiqui
Updated on 05-Mar-2021 06:11:00

642 Views

There is no in-built function to find the mode in R, hence we need to create one and then apply it to the rows of the matrix. The function for mode is created as follows −mode M1 M1Output     [,1] [,2] [,3] [,4] [,5] [1,]  2    2    1    2   2 [2,]  2    2    2    2   1 [3,]  2    2    1    1   1 [4,]  2    1    1    1   1 [5,]  2    1    1    2   2> apply(M1,1,mode)Output[1] 2 2 1 1 2Example2Live Demo> M2 M2Output     [,1] [,2] [,3] [,4] [,5] [1,]  1    1    2    2    1 [2,]  2    1    1    2    1 [3,]  2    2    1    1    1 [4,]  2    1    1    2    2 [5,]  2    1    1    2    2 [6,]  1    2    1    1    2 [7,]  1    1    2    1    2 [8,]  2    2    1    2    1 [9,]  2    1    1    2    2 [10,] 1    1    2    2    2 [11,] 1    1    2    1    2 [12,] 1    2    2    2    1 [13,] 2    2    2    2    1 [14,] 2    1    2    2    1 [15,] 1    2    1    1    2 [16,] 2    2    1    2    1 [17,] 2    2    1    1    1 [18,] 2    1    1    2    1 [19,] 1    1    1    2    1 [20,] 2    1    1    2    2> apply(M2,1,mode)Output[1] 1 1 1 2 2 1 1 2 2 2 1 2 2 2 1 2 1 1 1 2Example3Live Demo> M3 M3Output     [,1] [,2] [,3] [,4] [,5] [1,]  1    3    3    2    1 [2,]  2    3    1    2    2 [3,]  2    2    3    3    1 [4,]  1    3    1    3    2 [5,]  3    1    2    1    2 [6,]  2    3    1    1    1 [7,]  2    2    2    3    1 [8,]  1    2    2    2    2 [9,]  2    1    2    1    2 [10,] 1    3    1    2    1 [11,] 2    1    3    1    1 [12,] 1    1    3    2    2 [13,] 2    1    1    1    2 [14,] 2    1    3    3    2 [15,] 1    2    3    1    2 [16,] 1    2    1    2    1 [17,] 3    1    1    3    2 [18,] 3    3    3    3    1 [19,] 3    2    3    1    1 [20,] 3    3    2    2    1> apply(M3,1,mode)Output[1] 1 2 2 1 1 1 2 2 2 1 1 1 1 2 1 1 1 3 1 2Example4Live Demo> M4 M4Output      [,1] [,2] [,3] [,4] [,5] [1,]  10    10   9    10   9 [2,]  9     9    10   9    9 [3,]  9     9    9    10   10 [4,]  10    9    9    10   10 [5,]  10    10   9    10   9 [6,]  10    10   9    10   10 [7,]  9     9    9    10   9 [8,]  9     10   9    10   9 [9,]  9     9    9    9    9 [10,] 9     10   9    10   9 [11,] 10    10   9    9    9 [12,] 9     9    9    9    9 [13,] 10    10   10   9 10 [14,] 10    9    10   10 10 [15,] 9     10   9    10 9 [16,] 9     10   9    10 9 [17,] 9     10   10   9 10 [18,] 9     9    9    9 10 [19,] 10    9    9    10 9 [20,] 10    9    9    10 9> apply(M4,1,mode)Output[1] 10 9 9 10 10 10 9 9 9 9 9 9 10 10 9 9 10 9 9 9

How to create group names for consecutively duplicate values in an R data frame column?

Nizamuddin Siddiqui
Updated on 05-Mar-2021 06:12:19

453 Views

The grouping of values can be done in many ways and one such way is if we have duplicate values or unique values then the group can be set based on that. If all the values are unique then there is no sense for grouping but if we have varying values then the grouping can be done. For this purpose, we can use rleid function as shown in the below examples.Example1Consider the below data frame −Live Demo> x df1 df1Output x 1 2 2 1 3 2 4 2 5 1 6 ... Read More

How to convert a data frame into table for two factor columns and one numeric column in R?

Nizamuddin Siddiqui
Updated on 05-Mar-2021 06:15:52

571 Views

When we have two factor columns and one numeric column then we can create a contingency table for the total count of numeric values based on the factor columns. This can be done with the help of xtabs function in base R. For example, if we have a data frame called df that contains two factor columns say f1 and f2, and one numeric column say Y then the contingency table for df can be created by using the command xtabs(Y~f1+f1, df).Example1Consider the below data frame −Live Demo> x1 x2 y1 df1 df1Output   x1 x2 y1 1  B  a  5 ... Read More

How to print the range of numbers using C language?

Bhanu Priya
Updated on 13-Mar-2021 11:41:01

2K+ Views

ProblemFor given number try to find the range in which that number exists.SolutionHere, we are learning how to find the ranges of a number.The logic that we apply to find range is −lower= (n/10) * 10; /*the arithmetic operators work from left to right*/ upper = lower+10;ExplanationLet number n=45Lower=(42/10)*10 // division returns quotient       =4*10 =40Upper=40+10=50Range − lower-upper − 40-50ExampleFollowing is the C program for printing the range of numbers −#include main(){    int n, lower, upper;    printf("Enter a number:");    scanf("%d", &n);    lower= (n/10) * 10; /*the arithmetic operators work from left to right*/    upper ... Read More

Finding sum of first and last digit using divide and modulo operator in C language

Bhanu Priya
Updated on 13-Mar-2021 11:40:17

552 Views

ProblemWhat is the C program to obtain the sum of the first and last digit of the number, if a four-digit number is input through the keyboard?SolutionIn this program, we are taking a four-digit number at run time and trying to find the sum of first and last digit of that four-digit number by using the logic −a=n%10; b=n/1000; result = a + b;Let’s apply this logic to find sum of first and last digit of four-digit number −ExampleFollowing is the C program for finding sum of first and last digit by using divide and modulo operator −#include main(){   ... Read More

What are different operators and expressions used in C language?

Aishwarya Naglot
Updated on 12-Nov-2024 10:37:28

14K+ Views

Operators are special symbols in C that performs an operation on values and variables. These special symbols allow us to manipulate data and variables in different ways. Those Operators are classified into the following − Arithmetic operators. Relational operators. Logical operators. Assignment operators. Increment and decrement operators. Bitwise operators. Conditional operators. Special operators. Expressions in C is a combination of variables, operators, and values that generates ... Read More

Explain the Format of C language

Bhanu Priya
Updated on 11-Mar-2021 08:35:46

2K+ Views

C programming is a general-purpose, procedural, imperative computer programming language. In C language, we see thatStatements are terminated with semicolons.C is case sensitiveIndentation is ignored by the compiler.Strings are placed in double quotes.Library functions are lowercase.Newlines are handled via Format of CThe format of C programming language is explained below −SemicolonsSemicolons are very important in C.It tells the compiler, where one statement ends and the next statement begins.If we fail to place the semicolon after each statement, you will get compilation errors.Case SensitivityC is a case sensitive language. Although, int compiles, "Int”, "INT” or any other variation will not be ... Read More

Check if the value entered is palindrome or not using C language

Bhanu Priya
Updated on 11-Mar-2021 08:28:05

586 Views

A palindrome is nothing but any word, number, sentence, or other sequence of characters that reads the same backward as forward.In this programming, we are trying to enter a number from console, and assign that number to temp variable.If number is greater than zero, apply the logic given below −while(n>0){    r=n%10;    sum=(sum*10)+r;    n=n/10; }If temp=sum, then the given number is a palindrome. Otherwise, it is not a palindrome.ExampleFollowing is the C program for verification of a value being palindrome −#include #include void main(){    int n, r, sum=0, temp;    printf("Enter a number: ");    scanf("%d", &n); ... Read More

Explain the concept of Sorting in C language

Bhanu Priya
Updated on 11-Mar-2021 07:51:40

5K+ Views

ProblemWhy sorting makes searching easier in C language? How can you judge the sorting efficiency in C?SolutionSorting is the process of arranging elements either in ascending (or) descending order.The term sorting came into existence when humans realized the importance of searching quickly.There are different things in life that we need to search for, particular record in database, roll numbers in a list, a number in a telephone directory, a specific page in a book etc.If the data was kept in an unordered and unsorted form, it becomes difficult to search a particular thing. But fortunately, the concept of sorting came ... Read More

Explain the Random accessing files in C language

Bhanu Priya
Updated on 11-Mar-2021 07:20:51

11K+ Views

Random accessing of files in C language can be done with the help of the following functions −ftell ( )rewind ( )fseek ( )ftell ( )It returns the current position of the file ptr.The syntax is as follows −int n = ftell (file pointer)For example, FILE *fp; int n; _____ _____ _____ n = ftell (fp);Note − ftell ( ) is used for counting the number of characters which are entered into a file.rewind ( )It makes file ptr move to beginning of the file.The syntax is as follows −rewind (file pointer);For example, FILE *fp;    -----    -----   ... Read More

Advertisements