0

I have data frame Df with five columns and fifth column(TypeWire) is concatenate of first two columns to make things simpler. For every Type(like A) there should be 'Land' and 'WiFi' but in my dataframe it is missing for some of the Type's(For ex=WiFi for D and B is missing). Now how can i add row if any of the Wire missing for Type? to make things simple as stated above column TypeWire has been added. I can manually add that to dataframe but i m looking for way that it can detect automatically and add the row(for ex= B WiFI 0 0 BWiFi) I tried to create below function just for type B and D (if thbut it is adding the entry if even it exist in TypeWire column like BLand is there in TypeWire but still row for that hasbeen created by below funtion.

Missadd2<-function(x){
    if(any(!(x$TypeWire == 'DWiFi'))){
      x <- rbind(x, newrow<-c("D","WiFi",0,0,"DWiFi"))
          }else if(any(!(x$TypeWire == 'BWiFi'))){
            x <- rbind(x, newrow<-c("B","WiFi",0,0,"BWiFi"))
  }else  if(any(!(x$TypeWire == 'BLand'))){
    x <- rbind(x, newrow<-c("B","Land",0,0,"BLand"))
  }
 }

Df

Uttam
  • 1
  • 1

1 Answers1

0

I'm not entirely sure if this is what you're looking for.

You can use complete from the tidyr package to give you the missing combinations of Type and Wire, so you should have both "Land" and "WiFi" for all the Types. You can specify fill with a list of values to include for the missing elements in the new rows added.

library(tidyr)

df %>%
  complete(Type, Wire, fill = list(Value = 0, Count = 0)) %>%
  mutate(TypeWire = paste0(Type, Wire))

Output

# A tibble: 10 x 5
   Type  Wire  Value Count TypeWire
   <chr> <chr> <dbl> <dbl> <chr>   
 1 A     Land    500    10 ALand   
 2 A     WiFi     89     2 AWiFi   
 3 B     Land    125     5 BLand   
 4 B     WiFi      0     0 BWiFi   
 5 C     Land    300     6 CLand   
 6 C     WiFi    100     1 CWiFi   
 7 D     Land    425     1 DLand   
 8 D     WiFi      0     0 DWiFi   
 9 O     Land   8150    22 OLand   
10 O     WiFi      0     0 OWiFi 

Data

df <- structure(list(Type = c("A", "A", "B", "C", "C", "D", "O"), Wire = c("Land", 
"WiFi", "Land", "Land", "WiFi", "Land", "Land"), Value = c(500, 
89, 125, 300, 100, 425, 8150), Count = c(10, 2, 5, 6, 1, 1, 22
)), class = "data.frame", row.names = c(NA, -7L))
Ben
  • 28,684
  • 5
  • 23
  • 45
  • Thanks Ben , I just saw your comment and this is what exactly I was looking for. One more thing I want to know is that what I have to do if like want to add row with Type U and E and Wire Land, WiFi for both with Value and count 0 if they aren't there in dataframe. Again it should happen automatically not a manual code – Uttam Aug 21 '20 at 19:14
  • If you have a vector of `Type` that you want to add, you can use `add_row` from `dplyr`, something like `add_row(Type = c("U", "E"), Wire = "Land")` and add to pipe at the top, before the `complete` statement...this will add a row for "Land", with `NA` for Value and Count, and then `complete` will add "Wifi" row, and fill in with zeroes, including the row just added... – Ben Aug 21 '20 at 19:53
  • I have Type= A,B,C,D,E,O,U and Wire=Land & Wire however in this dataframe I don't have U and E. To add that you have shown me the way but what I want to achieve is that from any other dataframe if any of the Type is missing, the code detects that and then add that data type into it. – Uttam Aug 25 '20 at 11:29
  • You can create a vector that contains all possible "Types" you would need, something like: `vec <- c("A", "B", "C", "D", "O", "U", "E")` ... then, you can do: `add_row(Type = setdiff(vec, df$Type), Wire = "Land")` instead, and this should work with any data frame called `df`, adding the missing types...is that what you had in mind? – Ben Aug 25 '20 at 11:43
  • 1
    Thank you Ben for helping me to overcome this hurdle – Uttam Aug 27 '20 at 10:17