Replicate a Vector to Create Matrix in R

Nizamuddin Siddiqui
Updated on 17-Oct-2020 14:44:04

2K+ Views

The matrix can be created by using matrix function in R and if we want to create a matrix by replicating a vector then we just need to focus on the replication. For example, if we have a vector V and we want to create matrix by replicating V two times then the matrix can be created as matrix(replicate(2,V),nrow=2).Example1 Live DemoV1

Create Random Sample with Values 0 and 1 in R

Nizamuddin Siddiqui
Updated on 17-Oct-2020 14:30:01

2K+ Views

It is known that the random sample can be created by using sample function in R. If we want to create a random sample with values 0 and 1 only then there are three different ways to pass them inside the sample function −Creating a vector of 0 and 1Using 0:1Using c(1,2) directly inside the function.Also, we can set replace argument to TRUE or FALSE based on our requirement.Examples Live Demox1

Align Bars of a Barplot with X-Axis using ggplot2 in R

Nizamuddin Siddiqui
Updated on 17-Oct-2020 14:16:21

2K+ Views

The bar plot is created with geom_bar function but there always exist some space between the bars and the X-axis labels. If we want to reduce that space or completely remove it we need to use scale_y_continuous function by defining expand argument for former and scale_y_continuous(expand=c(0,0)) for latter.Example Live DemoConsider the below data frame −set.seed(888) x

Multiply a Matrix with a Vector in R

Nizamuddin Siddiqui
Updated on 17-Oct-2020 14:13:41

6K+ Views

When we multiply a matrix with a vector the output is a vector. Suppose we have a matrix M and vector V then they can be multiplied as M%*%V. To understand the step-by-step multiplication, we can multiply each value in the vector with the row values in matrix and find out the sum of that multiplication.Example1 Live DemoM1

Replicate Whole Data Frame and Add to Original in R

Nizamuddin Siddiqui
Updated on 17-Oct-2020 14:04:48

1K+ Views

The replicates of a data frame in R can be created with the help of sapply function, to set the number of times we want to repeat the data frame we can use rep.int,times argument. For example, if we have a data frame df and we want to create 5 replicates of df and add them in the original then sapply(df,rep.int,times=5) can be used.Example Live DemoConsider the below data frame −set.seed(151) x1

Find Day of the Year from Dates in R

Nizamuddin Siddiqui
Updated on 17-Oct-2020 14:02:00

8K+ Views

To find the day of the year from dates, we can use yday function of lubridate package. For example, if we have a date or a date of vectors then we simply need to pass that date or the vector inside yday function by using the below syntax −yday(“date”)oryday(“vector_of_date”)Loading lubridate package −library(lubridate)Examplesdate1

Calculate Row Means Excluding NA Values in R Data Frame

Nizamuddin Siddiqui
Updated on 17-Oct-2020 13:59:11

3K+ Views

To find the row means we can use rowMeans function but if we have some missing values in the data frame then na.rm=TRUE argument can be used in the same way as it is used while calculating the means for columns. For example, if we have a data frame df that contains two columns x and y each having some missing values then the row means can be calculated as rowMeans(df,na.rm=TRUE).ExampleConsider the below data frame − Live Demoset.seed(1515) x1

Change Y-Axis Limit for Boxplot in ggplot2 in R

Nizamuddin Siddiqui
Updated on 17-Oct-2020 13:57:17

2K+ Views

One of the most important aspects of a boxplot is Y-axis labels because these labels help us to understand the limit of the variable. Since R generate these labels automatically in a good way, we stick with that but we can change that using coord_cartesian function with ylim as shown in the below example.Example Live DemoConsider the below data frame −set.seed(1212) x

Replace Missing Values with Median in R Data Frame Column

Nizamuddin Siddiqui
Updated on 17-Oct-2020 13:55:12

2K+ Views

To replace missing values with median, we can use the same trick that is used to replace missing values with mean. For example, if we have a data frame df that contain columns x and y where both of the columns contains some missing values then the missing values can be replaced with median as df$x[is.na(df$x)]

Extract Value Based on Index from R Data Frame Column

Nizamuddin Siddiqui
Updated on 17-Oct-2020 13:52:35

3K+ Views

Sometimes we want to figure out which value lies at some position in an R data frame column, this helps us to understand the data collection or data simulation process. For example, if we have a data frame df that contain columns x, y, and z each with 5000 values then we can use df$x[[253]] to find which values lies at 253rd row in column x of data frame df.ExampleConsider the below data frame − Live Demoset.seed(987) x

Advertisements