Found 2038 Articles for R Programming

How to create a line chart with mean and standard deviation using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 05-Feb-2021 10:28:10

2K+ Views

Sometimes we have mean and standard deviation given for groups or factors, these are generally obtained from previous research studies and is referred to as the secondary data. In this case. the line chart with mean and standard deviation using ggplot2 can be created by defining the minimum and maximum inside geom_error function of ggplot2 package, where the difference between mean and standard deviation defines the standard deviation if the minimum is set as mean minus one standard deviation and the maximum is set as mean plus one standard deviation.ExampleConsider the below data frame − Live DemoGroup

How to find the mean of multiple columns based on a character column in R?

Nizamuddin Siddiqui
Updated on 05-Feb-2021 10:24:29

246 Views

If we have a character column that means we are more likely to have duplicated values in that column hence finding the mean of numerical columns based on the values in character column cannot be done directly. For this purpose, we can use aggregate function as shown in the below examples.Example1Consider the below data frame − Live Demoset.seed(214) x1

How to convert matrix rows into a list in R?

Nizamuddin Siddiqui
Updated on 05-Feb-2021 10:17:13

1K+ Views

Depending on our objective, a matrix rows might be needed to converted into a list that means each row will be an element of the list. This can be done by using the function as.list but firstly we need to convert the matrix into data frame after transposing. For example, if we have a matrix called M then it’s rows will be converted to a list using the command writtem below −as.list(data.frame(t(M)))Example1 Live DemoM1

How to create bins for a continuous vector in R?

Nizamuddin Siddiqui
Updated on 05-Feb-2021 10:11:01

3K+ Views

To create the bins for a continuous vector, we can use cut function and store the bins in a data frame along with the original vector. The values in the cut function must be passed based on the range of the vector values, otherwise, there will be NA’s in the bin values. For example, if we have a vector that contains 0.55 and we do not use 0 in the cut function then the first bin will be NA. Check out the below examples to understand how to properly do it.Example1 Live Demox1

How to convert a character data frame to numeric data frame in R?

Nizamuddin Siddiqui
Updated on 05-Feb-2021 10:08:30

2K+ Views

Sometimes numerical values are recorded as character values and we need to convert them to numeric type before starting our analysis. This is also possible for a whole data frame in R. Therefore, we can use sapply function to convert the columns of the data frame to numeric type and save the output in a data frame by reading it with as.data.frame.Example1Consider the below data frame − Live Demox

How to create a boxplot without frame in base R?

Nizamuddin Siddiqui
Updated on 05-Feb-2021 10:05:39

616 Views

The boxplot in base R is covered with a box and that box is called a frame. We can get rid of that frame by using frame argument while creating the boxplot. For example, if we have a vector called x and we want to create the boxplot without frame then it can be done by using the command boxplot(x,frame=F). This will remove all the sides of the boxplot except the Y−axis labels because this will help us to understand the distribution of the variable.Example Live Demox

How to create the plot of a vector that contains missing values by adding the missing values in base R?

Nizamuddin Siddiqui
Updated on 05-Feb-2021 10:03:42

259 Views

If there are missing values in a vector then the plot of such vector will not have all the values, only the non−missing values will be shown. If we want to create the plot by adding the missing values in the plot then we need to define the X−axis for the length of the vector and the Y−axis with the actual vector using cbind but missing values will be omitted as shown in the below example.Example Live Demox

How to print a large number of values in R without index?

Nizamuddin Siddiqui
Updated on 05-Feb-2021 10:00:33

851 Views

Whenever we print any vector then the left-hand side of the R window shows indexing, even if we have one value in the vector the index will be there. For example, if we print a vector that has value equal to 2 then the print output will be [1] 2, here [1] represents the index. If we want to print the vector without index then cat function can be used as shown in the below examples.Example1 Live Demox1

How to round exponential numbers in R?

Nizamuddin Siddiqui
Updated on 05-Feb-2021 09:56:22

917 Views

The exponential numbers are also called scientific numbers and these numbers have exponent representation by the letter e. For example, a number 12340000 can be represented as 1.234e + 107. We can round this to 1.2e + 107 and in R it can be done with the help of singif function.Example1 Live Demox1

How to create a vector with all dates in a particular year in R?

Nizamuddin Siddiqui
Updated on 05-Feb-2021 09:33:50

685 Views

We know that some years are leap years and some are normal years. The leap years have 366 days and the normal years have 365 days. To create a vector with all dates in a particular year we can use first date and the last date of the year by reading them with as.Date and creating a sequence with seq function. Check out the below examples to understand how it is done.Example1Creating a vector with dates in year 2020 − Live Demoseq(as.Date("2020-01-01"), as.Date("2020-12-31"), by="1 day")Output[1] "2020−01−01" "2020−01−02" "2020−01−03" "2020−01−04" "2020−01−05" [6] "2020−01−06" "2020−01−07" "2020−01−08" "2020−01−09" "2020−01−10" [11] "2020−01−11" "2020−01−12" "2020−01−13" "2020−01−14" ... Read More

Advertisements