Find Number of Positive Values in an R Vector

Nizamuddin Siddiqui
Updated on 05-Jan-2021 06:04:20

2K+ Views

We know that positive values are greater than 0, therefore, we can use this condition with length function to find the number of positive values in a vector. For example, if we have a vector x that contains some positive and some negative values and we want to find the number of values that are positive then we can use the command length(x[x>0]).Example1Live Demo> x1 x1Output[1] 0.21314126 1.23449384 -1.02721325 -0.23168203 -1.36368881 -0.82416287 [7] 0.31224895 -0.90773340 0.10312288 -0.38914253 0.01196499 0.44875369 [13] 0.40820219 0.70172242 -0.23766272 -0.01023414 1.12403398 0.05837136 [19] -0.67403563 -0.26134292 0.31192384 -1.25116951 0.22115555 0.46544495 [25] 0.76567139 0.76948285 -1.42650924 0.24616899 0.18043015 1.04896235 ... Read More

Check If All Values in a Vector Are Integer in R

Nizamuddin Siddiqui
Updated on 05-Jan-2021 06:03:06

3K+ Views

To check whether all values in a vector in R are integer or not, we can round the vector using floor function then subtract the vector values from it and check whether the output is zero or not. If the output will be zero that means the value is integer otherwise it is not. The floor function returns the largest integer that is smaller or equal to the actual value. For example, if we have a vector x then it can be done as x-floor(x)==0.Example1Live Demo> x1 x1Output[1] 4 0 2 8 6 1 3 7 3 4 0 7 ... Read More

Convert Matrix to Data Frame with Column and Row Names in R

Nizamuddin Siddiqui
Updated on 05-Jan-2021 06:01:36

1K+ Views

To convert a matrix into a data frame with column names and row names as variables, we first need to convert the matrix into a table and then read it as data frame using as.data.frame. For example, if we have a matrix M then it can be done by using the below command −as.data.frame(as.table(M))Example1Live Demo> M1 M1Output[, 1] [, 2] [, 3] [, 4] [, 5] [, 6] [1, ] 1 7 13 19 25 31 [2, ] 2 8 14 20 26 32 [3, ] 3 9 15 21 27 33 [4, ] 4 10 16 22 28 34 ... Read More

Create Empty Data Frame with Fixed Number of Rows in R

Nizamuddin Siddiqui
Updated on 05-Jan-2021 06:00:17

2K+ Views

To create an empty data frame with fixed number of rows but no columns, we can use data.frame function along with the matrix function. That means we need to create a matrix without any column using matrix and save it in a data frame using data.frame function as shown in the below examples.Example1Live Demo> df1 df1Outputdata frame with 0 columns and 10 rows Example2Live Demo> df2 df2Outputdata frame with 0 columns and 100 rows Example3Live Demo> df3 df3Outputdata frame with 0 columns and 39 rows Example4Live Demo> df4 df4Outputdata frame with 0 columns and 20 rows Example5Live Demo> df5 df5Outputdata ... Read More

Count Unique Numbers by Adding One and Removing Trailing Zeros in C++

Sunidhi Bansal
Updated on 05-Jan-2021 05:12:50

204 Views

We are given a number N as input. Perform two operations on N and identify the count of unique numbers generated in the process. Steps will −Add 1 to numberRemove trailing zeros from the generated number, if anyIf N is 8 then numbers generated will beApplying step 1− 8 → 9 →Applying step 2− 1 → ( removed 0 from 10 )Applying step 1: 2 → 3 → 4 → 5 → 6 → 7 → 8 ( same sequence )Count of unique numbers will be 9.For ExampleInputN=21OutputCount of unique numbers that can be generated from N by adding one ... Read More

Count Triplets in a Sorted Doubly Linked List Whose Sum is Equal to a Given Value X in C++

Sunidhi Bansal
Updated on 05-Jan-2021 05:10:32

271 Views

Given a sorted doubly linked list containing integer values. The goal is to find triplets whose product is equal to the given value x. If input linked list is 3−4−1−2 and x is 6 then count will be 1 (triplet (3, 1, 2) )For ExampleInputlinked list: [ 3−4−13−5−10−10−0 ] x=20OutputCount of triplets in a sorted doubly linked list whose product is equal to a given value x are: 2ExplanationTriplets will be: ( 3, 4, 13 ) and ( 10, 10, 0 )Inputlinked list: [ 4−3−1−5−2−4−2 ] x=8OutputCount of triplets in a sorted doubly linked list whose product is equal to ... Read More

Count Triplets in a Sorted Doubly Linked List with Product Equal to X in C++

Sunidhi Bansal
Updated on 05-Jan-2021 05:09:32

224 Views

Given a sorted doubly linked list containing integer values. The goal is to find triplets whose product is equal to the given value x. If input linked list is 3−4−1−2 and x is 6 then count will be 1 (triplet (3, 1, 2))For ExampleInputlinked list: [ 200−4−16−5−10−10−2 ] x=200OutputCount of triplets in a sorted doubly linked list whose product is equal to a given value x are: 3ExplanationTriplets will be: (4, 5, 10), (4, 5, 10) and (10, 10, 2)Inputlinked list: [ 4−3−1−5−2−4−2] x=12OutputCount of triplets in a sorted doubly linked list whose product is equal to a given value ... Read More

Count Occurrences of Average of Array Elements in C++

Sunidhi Bansal
Updated on 05-Jan-2021 05:00:41

168 Views

Given an array arr[] containing integer elements and an integer num. The goal is to find the average of each element arr[i] and num and print the count of the number of times that average appeared in the original array.If array arr[] is [ 5, 2, 3 ] and num is 2. Averages will be [ 3, 2, 2 ] occurrences in arr[] is [ 1, 1, 1 ]For ExampleInputarr[] = { 1, 6, 4, 3, 6, 4 } num=2Output1 2 1 0 2 1Count of occurrences of the average of array elements with a given number are − 5ExplanationThe ... Read More

Count Occurrences of a Character in a Repeated String in C++

Sunidhi Bansal
Updated on 05-Jan-2021 04:56:55

6K+ Views

Given a string str, a character and a positive integer N. The string str is repeated indefinitely. The goal is to find the count of occurrences of character in str in first N characters of repetitions.If str is “abac”, character is ch=‘b’ and N is 10.In first 10 characters of “abacabacabacabac…….” b occurs twice.Note − Take str and character ch within the same case.Let us understand with examples.For ExampleInputstr = "TPTTTT" ch = 'T' n = 12OutputCount of occurrences of a character in a repeated string are: 10ExplanationThe number of ‘T’ in str is 5. Length of str is 6. ... Read More

Count Occurrences of a Substring Recursively in Java

Sunidhi Bansal
Updated on 05-Jan-2021 04:54:47

9K+ Views

Given two strings str_1 and str_2. The goal is to count the number of occurrences of substring str2 in string str1 using a recursive process.A recursive function is the one which has its own call inside it’s definition.If str1 is “I know that you know that i know” str2=”know”Count of occurences is − 3Let us understand with examples.For ExampleInputstr1 = "TPisTPareTPamTP", str2 = "TP";OutputCount of occurrences of a substring recursively are: 4ExplanationThe substring TP occurs 4 times in str1.Inputstr1 = "HiHOwAReyouHiHi" str2 = "Hi"OutputCount of occurrences of a substring recursively are: 3ExplanationThe substring Hi occurs 3 times in str1.Approach used ... Read More

Advertisements