Programming Articles - Page 1368 of 3366

How to convert NaN values to NA in an R data frame?

Nizamuddin Siddiqui
Updated on 04-Mar-2021 08:52:10

757 Views

To convert NaN values to NA, we would need to detect the NaN values in the data frame and the set them to NA. For example, if we have a data frame called df that contains a column x which has some NaN values then we can convert those NaN to NA by using the command df$x[is.nan(df$x)] x1 x2 df1 df1Outputx1 x2 1 NaN -0.44923302 2 NaN -0.12670027 3 1 0.59120380 4 1 -0.18782341 5 NaN -0.28730385 6 1 0.57412261 7 NaN -0.33620181 8 1 1.37168545 9 NaN -2.24121448 10 NaN 1.05990104 11 1 1.95544957 12 NaN -2.19544854 13 1 ... Read More

How to create boxplot with multiple factor levels using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 04-Mar-2021 18:23:00

5K+ Views

To create a boxplot, we have one factor and one numerical column and the boxplot is created for each category or levels in that factor. Now if we have two factors then the boxplot can be created for both factor levels by passing fill argument in geom_boxplot. This will help us to differentiate between the boxplots for the two factors. Check out the below examples to understand how it works.ExampleConsider the below data frame −Live Demo> x y grp df dfOutput       x              y  grp 1 Female    0.790349405 b ... Read More

How to remove rows in a data.table object with NA's in R?

Nizamuddin Siddiqui
Updated on 04-Mar-2021 18:29:49

2K+ Views

If a row contains missing values then their sum will not finite, therefore, we can use is.finite function with the data.table object to remove the rows with NA’s. For example, if we have a data.table object called DT that contains some rows with NA’s then the removal of those rows can be done by using DT[is.finite(rowSums(DT))].Example1Loading data.table package and creating a data.table object −> library(data.table) > x1 x2 DT1 DT1Output   x1  x2 1:  1   2 2:  NA  4 3:  1   2 4:  NA  5 5:  1   6 6:  1   8 7:  NA  3 8:  1   ... Read More

How to find the mean for multiple columns in an R data frame using mean function not colMeans?

Nizamuddin Siddiqui
Updated on 04-Mar-2021 19:15:04

349 Views

The easiest way to find the column means of an R data frame is to use colMeans function but if we do not want to use it then it can be done with the help of sapply. While using sapply, we need to make sure that we are selecting only numerical columns of the data frame. Check out the below examples to understand how it works.Example1Consider the CO2 data frame in base R −Live Demo> head(CO2, 20)Output  Plant  Type  Treatment  conc  uptake 1   Qn1 Quebec nonchilled  95   16.0 2   Qn1 Quebec nonchilled  175  30.4 3   Qn1 ... Read More

C Program to print the sum of boundary elements of a matrix

Bhanu Priya
Updated on 24-Mar-2021 14:28:37

6K+ Views

Given a matrix, we need to print the boundary elements of the matrix and display their sum.ExampleRefer the matrix given below −Given matrix1 2 3 4 5 6 7 8 9Boundary Matrix1 2 3 4   6 7 8 9Sum of boundary elements: 1 + 2 + 3 + 4 + 6 + 7 + 8 + 9 = 40The logic to find the sum of boundary matrix is as follows −for(i = 0; i

C program to calculate power of a given number

Bhanu Priya
Updated on 24-Mar-2021 14:14:46

19K+ Views

Take two integers from the user for base and exponent and calculate the power as explained below.ExampleConsider the following for writing a C program.Suppose base =3Exponent = 4Power=3*3*3*3AlgorithmFollow the algorithm given below −Step 1: Declare int and long variables. Step 2: Enter base value through console. Step 3: Enter exponent value through console. Step 4: While loop. Exponent !=0    i. Value *=base    ii. –exponent Step 5: Print the result.ExampleThe following program explains how to calculate power of given number in C language.#include int main(){    int base, exponent;    long value = 1;    printf("Enter a base value: ... Read More

How to assign a column value in a data frame based on another column in another R data frame?

Nizamuddin Siddiqui
Updated on 04-Mar-2021 19:18:25

5K+ Views

To assign a column value based on another column, we can use ifelse function. The ifelse function checks whether the value in one column of one data frame matches the value in another column of another data frame by using equal sign (==) and then replace the original value with the new column if there is no match else returns the original value. Check out the below example to understand how it can be done.ExampleConsider the below data frame −Live Demo> x1 x2 df1 df1Output x1 x2 1 3 5 2 3 7 3 ... Read More

How to truncate character vector with three dots after n characters in R?

Nizamuddin Siddiqui
Updated on 04-Mar-2021 19:21:59

231 Views

To truncate character vector with three dots after n characters can be done with the help of str_trunc function of stringr package. For example, if we have a character vector say x and each value containing 10 characters then truncating those values with three dots after 5 characters can be done by using the command str_trunc(x, 8).Example1Live Demo> x1 x1Output[1] "rstuvwxyz" "rstuvwxyz" "abcbefgh" "rstuvwxyz" "ijklmnopq" "ijklmnopq" [7] "ijklmnopq" "rstuvwxyz" "rstuvwxyz" "rstuvwxyz" "rstuvwxyz" "abcbefgh" [13] "rstuvwxyz" "abcbefgh" "abcbefgh" "ijklmnopq" "ijklmnopq" "ijklmnopq" [19] "ijklmnopq" "rstuvwxyz" "rstuvwxyz" "abcbefgh" "abcbefgh" "ijklmnopq" [25] "ijklmnopq" "ijklmnopq" "rstuvwxyz" "rstuvwxyz" "rstuvwxyz" "rstuvwxyz" [31] "rstuvwxyz" "abcbefgh" "abcbefgh" "rstuvwxyz" "rstuvwxyz" ... Read More

How to filter single column of a matrix with column name in R?

Nizamuddin Siddiqui
Updated on 04-Mar-2021 19:25:51

2K+ Views

To filter a single column of a matrix in R if the matrix has column names, we can simply use single square brackets but this will result in a vector without the column name. If we want to use the column name then column name or column number needs to be passed with drop=FALSE argument as shown in the below examples.Example1Live Demo> M1 colnames(M1) M1Output      V1 V2 V3 V4 [1, ]  0  0  1  0 [2, ]  1  1  1  1 [3, ]  0  0  0  0 [4, ]  0  1  1  0 [5, ]  1  1  1 ... Read More

How to highlight a bar in base R histogram?

Nizamuddin Siddiqui
Updated on 04-Mar-2021 08:41:48

585 Views

To highlight a bar in base R histogram, we need to understand the X-axis values and pass the col argument inside hist function appropriately. We just need to put a separate value for the bar that we want to highlight and set the colouring of the rest of the bars to 0 (that is default in base R). Check out the below examples to understand how it works.Example1> x hist(x,col = c(rep(0,5),4,rep(0,5)))OutputExample2> y hist(y,col = c(rep(0,3),4,rep(0,9)))Output

Advertisements