To add a variable description in R, we can use comment function and if we want to have a look at the description then structure call of the data frame will be used. For example, if we have a data frame say df that contains a column x then we can describe x by using the command comment(df$x)
To format all decimal places in an R vector and data frame, we can use formattable function of formattable package where we can specify the number of digits after decimal places. For example, if we have a numerical vector say x then the values in x can be formatted to have only 2 decimal places by using the command formattable(x,format="f",digits=2).Example1Loading formattable package −library(formattable) Live Demox1
To create multiple bar plots for varying categories with same width bars using ggplot2, we would need to play with width argument inside geom_bar function to match the width of the bars in each bar plot. The best way to do this would be setting the larger ones to 0.25 and the shorter ones to 0.50.ExampleConsider the below data frame − Live Demox1
To find the high leverage values for a regression model, we first need to find the predicted values or hat values that can be found by using hatvalues function and then define the condition for high leverage and extract them. For example if we have a regression model say M then the hat values can be found by using the command hatvalues(M), now to find the high leverage values that are greater than 0.05 can be found by using the below code −which(hatvalues(M)>0.05)Example1Consider the below data frame − Live Demox1
To apply multiple conditions to a data frame, we can use double and sign that is &&. For example, if we have a data frame called df that contains three columns say x, y, z and we want to add a value to all columns if first element in z equals to 5 then it can be done by using the command −if(df$x && df$y && df$y == 5){ df$x = df$x+10 df$y = df$y+10 df$z = df$z+10 }Example1Consider the below data frame − Live Demox1
To create a bar plot in base R, we can use the function barplot and pass the vector or column of the data frame for which we want to create the bar plot but the bars created by using barplot by default has grey color. Therefore, if we want to create an empty bar plot then setting the color of bars to NA will make the plot an empty bar plot.Example1x
To check if a time series is stationary, we can use Dickey-Fuller test using adf.test function of tseries package. For example, if we have a time series object say TimeData then to check whether this time series is stationary or not we can use the command adf.test(TimeData).Example1 Live Demox1
To deal with Error: stat_count() can only have an x or y aesthetic, we need to pass the stat="identity" argument inside geom_bar function. Since we do not pass the count for bars and a bar graph can only contain only count variable, hence stat="identity" is needed so that geom_bar considers only one variable in aes for counting. Check out the below example to understand the difference.ExampleConsider the below data frame − Live Demofactor
To create data frame using nested list elements, we would need to unlist the list elements and store them in a matrix then read as a data frame using data.frame function. For example, if we have a nested called LIST then the data frame can be created by using the command −data.frame(matrix(unlist(LIST),ncol=”No of columns we want”,byrow=F))Check out the below example to understand how it works.Example Live DemonestedList
To subset an R data frame based on string match in two columns with OR condition, we can use grepl function with double square brackets and OR operator |. For example, if we have a data frame called df that contains two string columns say x and y then subsetting based on a particular string match in any of the columns can be done by using the belowSyntaxdf[grepl("text",df[["x"]])|grepl("text",df[["y"]]),]Check out the below examples to understand how it works.Example1Consider the below data frame − Live Demof1