Programming Articles - Page 917 of 3366

Remove rows from a data frame that exists in another data frame in R.

Nizamuddin Siddiqui
Updated on 02-Nov-2021 10:34:33

6K+ Views

To remove rows from a data frame that exists in another data frame, we can use subsetting with single square brackets. This removal will help us to find the unique rows in the data frame based on the column of another data frame.Check out the below Examples to understand how it can be done.Example 1Following snippet creates a sample data frame −x

Add values in sequence to the previous value with a constant.

Nizamuddin Siddiqui
Updated on 02-Nov-2021 10:15:59

584 Views

To add values in sequence to the previous value with a constant, we can find the cumulative sum of values and add the constant to the Output.For Example, if we have a vector called X and we want to add values in X in sequence to the previous value with a constant say 10 then we can use the command given below −10+cumsum(X)Example 1Following snippet creates a sample data frame −x1

Create cross tabulation for three categorical columns in an R data frame.

Nizamuddin Siddiqui
Updated on 02-Nov-2021 10:00:04

2K+ Views

To create cross tabulation for three categorical columns, we can use xtabs function. The xtabs function will create contingency table for each category in two columns and each contingency table will be created for the category in the third column.Check out the below Examples to understand how it can be done.Example 1Following snippet creates a sample data frame −df1

R Programing to change the name of column variable and row variable in xtab table.

Nizamuddin Siddiqui
Updated on 02-Nov-2021 09:44:48

470 Views

To change the name of column variable and row variable in xtab table, we can use setNames function.For Example, if we have a xtab table called XTAB and we want to change the column variable name C and row variable name R then we can use the below command −dimnames(XTAB)

Recursive program to print formula for GCD of n integers in C++

Sunidhi Bansal
Updated on 02-Nov-2021 08:29:54

233 Views

We are given an integer as input. The goal is to print the formula of GCD of n numbers using recursion.We know that the GCD of three numbers say a1, b1 and c1 will be gcd(a1, gcd(b1, c1)).Similarly for more than three numbers, gcd can be obtained by formula as gcd ( a1, gcd(b1, gcd(c1….., gcd(y1, z1)).ExamplesInput − Num = 4;Output − Formula is:GCD(int a3, GCD(int a2, GCD(int a1, int b1)))Input − Num = 6;Output − Formula is: GCD(int a5, GCD(int a4, GCD(int a3, GCD(int a2, GCD(int a1, int b1)))))Approach used in the below program is as followsIn this approach we are using the ... Read More

Recursive program to check if number is palindrome or not in C++

Sunidhi Bansal
Updated on 02-Nov-2021 08:28:12

6K+ Views

We are given an integer as input. The goal is to find whether the input number Num is a palindrome or not using recursion.To check if a number is palindrome, reverse that number and check if both numbers are the same or not. If the reversed number is equal to the original number, then it is palindrome.ExamplesInput − Num = 34212;Output − 34212 is not a Palindrome!Explanation − If we reverse 34212 then we get 21243. 34212 != 21243 hence the input number is not palindrome.Input − Num = 32123;Output − 32123 is Palindrome!Explanation − If we reverse 32123 then we get 32132. 32123!= 32123 ... Read More

Recursive program for prime number in C++

Sunidhi Bansal
Updated on 02-Nov-2021 08:26:06

6K+ Views

We are given an integer as input. The goal is to find whether the input number Num is a prime or non-prime using recursion.To check if a number is prime or not, start traversing from i=2 to i

Recursive Implementation of atoi() in C++

Sunidhi Bansal
Updated on 02-Nov-2021 08:20:05

2K+ Views

We are given a string containing a number. The goal is to find the equivalent number using the recursive atoi() method. int atoi(const char *str) converts the string argument str to an integer (type int).Example-:Input − Str[] = "58325"Output − The Equivalent decimal is :58325Explanation − The string contains the equivalent number 58325Input − Str[] = "00010"Output − The Equivalent decimal is :1Explanation − The string contains the equivalent number 10.Approach used in the below program is as followsIn this approach we are using the recursive function recurAtoi() which takes input string and its length and for each character convert it to decimal and multiply ... Read More

Recursive function to do substring search in C++

Sunidhi Bansal
Updated on 02-Nov-2021 08:18:19

1K+ Views

Given two strings Str and subStr as input. The goal is to find whether text present in subStr exists in Str as substring or not. The string X is called a substring of Y if whole X is present in Y at least once. We will use a recursive approach to do this.For ExampleInput − Str = “tutorialspoint” subStr=”Point”Output − Given string does not contain substring!Explanation − The string Point is not substring of tutorialspointInput − Str = “globalization” subStr=”global”Output − Given string contains substring!Explanation − The string global is substring of globalizationApproach used in the below program is as followsIn this approach we check ... Read More

C++ Program Recursive Insertion Sort

Sunidhi Bansal
Updated on 02-Nov-2021 08:14:39

1K+ Views

Insertion Sort is one of the sorting algorithms used to sort data by inserting elements like a deck of cards. All the elements are arranged from left to right then considering the first one as already sorted, insert rest to the sorted list on the left. Each element is compared with each element in the left list until it is inserted at its correct position.Insertion Sort Algorithmint arr[5]= { 5, 4, 2, 1, 3 };int i, j ;Traverse from index j=i+1 to jarr[i+1], swap those elements.Set temp=arr[i], arr[i]=arr[i+1] and arr[i+1]=temp.Now decrement length by 1 as the previous loop placed the ... Read More

Advertisements