Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201894

Gather duplicating rows

$
0
0

Newbie again in need for help.

I'm stuck with a transpose that is duplicating rows.

My data is structured like this:

id date client status  agent_1 agent_2 agent_3...agent_10  flag_1 Flag_2 Flag_3  order_num_1 Order_num_2
1  xxx   yyy   01        A1        C2    E3
2  xxx  yyyy   02        B1        D2    F3
3  xxx  yyy    03        C1        E2    G3

I want:

id  date   client  status  agent flag  order_num
1    xxx    yyy     01      A1
1    xxx   yyyy     01      C2
1    xxx   yyy      01      E3
2
2
3
3

The client information will repeat and the columns _1 to _10 will become rows.

I have the code below. The first gather agent works perfectly but then when I run the next gather it duplicates my df, with all possible combinations.

 df1 <- df %>% 
      select( id,
              created_on,
              date_reported,
              client_name,
              status_code, 
              starts_with('agent'), 
              starts_with('Flag'), 
              starts_with('order_num'),
              starts_with('order_date'),
              starts_with('manufacturer'))  %>%
      gather(key, value = "agent", starts_with('agent')) %>%
      select(-starts_with('key'))%>%
      filter(!is.na(agent))
      gather(key1, value = "flag", starts_with('Flag'))%>%
      gather(key2, value = "order_num", starts_with('order_num'))  %>%
      gather(key3, value = "order_date", starts_with('order_date'))   %>%
      gather(key4, value = "manufacturer", starts_with('manufacturer')) 

What am I doing wrong? I tried group_by agent but it didn't work.


Viewing all articles
Browse latest Browse all 201894

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>