Programming Articles - Page 914 of 3366

How to find the row wise mode of strings in an R data frame?

Nizamuddin Siddiqui
Updated on 03-Nov-2021 06:26:36

482 Views

To find the row wise model of strings in an R data frame, we can use apply function along with custom function for mode, if ties will be there then first value will be chosen based on alphabetical ordering.For Example, if we have a data frame called df that contains string values then we can find the row wise mode of strings by using the command given below −df$RowM

How to deal with error 'height' must be a vector or a matrix while creating barplot?

Nizamuddin Siddiqui
Updated on 03-Nov-2021 06:13:32

4K+ Views

The error 'height' must be a vector or a matrix while creating barplot occurs when we provide data frame name instead of column names or read it with as.matrix. If we want to create bar plot for columns in a data frame then the data frame needs to be read as matrix.For Example, if we have a data frame called df then we can create the barplot of columns in df by using the command given below −barplot(as.matrix(df))ExampleFollowing snippet creates a sample data frame −df

Row-wise common elements in two diagonals of a square matrix in C++

Sunidhi Bansal
Updated on 03-Nov-2021 06:15:22

264 Views

Given a 2D square matrix as input. The goal is to find the elements that are common in both its primary and secondary diagonals. If the input matrix is1 2 3 2 2 4 1 4 7Then its primary diagonal is 1 2 7 and the secondary diagonal is 3 2 1. Common element is 2.There will always be at least one common element in both.ExamplesInput − Matrix[][5] = {{1, 2, 1}, {4, 1, 6}, {1, 8, 1}};Output − Row-wise common elements in diagonals:3Explanation − The matrix is:1 2 1 4 1 6 1 8 1Primary diagonal=1 1 1, Secondary diagonal= 1 1 ... Read More

How to find bootstrap confidence interval in R?

Nizamuddin Siddiqui
Updated on 03-Nov-2021 06:03:48

6K+ Views

The bootstrap confidence interval can be found by using the boot function. The bootstrapping is a method of finding inferential statistics with the help of sample data. It is done by drawing a large number of samples with replacement from the same values. Check out the Examples given below to understand how we can create a bootstrap confidence interval.Example 1Following snippet creates a sample data frame −x1

How to find the frequency based on intervals in R data frame?

Nizamuddin Siddiqui
Updated on 03-Nov-2021 05:52:56

1K+ Views

To create intervals, we can use cut function with seq function and if we want to find the frequency based on these intervals then we just need to use table function along with cut function. We need to properly define the values for interval inside cut function. To understand how it can be done, check out the below Examples.Example 1Following snippet creates a sample data frame −x

Find the unique pair combinations of an R data frame column values.

Nizamuddin Siddiqui
Updated on 03-Nov-2021 05:48:46

2K+ Views

To find the unique pair combinations of an R data frame column values, we can use combn function along with unique function.For Example, if we have a data frame called df that contains a column say x then we can find the unique pair combinations of all column values by using the command given below −combn(unique(df$x),2,FUN=paste,collapse=' ')Example 1Following snippet creates a sample data frame −Grp

Create a random sample by ignoring missing values in an R vector.

Nizamuddin Siddiqui
Updated on 03-Nov-2021 05:41:52

389 Views

To create a random sample by ignoring missing values in an R vector, we can use sample function and the negation of is.na with vector name.For Example, if we have a vector called X that contains some NAs then we can create a random sample of size 100 of X values by using the command given below −sample(X[!is.na(X)],100,replace=TRUE)Example 1To create a random sample by ignoring the missing values in an R vector, use the command given below −x1

Row-wise vs column-wise traversal of matrix in C++

Sunidhi Bansal
Updated on 03-Nov-2021 06:08:30

4K+ Views

A matrix can be traversed in two ways. Row-mise traversal visits each row one by one starting from first row then second and so on till the last row. Elements in the row are returned from index 0 to the last index.In Column-wise traversal, elements are traversed from the first column to the last column in order.In 2D matrix M[i][j]. Index i is used for representing rows and index j is used for representing columns. For row-wise traversal, start fromi=0th row and 0

Reduce a number to 1 by performing given operations in C++

Sunidhi Bansal
Updated on 03-Nov-2021 05:39:26

1K+ Views

Given an integer Number as input. The goal is to find the minimum number of steps or operations required to reduce the input Number to 1. Operations that can be performed will be-:If Number is even, then Divide it by 2.If Number is odd, then increment or decrement it by 1.ExamplesInput − Number=28Output − Minimum steps to reduce 28 to 1: 6Explanation−28 is even - divide by 2 = 1414 is even - divide by 2 = 77 is odd - increment by 1 = 88 is even - divide by 2 = 44 is even - divide by 2 = 22 ... Read More

How to check which value is NA in an R data frame?

Nizamuddin Siddiqui
Updated on 03-Nov-2021 05:37:48

15K+ Views

To check which value in NA in an R data frame, we can use apply function along with is.na function.For Example, if we have a data frame called df that contains some NA values then we can check which value is NA by using the command mentioned below −apply(df,2, function(x) is.na(x))This will return the data frame in logical form with TRUE and FALSE. Check out the below Examples to understand how it works.Example 1Following snippet creates a sample data frame −x1

Advertisements