They are actually no difference between the two commands as they give the same result that is the number of objects in the current workspace. If we have five different type of objects say a data frame, a matrix, a list, a data.table object, and a vector then both the commands will give the names of these objects.ExampleConsider the below objects −Live Demo> x1 x2 df1 df1Outputx1 x2 1 A A 2 D A 3 C D 4 A A 5 B C 6 B D 7 D D 8 D C 9 B D 10 B A 11 D ... Read More
Most people get confused about levels and characters in R, especially the newbies. The difference is that levels specifically define the factor levels of a factor column and the characters are simple the character column that is not a factor or is not used as a factor but can be converted to a factor.ExampleConsider the below data frame −Live Demo> x1 x2 df1 df1Outputx1 x2 1 B B 2 B A 3 D D 4 D C 5 C A 6 D C 7 A D 8 D B 9 D C 10 B B 11 C B 12 D ... Read More
If we want to use unicode characters in the title for a plot created with the help of ggplot2 package then the ggtitle function will be used combined with functions called expression and paste. Using the expression and paste functions, we can write unicode characters for the title of the plot. Check out the below examples to understand how it works.ExampleConsider the below data frame −Live Demo> x y df dfOutput x y 1 3.4501307 6.354644 2 2.1561511 5.349282 3 4.5018653 6.080046 4 2.5512959 5.957558 5 3.6818738 5.749713 ... Read More
If a data frame contains missing value then visualising it in base R is not easily possible but we can make use of visdat package for this purpose. The vis_dat function of visdat package helps to visualize any data frame even if it contains missing values. For example, if a data frame df contains missing value then it can be visualized as vis_dat(df).Example1Consider the below data frame −Live Demo> x1 x2 x3 df1 df1Output x1 x2 x3 1 1 23 10 2 1 23 NA 3 NA NA 10 4 NA NA 10 5 1 24 NA 6 2 22 ... Read More
To create a line for equal values of x and y in scatterplot, we can make use of segments function in base R but this can be done after creating the plot with the help of plot function. The segments function has four arguments, x0, y0, x1, and y1, we need to put the same value in x0 and y0 and the same value in x1 and y1 to draw the appropriate line as shown in the below examples.Example1Live Demo> x xOutput[1] -1.14191974 1.11554154 -0.01255755 1.18841175 1.11300329 -0.69925814 [7] -0.88000117 0.67830803 -0.91237446 -1.14223973ExampleLive Demo> y yOutput[1] -1.69229826 -0.70352587 0.38544874 0.14022473 ... Read More
To plot the confidence interval of the regression model, we can use geom_ribbon function of ggplot2 package but by default it will have dark grey color. It can become transparent with the help of alpha argument inside the same function, the alpha argument can be adjusted as per our requirement but the most recommended value by me is 0.2.ExampleConsider the below data frame −Live Demo> x y df dfOutput x y 1 22.67102 29.37057 2 21.59415 29.54027 3 20.56817 28.27672 4 24.97228 31.38193 5 21.41651 31.86811 6 ... Read More
In Data Analysis, we often required to extract a single value, a single row, or a single column for a specific analysis. For example, if data frame contains column defined as height and weight then we might want to use only height then it can be extracted, this could be a part of a list as well, therefore, extraction from list will be required. If we have a list of data frames then extraction of a column from one of the data frames in the list can be done by using double square brackets for accessing the data frame and ... Read More
To convert a matrix into a color matrix, we can make use of image function. There are multiple ways for assigning the colors but the easiest one might be by defining the minimum and maximum value in the matrix. Also, we can do this by using the shades of a single color as shown in the example 3.Example1Live Demo> M1 M1Output [, 1] [, 2] [, 3] [, 4] [, 5] [1, ] 6 3 5 4 3 [2, ] 9 4 5 2 5 [3, ] 3 2 ... Read More
To create a plot with tick marks but without axes lines, we first need to create the plot without axes and then add the tick marks. This can be done with the help of plot function and axis function in base R. The axis function will help us to decide where do we need the tick marks and the ticks.Example1> plot(1:10,axes=FALSE) > axis(1,c(1:10),col=NA,col.ticks=1)OutputExample2Live Demo> x xOutput[1] 5 2 1 2 1Example> plot(x,axes=FALSE) > axis(1,c(1:5),col=NA,col.ticks=1)Output
If we have a list of data frames and the size of those data frames is same then we might want to combine the lists so that the data frames can be combined. This can be done by using mapply function along with cbind. For example, if we have two lists of data frames defined as List1 and List2 then we can combine them using the command −mapply(cbind, List1, List2, SIMPLIFY=FALSE).ExampleConsider the below data frame −Live Demo> x1 x2 df1 df1Output x1 x2 1 0.2378371 0.51433808 2 0.0638975 -1.66077353 3 0.3987209 0.68480587 ... Read More