Found 2038 Articles for R Programming

How to check if two vectors are exactly same in R?

Nizamuddin Siddiqui
Updated on 11-Nov-2021 05:33:24

6K+ Views

To check if two vectors are exactly same, we can use identical function.For example, if we have two vectors say x and y then we can find whether both of them are exactly same or not by using the command given below −identical(x,y)Check out the below examples to understand the result of identical function for two vectors.Example 1To check if two vectors are exactly same, use the command given below −x1

How to find the ID wise frequency in an R data frame?

Nizamuddin Siddiqui
Updated on 11-Nov-2021 05:32:22

402 Views

To find the ID wise frequency in an R data frame, we can use summarise function of dplyr package after defining the ID with group_by function, also the column for which we want to find the frequency will be placed inside group_by function.Check out the below examples to understand how it can be done.Example 1Following snippet creates a sample data frame −ID

How to find the mean of all columns by group in R?

Nizamuddin Siddiqui
Updated on 11-Nov-2021 05:28:13

1K+ Views

To find the mean of all columns by group, we can use summarise_all function along with mean function after defining the groups with group_by. For example, if we have a data frame called df that contains a grouping column say G and some numerical columns then we can find the mean of all columns by values in grouping column by using the below given command −df%>%group_by(G)%>%summarise_all("mean")Example 1Following snippet creates a sample data frame −Grp

How to find the mean of every n values in an R vector?

Nizamuddin Siddiqui
Updated on 11-Nov-2021 05:21:21

905 Views

To find the mean of every n values in an R vector, we would first need to convert the vector into matrix with number of rows based for n values then we can use colMeans function.For example, if we have a vector called X that contains hundred values then we can find the mean of every ten value by using the command given below −colMeans(matrix(X,10))Example 1To find the mean of every n values in an R vector, use the command given below −x1

How to change the border color of box of a base R plot?

Nizamuddin Siddiqui
Updated on 11-Nov-2021 05:19:01

2K+ Views

To change the border color of box of a base R plot, we can use box function with col argument where we can pass the color other than black because black is the default color. We first need to create the plot using plot function and then box function will be used as shown in the below examples.Example 1To change the border color of box of a base R plot, use the command given below −plot(1:10) box(col="red")OutputIf you execute the above given command, it generates the following output −Example 2To change the border color of box of a base R ... Read More

How to find the most frequent factor value in an R data frame column?

Nizamuddin Siddiqui
Updated on 11-Nov-2021 05:17:15

12K+ Views

To find the most frequent factor value in an R data frame column, we can use names function with which.max function after creating the table for the particular column. This might be required while doing factorial analysis and we want to know which factor occurs the most.Check out the below examples to understand how it can be done.Example 1Following snippet creates a sample data frame −Factor_1

How to extract the first row of each matrix stored in a list in R?

Nizamuddin Siddiqui
Updated on 11-Nov-2021 05:15:43

839 Views

To extract the first row of each matrix stored in a list in R, we can use lapply function. For example, if we have a list called LIST that contains some matrices then we can find the first row of each matrix by using the command given below − lapply(LIST,'[',1,)Check out the below given example to understand how it can be done.ExampleFollowing snippet creates a list of matrices −M1

How to extract the outliers of a boxplot in R?

Nizamuddin Siddiqui
Updated on 11-Nov-2021 05:12:10

2K+ Views

To extract the outliers of a boxplot, we can use out function along with the boxplot function. For example, if we have a vector called X which contains some outliers then we can extract those outliers by using the command given below − boxplot(df$X, plot=FALSE)$outThis command will not create a plot as plot is set to FALSE.ExampleFollowing snippet creates a sample data frame −df=data.frame(x=rlnorm(25)) dfThe following dataframe is created −      x 1  0.5699270 2  3.5812629 3  0.3507882 4  0.1400328 5  0.7239948 6  2.5494114 7  3.1243611 8  5.3207739 9  0.1672539 10 7.6235529 11 0.4950263 12 1.1713592 13 1.6590328 14 ... Read More

How to remove rows that contains coded missing value for all columns in an R data frame?

Nizamuddin Siddiqui
Updated on 11-Nov-2021 05:09:37

271 Views

Sometimes missing values are coded and when we perform analysis without replacing those missing values the result of the analysis becomes a little difficult to interpret, especially it is difficult to understand by first time readers.Therefore, we might want to remove rows that contains coded missing values. For this purpose, we can replace the coded missing values with NA and then replace the rows with NA as shown in the below given examples.Example 1Following snippet creates a data frame, if missing values are coded as 1 −x1

How to replace zero with previous value in an R data frame column?

Nizamuddin Siddiqui
Updated on 11-Nov-2021 05:05:34

703 Views

To replace zero with previous value in an R data frame column, we can use na.locf function of zoo package but to apply this we first need to replace the zero values with NA.For example, if we have a data frame called df that contains a column say Rate then we can use the below commands to replace 0 values in Rate with previous value by using the below given commands − df$Rate[df$Rate==0]

Advertisements