Found 2038 Articles for R Programming

How to find the row product of a matrix in R?

Nizamuddin Siddiqui
Updated on 16-Mar-2021 10:16:29

974 Views

To find the row product of a matrix in R, we can use apply function along with prod function. For example, if we have a matrix called M then to find the row product of a matrix we can use the command apply(M,1,prod). We need to remember that the output will be a vector not matrix. Check out the below examples to understand how to perform the row product of the matrix.ExampleConsider the below matrix − Live DemoM1

How to convert negative values in a matrix to 0 in R?

Nizamuddin Siddiqui
Updated on 16-Mar-2021 10:10:24

4K+ Views

To convert negative values in a matrix to 0, we can use pmax function. For example, if we have a matrix called M that contains some negative and some positive and zero values then the negative values in M can be converted to 0 by using the command pmax(M,0).ExampleConsider the below data frame − Live DemoM1

How to minus one column from another in an R matrix?

Nizamuddin Siddiqui
Updated on 16-Mar-2021 07:44:09

5K+ Views

To minus one column from another in an R matrix, we first need to read the matrix as a data frame using as.data.frame then find minus the columns using minus sign and accessing the column of the data frame. To understand how it can be done look at the steps in below examples.ExampleConsider the below data frame − Live DemoM1

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

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 order by using the command t(apply(df,1,sort)).Example1Consider the below data frame − Live Demox1

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

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 below data frame − Live DemoID

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

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

11K+ 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 sapply(LIST,"[[",1).Example1Consider the below data frame − Live DemoList1

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

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 monthly average can be found by using the command −aggregate(x~Month+Year,df,mean)Example1Consider the below data frame − Live DemoDate1

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

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 to 1 and 0 using the command ifelse(df$x=="Yes",1,0).Example1Consider the below data frame − Live DemoAgree

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

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

117 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 use the command nchar(gsub("[^i]","",x)) and the output will be 1 1 1 because first I’s in India and Indonesia will not be considered as they are in uppercase.Example1 Live Demox1

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

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 then we can find the mean squared error using the command mean((df$y-predict(M))^2).Example1Consider the below data frame − Live Demox1

Advertisements