Programming Articles - Page 890 of 3363

How to find the smallest number in an R data frame column excluding values zero or less?

Nizamuddin Siddiqui
Updated on 09-Nov-2021 07:06:46

3K+ Views

To find the smallest number in an R data frame column excluding values zero or less, we can use min function along with subsetting of values greater than 0 through single square brackets.For example, if we have a data frame called df that contains a column say X then the smallest number excluding values zero or less can be found by using the below command −min(df[df$X>0,1])Example 1Following snippet creates a sample data frame −x

How to select data frame columns based on their class in R?

Nizamuddin Siddiqui
Updated on 09-Nov-2021 07:10:06

613 Views

To select data frame columns based on their class in R, we can follow the below steps −First of all, create a data frame or consider an inbuilt data set.Then, use select_if function of dplyr package with class function.Example 1str(CO2)OutputOn executing, the above script generates the below output(this output will vary on your system due to randomization) −Classes ‘nfnGroupedData’, ‘nfGroupedData’, ‘groupedData’ and 'data.frame':84 obs. of 5 variables: $ Plant : Ord.factor w/ 12 levels "Qn1"

How to create a plot of empirical distribution in R?

Nizamuddin Siddiqui
Updated on 09-Nov-2021 07:04:55

3K+ Views

The empirical distribution can be found by using the function ecdf and if we want to create a plot of empirical distribution then plot function will be used.For example, if we have a vector called X then plot of empirical distribution can be created by using the below command −plot(ecdf(X))Example 1To create a plot of empirical distribution in R, use the code given below −x

How to create sequential index value for binary column assigning 0 to FALSE values in data.table object in R?

Nizamuddin Siddiqui
Updated on 09-Nov-2021 07:04:07

135 Views

To create sequential index value for binary column assigning 0 to FALSE values in data.table object in R, we can follow the below steps: −First of all, create a data.table object with binary column.Then, use rle function along with sequence and lengths function to create sequential index column.ExampleCreate the data.table objectLet’s create a data.table object as shown below −library(data.table) x

How to repeat a whole matrix in R?

Nizamuddin Siddiqui
Updated on 09-Nov-2021 07:01:13

281 Views

To repeat a whole matrix in R, we can follow the below steps −First of all, create a matrix.Then, use rep function to repeat the matrix.ExampleCreate the matrixLet’s create a matrix as shown below −M

How to find the dot product of two matrices in R?

Nizamuddin Siddiqui
Updated on 09-Nov-2021 07:06:13

2K+ Views

To find the dot product of two matrices in R, we can use dot function of geometry package.For Example, if we have two matrices say matrix1 and matrix2 then we can use the dot product of these two matrices by using the below given command −dot(matrix1,matrix2)Example 1Following snippet creates a sample matrix −M1

How to separate first text value and the remaining text in R data frame column values?

Nizamuddin Siddiqui
Updated on 09-Nov-2021 06:55:19

226 Views

To separate a first text value and the remaining text in R data frame column values, we can follow the below steps −First of all, create a data frame.Then, use str_split function from stringr package to separate first text value and the remaining text.ExampleCreate the data frameLet’s create a data frame as shown below −Names

Extract a particular level from factor column in an R data frame.

Nizamuddin Siddiqui
Updated on 09-Nov-2021 06:50:59

2K+ Views

To extract a particular level from factor column in an R data frame, we can use levels function with the number of the factor.For Example, if we have a data frame called df that contains a factor column say F then we can find the third level in F by using the below given command −levels(df$F)[2]Example 1Following snippet creates a sample data frame −Grp

How to find the count of each category in a data.table object column in R?

Nizamuddin Siddiqui
Updated on 09-Nov-2021 06:46:59

520 Views

To find the count of each category in a data.table object column in R, we can follow the below steps −First of all, create a data.table object.Then, use summarise function of dplyr package after grouping along with n.ExampleCreate the data.table objectLet’s create a data.table object as shown below −library(data.table) factor

How to deal with glm.fit error “NA/NaN/Inf” for logistic regression model in R?

Nizamuddin Siddiqui
Updated on 09-Nov-2021 07:02:40

5K+ Views

When we create a general linear model for logistic regression model, we need to specify the distribution family as binomial. The error “NA/NaN/Inf” occurs when we do not specify the distribution family. Hence, family="binomial" needs to be used inside glm function while creating the logistic regression model.Example 1Following snippet creates a sample data frame −iv1

Advertisements