Programming Articles - Page 1767 of 3363

How to find the total number of rows per group combination in an R data frame?

Nizamuddin Siddiqui
Updated on 24-Aug-2020 11:40:09

292 Views

An R data frame that contains two or more factor columns then there are a greater number of combinations for the number of factors, obviously, if the number of factors is large with large number of levels then the combination of levels of the factors is also large. To find total number of rows per group combination we can use transform function.ExampleConsider the below data frame − Live Demoset.seed(101) Group

How to remove last few rows from an R data frame?

Nizamuddin Siddiqui
Updated on 24-Aug-2020 11:38:37

2K+ Views

An R data frame can contain a very large number of rows and we might want to get rid of some rows if they’re not supposed to be helpful in our data analysis. Therefore, we can remove these rows prior to starting the analysis process. We can say that this removal of some rows is a part of data cleaning and obviously data cleaning helps us creating a smooth data set for analysis. In R, we can simply use head function to remove last few rows from an R data frame, also we can store them as a new data ... Read More

How to sort one column of an R data frame in ascending and the other in descending order?

Nizamuddin Siddiqui
Updated on 24-Aug-2020 11:33:28

287 Views

Sorting of columns of an R data frame is not difficult but sometimes we want to sort them in opposite orders, for example, we might want to sort some columns in ascending order and some in descending order. This variation in sorting purpose makes it a little complicated. Therefore, we can use negation with sort function to sort the columns that we want to sort in descending order.ExampleConsider the below data frame − Live Demoset.seed(111) x1

How to change the position of the title of a plot which is created using plot function in R?

Nizamuddin Siddiqui
Updated on 24-Aug-2020 11:27:31

1K+ Views

When we create a plot using plot function, the title of the plot appears on top of the plot while using main argument. If we use title function to create the title of the plot then we can adjust its position in many different ways such as any position between below and top border of the plot.Examplesx

How to rescale a continuous variable so that the range of the rescale becomes 0 to 1 in R?

Nizamuddin Siddiqui
Updated on 24-Aug-2020 11:23:03

493 Views

Rescaling a continuous means that we want to standardize it with some properties and if we are using 0 to 1 as a range that represents that property. Most of the times, the objective behind rescaling is we want to nullify the effect of measurement units of the variable under consideration. To rescale so that the range becomes 0 to 1, we can use rescale function of scales package.ExampleLoading scales package −Examplelibrary(scales) x1

How to write partial title of X-axis in italics using ggplot2 of R?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 12:37:06

6K+ Views

Of course, writing axes titles help viewers to understand the plot in a better way because they add more information to the plot. In general, the axes titles have simple font but we can change partial or complete title to italics to get the viewers attraction. This is needed when we want to highlight the title by making it different. In ggplot2, we can do this by using expression.ExampleConsider the below data frame −set.seed(1) x

How to select top rows of an R data frame based on groups of factor column?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 12:33:35

255 Views

We use head function to take a look at some top values in an R data frame but it shows the top values for the whole data frame without considering the groups of factor column. Therefore, if we have a large number of values in a particular group then head function does not seem to be helpful alone, we must use something to extract the top values for each of the groups. This can be done through using by function with single square brackets and head function.Examplesdata(iris) str(iris) 'data.frame': 150 obs. of 5 variables: $ Sepal.Length: num 5.1 4.9 4.7 ... Read More

How to delete rows of an R data frame based on string match?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 12:32:18

1K+ Views

Often, we need to subset our data frame and sometimes this subsetting is based on strings. If we have a character column or a factor column then we might be having its values as a string and we can subset the whole data frame by deleting rows that contain a value or part of a value, for example, we can get rid of all rows that contain set or setosa word in Species column.ExampleConsider the below data frame −Character

How to convert a matrix to a data frame with column names and row names as new columns in R?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 12:30:43

2K+ Views

Sometimes we want to create a factor column of the column names and row names of a matrix so that we can use them in the analysis. It is required in situations where we want to know the effect of factor variables on the response and the factor variables were recorded as column names and row names in a matrix. To do this, we can convert the matrix into table and the table obtained is converted to data frame.ExampleConsider the below matrix −M1

How to remove the boxes around legend of a plot created by ggplot2 in R?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 12:29:10

3K+ Views

When we create a plot with legend using ggplot2, the legend values are covered with a box and that makes an impact on the smoothness of the plot. These boxes around the legend values can be removed so that complete the chart becomes more appealing to the viewer and it can be done with the help of theme function by setting the legend.key element to blank.ExampleConsider the below data frame −set.seed(1) x

Advertisements