Server Side Programming Articles - Page 1417 of 2650

How to compare two columns in an R data frame for an exact match?

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

7K+ Views

Sometimes analysis requires the user to check if values in two columns of an R data frame are exactly the same or not, this is helpful to analyze very large data frames if we suspect the comparative values in two columns. This can be easily done with the help of ifelse function.ExampleConsider the below data frame − Live Demox1

How to create a scatterplot with regression line using ggplot2 with 0 intercept and slope equals to 1 in R?

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

331 Views

To create a regression line with 0 intercept and slope equals to 1 using ggplot2, we can use geom_abline function but we need to pass the appropriate limits for the x axis and y axis values. For example, if we have two columns x and y in a data frame df and both have ranges starting from -1 to 1 then the scatterplot with regression line with 0 intercept and slope equals to 1 can be created as −ggplot(df,aes(x,y))+geom_point()+geom_abline()+lims(x=c(-1,1),y=c(-1,1))ExampleConsider the below data frame − Live Demox

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

1K+ 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

1K+ 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

4K+ 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

2K+ 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

147 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

228 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

6K+ 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

Advertisements