Adding values for missing rows based on a column value

Sana Ali

I have a data frame:

things <- data.frame( category = c("A","B","A","B","B","A","B"),
               things2do = c("ball","ball","bat","bat","hockey","volley ball","foos ball"),
                  number = c(12,5,4,1,2,1,1))

now I want to add "0" in number where the particular category and things2do is missing e.g. a new row for "A", "hockey" and "0" should be added, same for volleyball, and foos ball.

I hope I can get some help here.

Marius

tidyr's complete() function does this:

library(tidyr)

things %>%
    complete(category, things2do, fill = list(number = 0))

Output:

# A tibble: 10 x 3
   category   things2do number
     <fctr>      <fctr>  <dbl>
 1        A        ball     12
 2        A         bat      4
 3        A   foos ball      0
 4        A      hockey      0
 5        A volley ball      1
 6        B        ball      5
 7        B         bat      1
 8        B   foos ball      1
 9        B      hockey      2
10        B volley ball      0

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Adding missing rows and setting column value to zero based on current dataframe

Move data from rows into columns in R, ignoring missing values and adding column based on the transposed columns

Fill new rows based on missing column values

Adding/duplicating rows to a dataframe based on the value in a column

Adding new rows based on column value in SQL?

Adding empty dataframe rows based on missing datetime values

Adding rows and incrementing datetime values to pandas data frame based on column value?

Adding a new column to a dataframe with a value which is based on the values from next rows

Adding a row for missing column value, with all other column values as NaNs

Pandas: Filling missing values, based on particular column, by adding some days

Adding multiple cells in a column based on a value in another column but same rows

Adding Missing Rows and Interpolating their Values

Identify common rows based on a column value and adding conditions for the grouping in SQL

merging rows based on common id in first column by adding those value?

Adding new rows with default value based on dataframe values into dataframe

Select rows by column value based on range of values in another column in R

Populating the column values from other rows based on a specific column value

Transposing column values into rows and grouping them based on value of another column

Removing rows with duplicated column values based on another column's value

Concatenate column values of multiple rows based on another column value

Add missing rows based on column

Adding rows with value "0" for missing rows in python

Replace missing values based on value of a specific column in Python

Adding values to dataframe columns based on value from a single column

adding rows based on a field value

adding rows based on values of other rows

Google Sheets: Group Values in rows based on duplicate value in column

Pandas: Replace values in a column with 'Other' based on the count of rows corresponding to the value

Combining rows on a Dataframe based on a specific column value and add other values