将重复行转换为唯一列,填充第二列值(原始df)

时间:2018-04-14 21:33:27

标签: r

我有df1

 Client   Product
    Bob     House
    Bob       Car
   John     Shoes
   John      Food
   John     House
   Mary  Computer
   Mary Furniture
   Mary     Shoes
   Mary   Clothes

我需要重新排列 - 将其翻转到此df2

   Bob  John      Mary
 House Shoes  Computer
   Car  Food Furniture
    NA House     Shoes
    NA    NA   Clothes

PS:NAs是空值的占位符,因此它将是一个有效的data.frame对象。

df的代码 - s:

df1 <- data.frame(Client = c("Bob", "Bob", "John", "John", "John",
                            "Mary", "Mary", "Mary", "Mary"),
                 Product = c("House", "Car", "Shoes", "Food",
                             "House", "Computer", "Furniture",
                             "Shoes", "Clothes"))

df2 <- data.frame(Bob = c("House", "Car", "NA", "NA"),
                  John = c("Shoes", "Food", "House", "NA"),
                  Mary = c("Computer", "Furniture", "Shoes", "Clothes"))

1 个答案:

答案 0 :(得分:2)

使用base R,可以使用reshape函数完成。我必须每次都查找如何编码 - 使用示例。

(更简单)方法是使用spread包中的tidyr函数。

df2 <- df1 %>%
  mutate(one = 1) %>% 
  spread(Client, one)

另一种选择是为每个人制作一份清单:

list1 <- df1 %>% 
  split(.$Client) %>% 
  map(~ as.character(.$Product))

从这里开始,我们可以添加NAs来制作相同长度的列,并使其成为一个数据框,以获得您想要的内容:

n <- sapply(list1, length)
list1 %>%
  map(function(x) c(x, rep(NA, max(n) - length(x)))) %>%
  bind_cols()
相关问题