Found 2038 Articles for R Programming

How to delete matrix rows if a particular column value satisfies some condition in R?

Nizamuddin Siddiqui
Updated on 17-Mar-2021 06:58:30

719 Views

To delete matrix rows if a particular column satisfies some condition, we can use subsetting with single square brackets and take the subset of the matrix based on the condition. For example, if we have a matrix M and want to delete rows if column first of M do not contain value 5 then we can use the command M[M[,1]==5,].ExampleConsider the below matrix − Live DemoM1

How to filter rows by excluding a particular value in columns of the R data frame?

Nizamuddin Siddiqui
Updated on 17-Mar-2021 06:49:48

3K+ Views

To filter rows by excluding a particular value in columns of the data frame, we can use filter_all function of dplyr package along with all_vars argument that will select all the rows except the one that includes the passed value with negation. For example, if we have a data frame called df and we want to filter rows by excluding value 2 then we can use the commanddf%>%filter_all(all_vars(.!=2))ExampleConsider the below data frame − Live Demox1

How to set the range for boxplot in base R?

Nizamuddin Siddiqui
Updated on 17-Mar-2021 06:46:24

2K+ Views

By default, R considers the vector or the data frame column values. But if we want to set the range for boxplot in base R, we can use ylim argument within the boxplot function. For example, if we have a vector called x that contains values starting from 21 to 50 and we want to have the range in the boxplot starting from 1 to 100 then we can use the commandboxplot(x,ylim=c(1,100))Example Live Demox

How to separate string and a numeric value in R?

Nizamuddin Siddiqui
Updated on 17-Mar-2021 06:43:49

913 Views

To separate string and a numeric value, we can use strplit function and split the values by passing all type of characters and all the numeric values. For example, if we have a data frame called df that contains a character column Var having concatenated string and numerical values then we can split them using the below command −strsplit(df$Var,split="(?

How to find mean for x number of rows in a column in an R matrix?

Nizamuddin Siddiqui
Updated on 17-Mar-2021 06:41:44

97 Views

To find the mean for x number of rows in a column, we can use colMeans function by accessing the column and providing the number of rows. For example, if we have a matrix called M that contains 20 rows and 5 columns then we can find the mean of column 5 for 5 number of rows can use the command colMeans(matrix(M[,5],nrow=5))ExampleConsider the below data frame − Live DemoM1

How to divide each value in a data frame by column total in R?

Nizamuddin Siddiqui
Updated on 17-Mar-2021 06:37:36

2K+ Views

To divide each value in a data frame by column total, we can use apply function and define the function for the division. For example, if we have a data frame called df that contains five columns then we can divide each value of these columns by column total using the command apply(df,2,function(x){x/sum(x)})ExampleConsider the below data frame − Live Demox1

How to find the sum by two factor columns in R?

Nizamuddin Siddiqui
Updated on 17-Mar-2021 06:31:47

2K+ Views

To find the sum by two factor columns, we can use aggregate function. This is mostly required when we have frequency/count data for two factors. For example, if we have a data frame called df that contains two factor columns say f1 and f2 and one numerical column say Count then the sum of Count by f1 and f2 can be calculated by using the command aggregate(Count~f1+f2,data=df,sum).ExampleConsider the below data frame − Live Demox1

How to convert an array into a matrix in R?

Nizamuddin Siddiqui
Updated on 17-Mar-2021 06:25:25

4K+ Views

To convert an array into a matrix in R, we can use apply function. For example, if we have an array called ARRAY that contains 2 array elements then we can convert this array into a single matrix using the command apply(ARRAY,2,c). We need to understand the dimension of the array making the conversion otherwise the output will not be as expected.ExampleConsider the below array − Live Demox1

How to add name to data frame columns in R?

Nizamuddin Siddiqui
Updated on 17-Mar-2021 06:18:47

1K+ Views

A data frame can be created by using data.frame function but in this case R generates the column names using the values we pass for the data. For example, if we pass a probability distribution as shown in the below examples then its name will be there. If we want to change those names then setNames function can be used along with the data frame name.Example Live Demodf1

How to find the position of odd numbers in an R vector?

Nizamuddin Siddiqui
Updated on 17-Mar-2021 06:13:00

2K+ Views

To find the position of odd numbers in an R vector, we can find the position of values that are divisible by 2 with the help of which function. For example, if we have a vector called x then we can find the position of odd numbers using the command which(x%%2==1). Check out the below examples to understand how it works.Example Live Demox1

Advertisements