Nizamuddin Siddiqui has Published 2307 Articles

How to sort each row of an R data frame in increasing order?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 16-Mar-2021 07:35:06

2K+ Views

To sort each row of an R data frame in increasing order, we can use apply function for sorting the columns and then transpose the output. For example, if we have a data frame called df that contains 5 columns then each row of df can be sorted in increasing ... Read More

How to check if a data frame column contains duplicate values in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 16-Mar-2021 07:25:26

1K+ Views

To check if a data frame column contains duplicate values, we can use duplicated function along with any. For example, if we have a data frame called df that contains a column ID then we can check whether ID contains duplicate values or not by using the command −any(duplicated(df$ID))Example1Consider the ... Read More

How to extract only first sub-element from a list in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 06-Mar-2021 14:15:37

10K+ Views

To extract only first element from a list, we can use sapply function and access the first element with double square brackets. For example, if we have a list called LIST that contains 5 elements each containing 20 elements then the first sub-element can be extracted by using the command ... Read More

How to find the monthly average from different date data in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 06-Mar-2021 14:15:10

2K+ Views

To find the monthly average from different date data, we first need to extract the months and year from date column and then find the average using aggregate function. For example, if we have a data frame called df which has three columns say x, month, and year then the ... Read More

How to change the code "Yes" to 1 in an R data frame column?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 06-Mar-2021 14:07:30

13K+ Views

To change the code “Yes” to 1, we can use ifelse function and set the Yes to 1 and others to 0. For example, if we have a data frame called df that contains a character column x which has Yes and No values then we can convert those values ... Read More

How to find the count of a particular character in a string vector in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 06-Mar-2021 14:02:00

116 Views

To find the count of a particular character in a string vector we can use nchar function along with gsub. For example, if we have a vector called x that contains string such India, Russia, Indonesia then we can find the number of times character i occurred then we can ... Read More

How to find the mean squared error for linear model in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 06-Mar-2021 14:01:33

3K+ Views

To find the mean squared error for linear model, we can use predicted values of the model and find the error from dependent variable then take its square and the mean of the whole output. For example, if we have a linear model called M for a data frame df ... Read More

How to create a data frame column with letters of both size in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 06-Mar-2021 13:57:25

299 Views

To create a data frame column with letters of both sizes, we can simply use the letters function and LETTERS function, the first one corresponds to lowercase letters and the latter corresponds to uppercase letters with single square brackets as shown in the below examples.Example1 Live Demodf1

How to convert a matrix to binary matrix in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 06-Mar-2021 13:55:48

1K+ Views

To convert a matrix to binary matrix, we can use as.matrix function and converting the matrix to logical matrix then adding 0 to the values greater than 0. For example, if we have a matrix called M then it can be converted to a binary matrix using the command −as.matrix((MRead More

How to find the index of the nearest smallest number in an R data frame column?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 06-Mar-2021 13:55:20

142 Views

To find the index of the nearest smallest number in an R data frame column, we can use which function along with subsetting for the value for which we want to find the index of the nearest smallest number. To understand how it can be done check out the below ... Read More

Advertisements