Found 33676 Articles for Programming

How to create a chart by covering the area of the plot from bottom left to upper right in R?

Nizamuddin Siddiqui
Updated on 14-Oct-2020 14:39:29

109 Views

The plot area in plot window is fixed by default and we can create a lint chart with extended width so that the chart covers the area of the plot from bottom left to upper right. This can be done by using very large width of the line chart with the help of lwd argument.Consider the below vector and create the very wide line chart to cover the plot area −Examplex

How to find the sum of anti-diagonal elements in a matrix in R?

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

584 Views

The anti-diagonal elements in a matrix are the elements that form straight line from right upper side to right bottom side. For example, if we have a matrix as shown below −1 2 3 4 5 6 7 8 9then the diagonal elements would be 1, 5, 9 and the anti-diagonal elements would be 3, 5, 7.To find the sum of these anti-diagonal elements, we can use apply function.Example Live DemoM1

How to find the correlation coefficient between rows of two data frames in R?

Nizamuddin Siddiqui
Updated on 18-Oct-2020 14:18:17

867 Views

It is common the find the correlation coefficient between columns of an R data frame but we might want to find the correlation coefficient between rows of two data frames. This might be needed in situations where we expect that there exists some relationship row of an R data frame with row of another data frame. For example, row of an R data frame showing buying trend of a customer in one year and the same row of the other data frame showing buying trend of the same customer in another year.Consider the below data frame −Example Live Demox1Read More

How to deal with warning message `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. in R while creating a histogram?

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

8K+ Views

The default value for bins is 30 but if we don’t pass that in geom_histogram then the warning message is shown by R in most of the cases. To avoid that, we can simply put bins=30 inside the geom_histogram() function. This will stop showing the warning message.Consider the below data frame −x

How to save an R data frame as txt file?

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

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

758 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

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

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

703 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

333 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

Advertisements