重塑和扩展R中的数据框

时间:2019-01-31 04:27:14

标签: r dataframe

我在R中有一个数据帧,看起来像这样:

 customer      city      choice_1   choice_2   choice_3
    A        New York      Blue       Gray       Black
    B          Paris      Orange      Green      Brown
    C      San Francisco    Red      Yellow     Magenta

生成上述数据框的代码如下:

customer <- c("A","B","C")
city <- c("New York", "Paris", "San Francisco")
choice_1 <- c("Blue", "Orange", "Red")
choice_2 <- c("Gray", "Green", "Yellow")
choice_3 <- c("Black", "Brown", "Magenta")

df <- data.frame(customer, city, choice_1, choice_2, choice_3)

我想重塑和扩展数据框,使其看起来像这样:

 customer      city       choice   
    A        New York      Blue       
    A        New York      Gray       
    A        New York      Black       
    B          Paris      Orange      
    B          Paris       Green      
    B          Paris       Brown      
    C      San Francisco    Red      
    C      San Francisco   Yellow      
    C      San Francisco   Magenta      

如何在R中做到这一点?

0 个答案:

没有答案