Found 2038 Articles for R Programming

How to save an R data frame as txt file?

Nizamuddin Siddiqui
Updated on 18-Oct-2020 14:16:06

2K+ Views

If we want to use a data frame created in R in the future then it is better to save that data frame as txt file because it is obvious that data creation takes time. This can be done by using write.table function. For example, if we have a data frame df then we can save it as txt file by using the code write.table(df,"df.txt",sep="\t",row.names=FALSE)Consider the below data frame −Example Live Demoset.seed(111) x1

How to create a string vector with numbers at the end in R?

Nizamuddin Siddiqui
Updated on 18-Oct-2020 14:15:08

568 Views

If we want to create a categorical vector with all unique values representing strings with numbers at the end then paste0 function can help us in the same. For example, if we want to create a vector for ID up to 10 as ID1, ID2, ID3, ID4, ID5, ID6, ID7, ID8, ID9, and ID10 then it can be done as paste0("ID",seq(1:10)).Example Live Demox1

How to replace NA values in columns of an R data frame form the mean of that column?

Nizamuddin Siddiqui
Updated on 18-Oct-2020 14:05:48

7K+ Views

In the whole world, the first step people teach to impute missing values is replacing them with the relevant mean. That means if we have a column which has some missing values then replace it with the mean of the remaining values. In R, we can do this by replacing the column with missing values using mean of that column and passing na.rm = TRUE argument along with the same.Consider the below data frame −Example Live Demoset.seed(121) x

How to combine year, month, and day column in an R data frame?

Nizamuddin Siddiqui
Updated on 18-Oct-2020 14:04:22

6K+ Views

Sometimes date variable is recorded in three different columns representing year, month, and day instead of a single column as date. Therefore, we need to combine these three columns and create a single column. This can be done by using paste function and define the values with as.Date.Consider the below data frame −Example Live DemoYear

How to create a subset of a matrix in R using row names?

Nizamuddin Siddiqui
Updated on 18-Oct-2020 14:02:04

473 Views

When we create a matrix in R, the row names and column names are not defined but we can define them separately. If we want to take a subset of rows of a matrix then row numbers can be used within single square brackets but if we want to do it with the names then we need to specify those names.Example Live DemoM1

How to create varying width bar chart using barplot function in R?

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

237 Views

The barplot function create the bars of equal width but if we have equal or unequal width values for each bar then we can use width within the barplot function. Thus, the newly generated barplot will have different width of the bars. For example, if we the width are defined for four categories as 0.25 each then each bar will be of equal width and if they vary as 0.30, 0.40, 0.20, 0.45 then the width of the bars will be different based on these widths.Consider the below vector x and the corresponding width vector −x

How to change the X-axis labels for boxplots created by using boxplot function in R?

Nizamuddin Siddiqui
Updated on 14-Oct-2020 12:14:30

720 Views

When we create boxplots for multiple categories in R using boxplot function, by default the X-axis labels are represented by numbers. But we might want to express the categories by their name. In this situation, we can use names argument along with the boxplot function.Consider the below vectors that represent different categories and create the boxplot for these categories −ExampleClass1

How to change the color of bars in histogram for values that are greater than 0 or less than 0 in R?

Nizamuddin Siddiqui
Updated on 14-Oct-2020 12:11:11

193 Views

Although, the histogram represents the distribution of a complete set of values but we might want to visualize that histogram based on the division of some threshold value. For example, we might want to visualize the histogram with different bars that have values greater than 1 or less than 1. This will help us to understand the distribution of the values in whole data set that lies above or below certain value. For this purpose, we can simply use hist function with col argument to change the color of the values that are greater than or less than a fixed ... Read More

How to find residual variance of a linear regression model in R?

Nizamuddin Siddiqui
Updated on 14-Oct-2020 12:08:54

5K+ Views

The residual variance is the variance of the values that are calculated by finding the distance between regression line and the actual points, this distance is actually called the residual. Suppose we have a linear regression model named as Model then finding the residual variance can be done as (summary(Model)$sigma)**2.Examplex1

How to display the values of two columns of an R data frame separately in a plot?

Nizamuddin Siddiqui
Updated on 14-Oct-2020 12:03:50

885 Views

In general, the scatterplot is used to visualize the relationship between two columns of an R data frame but if we want to display the two columns separately not as a pair then we need to use matplot function. This function will create a plot for all the values in the two columns and represent them by their column number.Consider the below data frame −Example Live Demoset.seed(222) x

Advertisements