Found 2038 Articles for R Programming

How to convert a time series object to a vector in R?

Nizamuddin Siddiqui
Updated on 07-Dec-2020 06:07:11

2K+ Views

To convert a time series object into a vector, we just need to read that object with as.numeric and store it in some other object or in the same object. For example if we have a time series object x then it can be converted to a vector by using x

How to convert multiple columns in an R data frame into a single numerical column along with a column having column names as factor?

Nizamuddin Siddiqui
Updated on 07-Dec-2020 06:04:55

821 Views

When we receive data from any source, it is highly likely that it would not be a perfect data set for the intended analysis, therefore, we need to perform some cleaning or mining based on the characteristics of the data. For example, if we have a column name of a data frame as factor levels of a numerical variable then we might want to convert that data frame in such a way that numerical values are stored in a single column and the column names are stored in another column that will represent a factor so that we can apply ... Read More

How to perform one-way anova with unequal sample sizes in R?

Nizamuddin Siddiqui
Updated on 07-Dec-2020 05:59:43

907 Views

To perform the one-way anova with sample sizes having different sizes we can use aov function. Suppose we have a categorical column defined as Group with four categories and a continuous variable Response both stored in a data frame called df then the one-way anova can be performed as −aov(Response~Group,data=df)ExampleConsider the below data frame − Live DemoGroup

How to generate standard normal random numbers in R?

Nizamuddin Siddiqui
Updated on 07-Dec-2020 05:54:52

3K+ Views

A standard normal distribution is the type of distribution that has mean equals to zero with standard deviation 1. If we want to generate standard normal random numbers then rnorm function of R can be used but need to pass the mean = 0 and standard deviation = 1 inside this function.Example Live Demornorm(10, 0, 1)Output[1] 0.6936607 -0.7967657 -2.7544428 0.2688767 0.5278463 -1.5387568 [7] 1.1716632 -1.5033895 0.8112929 -1.0101065Example Live Demornorm(50, 0, 1)Output[1] 2.58246666 -0.53083341 -0.57343343 1.08172756 1.30341849 -0.07440422 [7] -0.41869305 -0.96227706 -0.46899119 1.55428279 0.09162738 -0.96027221 [13] -0.84735327 -1.74949782 0.58541758 0.23117630 0.47402479 -0.72453853 [19] 0.07171564 1.13088794 0.18735157 0.25091758 -1.34728315 -0.39768159 [25] -0.38109955 -0.34019286 -1.51778561 ... Read More

How to remove list elements by their name in R?

Nizamuddin Siddiqui
Updated on 07-Dec-2020 05:53:06

1K+ Views

Data analysis not only includes mathematical operations, we have to spend a lot of time in data cleaning, data mining, feature engineering etc. One operation could be removing list elements by their name if they are not helpful to achieve our objective. The removal of the elements by using their names can be done with the help of which function.ExampleConsider the below list  Live DemoList1

How to create a line chart using ggplot2 with larger width in R?

Nizamuddin Siddiqui
Updated on 07-Dec-2020 05:50:43

93 Views

The width of the line chart can be increased by using size argument inside geom_line aesthetics of ggplot2. For example, if we have a data frame df that contains two numerical columns x and y, and we want to create a line chart between the two with larger width then it can be done as −ggplot(df)+geom_line(aes(x,y,size=2))ExampleConsider the below data frame − Live Demox

How to save list where each element contains equal number of values to a text file in R?

Nizamuddin Siddiqui
Updated on 07-Dec-2020 05:49:00

146 Views

If we want to save a list to a text file then first step would be converting that list to a data frame then write.table function can be used for saving. For example, if we have a list defined as LIST and it has elements each containing 50 values then we can convert it to a data frame as −LIST_df=as.data.frame(do.call(cbind,LIST))Now we can save it as −write.table(LIST_df,"LIST.txt")ExampleConsider the below list − Live Demox1

How to set comma as decimal separator in R?

Nizamuddin Siddiqui
Updated on 07-Dec-2020 05:45:57

5K+ Views

In European countries, a comma is used to separate the integral part of a number from the decimal part. Thus, we might want to create data or perform calculations with comma as decimal separator. In R, we can do this by just using the code options(OutDec=", "). Once we will type this in R console, all the numerical values with decimals will be printed with commas in place of dots.Example Live Demooptions(OutDec=", ") rnorm(10)Output[1] 0, 14421957 -0, 24152088 -0, 05215867 -0, 40577010 0, 19806357 -1, 49349808 [7] 0, 91085263 0, 43550033 2, 64009603 1, 17177332Example Live Demornorm(50)Output[1] -0, 56186368 -2, 11404777 0, ... Read More

How to generate a probability density distribution from a set of observations in R?

Nizamuddin Siddiqui
Updated on 07-Dec-2020 05:44:08

3K+ Views

The probability density distribution is the synonym of probability density function. It is a function that defines the density of a continuous random variable. In R, we can use density function to create a probability density distribution from a set of observations.Example Live Demox1

How to divide row values of a numerical column based on categorical column values in an R data frame?

Nizamuddin Siddiqui
Updated on 07-Dec-2020 05:38:51

319 Views

If we have a categorical column that has two or more categories and a numerical column then we might want to divide the one category numerical value from other category numerical value. This can be done by using divide sign / but we need to use the proper subset of the values.ExampleConsider the below data frame − Live Demox1

Advertisements