【发布时间】:2020-03-25 06:49:11
【问题描述】:
新手再次需要帮助。
我遇到了重复行的转置。
我的数据结构如下:
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
我想要:
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
客户信息将重复,_1 到 _10 列将变为行。
我有下面的代码。第一个收集代理运行良好,但是当我运行下一个收集时,它会复制我的 df,以及所有可能的组合。
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'))
我做错了什么?我试过group_by代理,但没用。
【问题讨论】: